Merge pull request #50 from mapbox/iteration

Error handling in async loop
This commit is contained in:
Ryan Clark
2015-01-05 11:31:52 -05:00
+24 -13
View File
@@ -31,7 +31,7 @@ MBTiles.schema = fs.readFileSync(__dirname + '/schema.sql', 'utf8');
// - callback: Will be called when the resources have been acquired // - callback: Will be called when the resources have been acquired
// or acquisition failed. // or acquisition failed.
require('util').inherits(MBTiles, require('events').EventEmitter) require('util').inherits(MBTiles, require('events').EventEmitter)
function MBTiles(uri, callback) { function MBTiles(uri, callback) {
if (typeof uri === 'string') { if (typeof uri === 'string') {
uri = url.parse(uri, true); uri = url.parse(uri, true);
uri.pathname = qs.unescape(uri.pathname); uri.pathname = qs.unescape(uri.pathname);
@@ -274,17 +274,29 @@ MBTiles.prototype.getInfo = function(callback) {
var zooms = []; var zooms = [];
var query = mbtiles._db.prepare('SELECT zoom_level FROM tiles WHERE zoom_level = ? LIMIT 1', function(err) { var query = mbtiles._db.prepare('SELECT zoom_level FROM tiles WHERE zoom_level = ? LIMIT 1', function(err) {
if (err) return callback(err.errno === 1 ? null : err, info); if (err) return callback(err.errno === 1 ? null : err, info);
for (var i = 0; i < remaining; i++) query.get(i, function(err, row) {
if (err) return (remaining = 0) && callback(err); function done(err, info) {
if (row) zooms.push(row.zoom_level); if (done.sent) return;
if (--remaining === 0) { callback(err, info);
if (!zooms.length) return callback(null, info); done.sent = true;
zooms.sort(function(a,b) { return a < b ? -1 : 1 }); }
info.minzoom = zooms[0];
info.maxzoom = zooms.pop(); done.sent = false;
return callback(null, info);
} for (var i = 0; i < remaining; i++) {
}); query.get(i, function(err, row) {
if (err) return done(err);
if (row) zooms.push(row.zoom_level);
if (--remaining === 0) {
if (!zooms.length) return callback(null, info);
zooms.sort(function(a,b) { return a < b ? -1 : 1; });
info.minzoom = zooms[0];
info.maxzoom = zooms.pop();
return done(null, info);
}
});
}
query.finalize(); query.finalize();
}); });
}; };
@@ -752,4 +764,3 @@ MBTiles.prototype.geocoderCentroid = function(id, zxy, callback) {
MBTiles.prototype.createZXYStream = function(options) { MBTiles.prototype.createZXYStream = function(options) {
return new ZXYStream(this, options); return new ZXYStream(this, options);
}; };