Ditch inflateSync in favor of plain inflate; test iteration order; add d3-queue dep

This commit is contained in:
Andrew Pendleton
2016-04-14 18:49:34 -04:00
parent 4474634452
commit 405d987716
3 changed files with 72 additions and 39 deletions
+25 -16
View File
@@ -9,6 +9,7 @@ var sm = new (require('sphericalmercator'));
var sqlite3 = require('sqlite3');
var tiletype = require('tiletype');
var ZXYStream = require('./zxystream');
var queue = require('d3-queue').queue;
function noop(err) {
if (err) throw err;
@@ -625,34 +626,42 @@ MBTiles.prototype.geocoderDataIterator = function(type) {
var doneSentinel = {};
var _this = this;
var zlibQueue = queue(1);
var inflate = function(data, callback) {
zlibQueue.defer(function(cb) {
zlib.inflate(data, function(err, buf) {
callback(err, buf);
cb();
});
});
}
var sending = false;
var sendIfAvailable = function() {
if (sending) return;
sending = true;
while (nextQueue.length && dataQueue.length) {
var nextCb = nextQueue.shift(), data, cbValue;
var nextCb = nextQueue.shift(), data;
if (dataQueue[0] == doneSentinel) {
cbValue = {
err: null,
row: {value: undefined, done: true}
}
(function(nextCb) {
setImmediate(function() {
nextCb(null, {value: undefined, done: true});
});
})(nextCb);
} else {
data = dataQueue.shift();
maybeRefillBuffer();
cbValue = {
err: data.err,
row: {value: {shard: data.row.shard, data: zlib.inflateSync(data.row.data)}, done: false}
};
(function(nextCb, cbValue) {
inflate(data.row.data, function(err, buf) {
nextCb(
data.err,
{value: {shard: data.row.shard, data: buf}, done: false}
);
})
})(nextCb, data);
}
// bind the callback and data now so that they don't change before the setImmediate
// callback is executed
(function(nextCb, cbValue) {
setImmediate(function() {
nextCb(cbValue.err, cbValue.row);
});
})(nextCb, cbValue);
}
sending = false;