Skip to content

Commit

Permalink
Fix doneLater bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jun 18, 2013
1 parent e34185f commit 0bf5627
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 36 deletions.
43 changes: 43 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"predef": [
"document",
"module",
"require",
"__dirname",
"process",
"console",
"it",
"xit",
"describe",
"xdescribe",
"before",
"beforeEach",
"after",
"afterEach"
],
"node": true,
"es5": true,
"bitwise": true,
"curly": true,
"eqeqeq": true,
"forin": false,
"immed": true,
"latedef": true,
"newcap": false,
"noarg": true,
"noempty": true,
"nonew": true,
"plusplus": false,
"undef": true,
"strict": false,
"trailing": false,
"globalstrict": true,
"nonstandard": true,
"white": true,
"indent": 2,
"expr": true,
"multistr": true,
"onevar": false,
"unused": "vars",
"swindent": false
}
8 changes: 5 additions & 3 deletions lib/eventproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,16 @@
return that.emit('error', err);
}

// callback(err, args1, args2, ...)
var args = Array.prototype.slice.call(arguments, 1);

if (typeof handler === 'string') {
// getAsync(query, ep.done('query'));
// or
// getAsync(query, ep.done('query', function (data) {
// return data.trim();
// }));
return that.emit(handler, callback ? callback.call(data) : data);
return that.emit(handler, callback ? callback.apply(null, args) : data);
}

// speed improve for mostly case: `callback(err, data)`
Expand All @@ -489,7 +492,6 @@
}

// callback(err, args1, args2, ...)
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(null, args);
};
};
Expand All @@ -505,7 +507,7 @@
_doneHandler.apply(null, args);
});
};
};
};

/**
* Create a new EventProxy
Expand Down
115 changes: 82 additions & 33 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.bind("event", function (data) {
counter += 1;
counter += 1;
});
ep.trigger("event");
assert.equal(counter, 1, 'Counter should be incremented.');
Expand All @@ -47,7 +47,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.bind('event', function () {
counter += 1;
counter += 1;
});
ep.trigger('event');
assert.equal(counter, 1, 'counter should be incremented.');
Expand All @@ -60,7 +60,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.bind('event', function () {
counter += 1;
counter += 1;
});
ep.trigger('event');
assert.equal(counter, 1, 'counter should be incremented.');
Expand All @@ -73,7 +73,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.once('event', function () {
counter += 1;
counter += 1;
});
ep.trigger('event');
assert.equal(counter, 1, 'counter should be incremented.');
Expand All @@ -84,8 +84,8 @@ describe("EventProxy", function () {
it('immediate', function () {
var ep = EventProxy.create();
var counter = 0;
ep.immediate('event', function (){
counter +=1;
ep.immediate('event', function () {
counter += 1;
});
assert.equal(counter, 1, "counter should be incremented.");
ep.trigger('event');
Expand All @@ -96,8 +96,8 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var param = new Date(), counter = 0;
ep.immediate('event', function (data) {
assert.equal(data, param, "data should same as param.");
counter += 1;
assert.equal(data, param, "data should same as param.");
counter += 1;
}, param);
assert.equal(counter, 1, "counter should be incremented.");
ep.trigger('event', param);
Expand All @@ -108,7 +108,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.assign('event', function () {
counter += 1;
counter += 1;
});
ep.trigger('event');
assert.equal(counter, 1, 'counter should be incremented.');
Expand All @@ -120,9 +120,9 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.assign('event1', 'event2', function (event1, event2) {
assert.equal(event1, 'event1', 'counter should not be incremented.');
assert.equal(event2, 'event2', 'counter should not be incremented.');
counter += 1;
assert.equal(event1, 'event1', 'counter should not be incremented.');
assert.equal(event2, 'event2', 'counter should not be incremented.');
counter += 1;
});
ep.trigger('event1', 'event1');
assert.equal(counter, 0, 'counter should not be incremented.');
Expand All @@ -137,9 +137,9 @@ describe("EventProxy", function () {
var counter = 0;
var events = ['event1', 'event2'];
ep.assign(events, function (event1, event2) {
assert.equal(event1, 'event1', 'counter should not be incremented.');
assert.equal(event2, 'event2', 'counter should not be incremented.');
counter += 1;
assert.equal(event1, 'event1', 'counter should not be incremented.');
assert.equal(event2, 'event2', 'counter should not be incremented.');
counter += 1;
});
ep.trigger('event1', 'event1');
assert.equal(counter, 0, 'counter should not be incremented.');
Expand All @@ -154,9 +154,9 @@ describe("EventProxy", function () {
var counter = 0;
var event2 = null;
ep.assignAlways('event1', 'event2', function (data1, data2) {
counter += 1;
assert.equal(data1, 'event1');
assert.equal(data2, event2, 'Second data should same as event2.');
counter += 1;
assert.equal(data1, 'event1');
assert.equal(data2, event2, 'Second data should same as event2.');
});
ep.trigger('event1', 'event1');
assert.equal(counter, 0, 'counter should not be incremented.');
Expand All @@ -176,13 +176,13 @@ describe("EventProxy", function () {
var n = Math.round(Math.random() * 100) + 1;
var counter = 0;
ep.after('event', n, function (data) {
assert.deepEqual(data.length, n);
for(var i = 0, l = data.length; i < l; i++) {
assert.deepEqual(data[i], i);
}
counter += 1;
assert.deepEqual(data.length, n);
for (var i = 0, l = data.length; i < l; i++) {
assert.deepEqual(data[i], i);
}
counter += 1;
});
for(var i = 0, last = n - 1; i < n; i++) {
for (var i = 0, last = n - 1; i < n; i++) {
ep.trigger('event', i);
if (i !== last) {
assert.deepEqual(counter, 0, 'counter should not be incremented.');
Expand All @@ -199,9 +199,9 @@ describe("EventProxy", function () {

var counter = 0;
ep.after('event', 1, function (data) {
assert.deepEqual(data.length, 1);
assert.deepEqual(data[0], "1 time");
counter += 1;
assert.deepEqual(data.length, 1);
assert.deepEqual(data[0], "1 time");
counter += 1;
});

ep.trigger('event', "1 time");
Expand Down Expand Up @@ -283,7 +283,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();
var counter = 0;
ep.not('event1', function (data) {
counter += 1;
counter += 1;
});
ep.trigger('event1', 1);
assert.deepEqual(counter, 0, 'counter should not be incremented.');
Expand All @@ -309,7 +309,6 @@ describe("EventProxy", function () {

it('done(event)', function (done) {
var ep = EventProxy.create();
var counter = 0;
ep.bind('event1', function (data) {
should.exist(data);
done();
Expand All @@ -331,6 +330,57 @@ describe("EventProxy", function () {
}));
});

it('done(event, fn) multi data', function (done) {
var async = function (callback) {
process.nextTick(function () {
callback(null, 'data1', 'data2');
});
};
var ep = EventProxy.create();
ep.on("file", function (data) {
should.exist(data);
assert.deepEqual(data, 'data1data2', 'data should be modified');
done();
});
ep.bind('error', done);
async(ep.done('file', function (data1, data2) {
return data1 + data2;
}));
});

it('doneLater(event, fn)', function (done) {
var ep = EventProxy.create();
fs.readFile(__filename, "utf-8", ep.doneLater("file", function (str) {
return 'hehe';
}));

ep.on("file", function (data) {
should.exist(data);
assert.deepEqual(data, 'hehe', 'data should be modified');
done();
});
ep.bind('error', done);
});

it('doneLater(event, fn) multi data', function (done) {
var async = function (callback) {
process.nextTick(function () {
callback(null, 'data1', 'data2');
});
};
var ep = EventProxy.create();
async(ep.doneLater('file', function (data1, data2) {
return data1 + data2;
}));

ep.on("file", function (data) {
should.exist(data);
assert.deepEqual(data, 'data1data2', 'data should be modified');
done();
});
ep.bind('error', done);
});

describe('errorHandler mode', function () {
it('should auto handler callback error', function (done) {
done = pedding(2, done);
Expand Down Expand Up @@ -435,7 +485,7 @@ describe("EventProxy", function () {
}
function mockGet(query, callback) {
process.nextTick(callback.bind(null, null, query + 'args1'));
}
}

function mockGetSync(query, callback) {
callback(null, query + 'args1');
Expand All @@ -444,7 +494,6 @@ describe("EventProxy", function () {
it('should doneLater work fine', function (done) {
var query = 'laterQuery';
var ep = EventProxy.create();

check(query, ep.doneLater('check'));

ep.once('check', function (permission) {
Expand All @@ -460,7 +509,7 @@ describe("EventProxy", function () {
it('should doneLater work fine when both sync', function (done) {
var query = 'laterQuery';
var ep = EventProxy.create();

check(query, ep.doneLater('check'));

ep.once('check', function (permission) {
Expand Down Expand Up @@ -492,7 +541,7 @@ describe("EventProxy", function () {
var ep = EventProxy.create();

mcheck(query, ep.doneLater(function (permission1, permission2) {
permission1 && permission2 && mockGet(query, ep.done('mockGet'))
permission1 && permission2 && mockGet(query, ep.done('mockGet'));
}));

ep.once('mockGet', function (a1) {
Expand Down

0 comments on commit 0bf5627

Please sign in to comment.