Permalink
Browse files

fixed bug where multiple calls are made on same callback

  • Loading branch information...
1 parent 2e7732a commit 2f3be4f50bd620f3255b9c0fb3fc96bf8dc7a8c4 @refractalize committed Mar 9, 2011
Showing with 75 additions and 6 deletions.
  1. +24 −6 cupoftea.js
  2. +37 −0 docs/map_test.js
  3. +14 −0 docs/multi_callback_test.js
View
@@ -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 numberOfCallbacks = 0;
+ var outstandingCallbacks = new OutstandingCallbacks();
var currentSpecResultsCalled = false;
var hasCallbacks = false;
@@ -139,9 +157,9 @@ var Callbacks = function (runStack) {
this.shouldCall = function (f) {
hasCallbacks = true;
- numberOfCallbacks++;
+ var callbackId = outstandingCallbacks.add();
return function () {
- numberOfCallbacks--;
+ outstandingCallbacks.remove(callbackId);
try {
var result = f.apply(null, arguments);
} catch (e) {
@@ -150,7 +168,7 @@ var Callbacks = function (runStack) {
throw e;
}
- if (numberOfCallbacks === 0) {
+ if (outstandingCallbacks.isEmpty()) {
results();
}
@@ -163,7 +181,7 @@ var Callbacks = function (runStack) {
};
this.assertCallbacks = function () {
- if (numberOfCallbacks > 0) {
+ if (!outstandingCallbacks.isEmpty()) {
results('not called');
} else if (hasCallbacks) {
results();
@@ -269,7 +287,7 @@ process.addListener('exit', function () {
process.on('uncaughtException', function(err) {
if (!_(expectedExceptions).contains(err)) {
- throw err;
+ console.log(err);
} else {
console.log('caught exception');
}
View
@@ -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]);
+ });
+});
@@ -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.