Skip to content

Commit

Permalink
Make guarantee reentrant by leaving results alone. Removed JSDoc comm…
Browse files Browse the repository at this point in the history
…ents now that there's an API doc.
  • Loading branch information
atesgoral committed Jan 27, 2011
1 parent 5135fa1 commit 3e4d687
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions satisfaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,16 @@
*
* MIT or BSD license. Does something this short even need a license?
*/

/**
* @param satisfy Callback to call when all async expectations are satisfied
* @param scope The optional callback scope
*/
function Satisfaction(satisfy, scope) {
function Satisfaction(satisfy, scope) {
var queued = 0,
met = 0,
results = [],
wanting;

function guarantee() {
results.push.apply(results, wanting.args);
queued == met && satisfy.apply(scope, results);
queued == met && satisfy.apply(scope, results.concat(wanting.args));
}

/**
* Set an expectation by intercepting a callback
* @param callback The callback function we're intercepting
*/
this.from = function (callback, scope) {
return (function (satisfaction, idx) {
return function () {
Expand All @@ -39,11 +29,8 @@
})(this, queued++);
};

/**
* Call when you're done setting expectations
*/
this.guarantee = function () {
wanting = { args: arguments };
wanting = { args: Array.prototype.slice.call(arguments) };
guarantee();
};
}

0 comments on commit 3e4d687

Please sign in to comment.