I give up: setImmediate for every callback

This commit is contained in:
Andrew Pendleton
2016-04-04 13:16:52 -04:00
parent b37655577f
commit b6ef79d7dc

View File

@@ -615,9 +615,6 @@ MBTiles.prototype.putGeocoderData = function(type, shard, data, callback) {
});
};
var stackCounter = 0;
var stackMax = 3;
// Implements carmen#geocoderDataIterator method.
MBTiles.prototype.geocoderDataIterator = function(type) {
var chunkSize = 100;
@@ -628,35 +625,28 @@ MBTiles.prototype.geocoderDataIterator = function(type) {
var doneSentinel = {};
var _this = this;
// every nth callback call (globally) do it via setImmediate
// to avoid callback loops that blow the callstack
var checkStack = function(cb, arg) {
if (stackCounter >= stackMax) {
stackCounter = 0;
setImmediate(function() {
cb(arg);
})
} else {
stackCounter += 1;
cb(arg);
}
}
var sending = false;
var sendIfAvailable = function() {
if (sending) return;
sending = true;
while (nextQueue.length && dataQueue.length) {
var nextCb = nextQueue.shift(), data;
var nextCb = nextQueue.shift(), data, cbValue;
if (dataQueue[0] == doneSentinel) {
checkStack(nextCb, {value: undefined, done: true});
cbValue = {value: undefined, done: true};
} else {
data = dataQueue.shift();
maybeRefillBuffer();
checkStack(nextCb, {value: {shard: data.shard, data: zlib.inflateSync(data.data)}, done: false});
cbValue = {value: {shard: data.shard, data: zlib.inflateSync(data.data)}, done: false};
}
// 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);
});
})(nextCb, cbValue);
}
sending = false;