I give up: setImmediate for every callback
This commit is contained in:
+10
-20
@@ -615,9 +615,6 @@ MBTiles.prototype.putGeocoderData = function(type, shard, data, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var stackCounter = 0;
|
|
||||||
var stackMax = 3;
|
|
||||||
|
|
||||||
// Implements carmen#geocoderDataIterator method.
|
// Implements carmen#geocoderDataIterator method.
|
||||||
MBTiles.prototype.geocoderDataIterator = function(type) {
|
MBTiles.prototype.geocoderDataIterator = function(type) {
|
||||||
var chunkSize = 100;
|
var chunkSize = 100;
|
||||||
@@ -628,35 +625,28 @@ MBTiles.prototype.geocoderDataIterator = function(type) {
|
|||||||
var doneSentinel = {};
|
var doneSentinel = {};
|
||||||
var _this = this;
|
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 sending = false;
|
||||||
var sendIfAvailable = function() {
|
var sendIfAvailable = function() {
|
||||||
if (sending) return;
|
if (sending) return;
|
||||||
sending = true;
|
sending = true;
|
||||||
|
|
||||||
while (nextQueue.length && dataQueue.length) {
|
while (nextQueue.length && dataQueue.length) {
|
||||||
var nextCb = nextQueue.shift(), data;
|
var nextCb = nextQueue.shift(), data, cbValue;
|
||||||
if (dataQueue[0] == doneSentinel) {
|
if (dataQueue[0] == doneSentinel) {
|
||||||
checkStack(nextCb, {value: undefined, done: true});
|
cbValue = {value: undefined, done: true};
|
||||||
} else {
|
} else {
|
||||||
data = dataQueue.shift();
|
data = dataQueue.shift();
|
||||||
maybeRefillBuffer();
|
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;
|
sending = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user