Skip to content

Commit fc1f0c8

Browse files
committed
Update code that calls the callbacks
1 parent cb2f47e commit fc1f0c8

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ exports.add = function (key, callback) {
7575
return false;
7676
}
7777

78-
bucket = callbackQueue[key] = [callback];
79-
80-
/**
81-
* This is wrapped in once so that we might escape all sorts of shit-like code
82-
* where people forget to remove certain even listeners and callback can be called
83-
* twice, but for another request. If you are certain that your code lacks these
84-
* stupid mistakes - you are more than welcome to fork and remove this restriction
85-
*/
78+
callbackQueue[key] = [callback];
79+
8680
return function queuedCallback() {
81+
// its essential that we do not use any reference, because of garbabe collection
82+
// when object reaches certain number of nullified values - its recreated using compactObject
83+
// function. Therefore we need to grab a reference when callback needs to be invoked and not at
84+
// other time
85+
var callbacks = callbackQueue[key];
86+
8787
debug('calling callback for key %s', key);
8888

89-
if (!bucket || callbackQueue[key] !== bucket || !isArray(bucket)) {
90-
debug('Callbacks couldn\'t be invoked: ', bucket, callbackQueue[key] !== bucket);
89+
if (!isArray(callbacks)) {
90+
debug('Callbacks couldn\'t be invoked: ', callbacks);
9191
bucket = null;
9292
return;
9393
} else {
@@ -100,9 +100,8 @@ exports.add = function (key, callback) {
100100
args[i] = arguments[i];
101101
}
102102

103-
iterateOverCallbacks(bucket, args);
103+
iterateOverCallbacks(callbacks, args);
104104
cleanup(key);
105-
bucket = null;
106105
};
107106

108107
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "callback-queue",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)