Skip to content

Commit

Permalink
fixed bug where multiple calls are made on same callback
Browse files Browse the repository at this point in the history
  • Loading branch information
refractalize committed Mar 9, 2011
1 parent 2e7732a commit 2f3be4f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
30 changes: 24 additions & 6 deletions cupoftea.js
Expand Up @@ -117,8 +117,26 @@ var TopSpec = function (runStack) {
} }
}; };


var OutstandingCallbacks = function () {
var currentCallbackId = 0;
var callbacks = {};

this.add = function () {
callbacks[currentCallbackId] = true;
return currentCallbackId++;
};

this.remove = function (id) {
delete callbacks[id];
};

this.isEmpty = function () {
return _.isEmpty(callbacks);
};
};

var Callbacks = function (runStack) { var Callbacks = function (runStack) {
var numberOfCallbacks = 0; var outstandingCallbacks = new OutstandingCallbacks();
var currentSpecResultsCalled = false; var currentSpecResultsCalled = false;
var hasCallbacks = false; var hasCallbacks = false;


Expand All @@ -139,9 +157,9 @@ var Callbacks = function (runStack) {


this.shouldCall = function (f) { this.shouldCall = function (f) {
hasCallbacks = true; hasCallbacks = true;
numberOfCallbacks++; var callbackId = outstandingCallbacks.add();
return function () { return function () {
numberOfCallbacks--; outstandingCallbacks.remove(callbackId);
try { try {
var result = f.apply(null, arguments); var result = f.apply(null, arguments);
} catch (e) { } catch (e) {
Expand All @@ -150,7 +168,7 @@ var Callbacks = function (runStack) {
throw e; throw e;
} }


if (numberOfCallbacks === 0) { if (outstandingCallbacks.isEmpty()) {
results(); results();
} }


Expand All @@ -163,7 +181,7 @@ var Callbacks = function (runStack) {
}; };


this.assertCallbacks = function () { this.assertCallbacks = function () {
if (numberOfCallbacks > 0) { if (!outstandingCallbacks.isEmpty()) {
results('not called'); results('not called');
} else if (hasCallbacks) { } else if (hasCallbacks) {
results(); results();
Expand Down Expand Up @@ -269,7 +287,7 @@ process.addListener('exit', function () {


process.on('uncaughtException', function(err) { process.on('uncaughtException', function(err) {
if (!_(expectedExceptions).contains(err)) { if (!_(expectedExceptions).contains(err)) {
throw err; console.log(err);
} else { } else {
console.log('caught exception'); console.log('caught exception');
} }
Expand Down
37 changes: 37 additions & 0 deletions docs/map_test.js
@@ -0,0 +1,37 @@
require('../cupoftea');
var fs = require('fs');
var assert = require('assert');
var _ = require('underscore');

spec('set', function () {
var set = {};

spec('adding item', function () {
set[0] = true;

spec('and retrieving it', function () {
assert.ok(set[0]);
});

spec('and then unsetting it', function () {
set[0] = undefined;
assert.ok(!set[0]);
});
});

spec('can add items and enumerate items', function () {
set[0] = true;
set[2] = true;
delete set[2];
set[3] = true;

var keys = _.keys(set);
assert.ok(_(keys).contains('0'));
assert.ok(_(keys).contains('3'));
assert.equal(keys.length, 2);
});

spec('getting item not set', function () {
assert.ok(!set[0]);
});
});
14 changes: 14 additions & 0 deletions docs/multi_callback_test.js
@@ -0,0 +1,14 @@
require('../cupoftea');
var fs = require('fs');
var assert = require('assert');

spec('multiple callbacks', function () {
var i = 1;
setInterval(shouldCall(function () {
console.log('first');
}), 100);

setTimeout(shouldCall(function () {
console.log('second');
}), 1000);
});

0 comments on commit 2f3be4f

Please sign in to comment.