Skip to content

Commit

Permalink
Added guard to fullproof.make_synchro_point to make sure it doesn't c…
Browse files Browse the repository at this point in the history
…all its callback multiple times.
  • Loading branch information
terrycojones committed Mar 10, 2013
1 parent 461aabf commit 1850fe1
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/utils.js
Expand Up @@ -75,11 +75,14 @@ fullproof.ScoredEntry.prototype.toString = function () {


/**
* Creates a synchronization point. This function returns a function that collects
* calls to callback, then calls its callback argument with all the data collected.
* The synchronization point can trigger the final call to callback when it either
* receives a fixed number of calls (expected argument >= 1), or when it
* receives a false boolean value as argument (expected has to be either undefined or false)
* Creates a synchronization point. Return a function that collects
* results and calls its callback argument with the collected data.
* The synchronization point will trigger the callback when either (a) it
* receives a predetermined number of results (expected argument >= 1), or
* (b) it receives a false boolean value as argument (expected has to be
* either undefined or false).
* Note that the callback function will never be called more than once.
* @param {function} callback the function to call when the synchronization point is reached
* @param {number} expected defines the synchronization point. If this is a number, the synchronization is
Expand All @@ -90,13 +93,17 @@ fullproof.ScoredEntry.prototype.toString = function () {
fullproof.make_synchro_point = function (callback, expected, debug, thrown_if_false) {
var count = 0;
var results = [];
var callbackCalled = false;
return function (res) {
if (thrown_if_false !== undefined && res === false) {
throw thrown_if_false;
}
if (expected === false || expected === undefined) {
if (res === false) {
callback(results);
if (callbackCalled === false) {
callbackCalled = true;
callback(results);
}
} else {
results.push(res);
}
Expand All @@ -108,10 +115,13 @@ fullproof.make_synchro_point = function (callback, expected, debug, thrown_if_fa
console.log("synchro point " + (typeof debug == "string" ? debug + ": " : ": ") + count + " / " + expected);
}
if (count == expected) {
callback(results);
if (callbackCalled === false) {
callbackCalled = true;
callback(results);
}
}
}
}
};
};

fullproof.call_new_thread = function() {
Expand Down

0 comments on commit 1850fe1

Please sign in to comment.