Permalink
Browse files

Handle multiple versions of same function for event. Fixes #5

  • Loading branch information...
scottcorgan committed Jan 9, 2014
1 parent 8c38769 commit c6ebfaa9bc973b33d110a84a307742b7cf94c953
Showing with 53 additions and 19 deletions.
  1. +5 −8 dist/tinyemitter.js
  2. +1 −1 dist/tinyemitter.min.js
  3. +5 −8 index.js
  4. +1 −1 package.json
  5. +41 −1 test/index.js
@@ -42,18 +42,15 @@ E.prototype = {
off: function (name, callback) {
var e = this.e;
var evts = e[name];
var i = 0;
var len = evts.length;
var liveEvents = [];

if (evts && callback) {
for (i; i < len; i++) {
if (evts[i] && evts[i].fn !== callback) continue;
evts.splice(i, 1);
for (var i = 0, len = evts.length; i < len; i++) {
if (evts[i] && evts[i].fn !== callback) liveEvents.push(evts[i]);
}
}
else if (evts) {
e[name] = [];
}

e[name] = liveEvents;

return this;
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -41,18 +41,15 @@ E.prototype = {
off: function (name, callback) {
var e = this.e;
var evts = e[name];
var i = 0;
var len = evts.length;
var liveEvents = [];

if (evts && callback) {
for (i; i < len; i++) {
if (evts[i] && evts[i].fn !== callback) continue;
evts.splice(i, 1);
for (var i = 0, len = evts.length; i < len; i++) {
if (evts[i] && evts[i].fn !== callback) liveEvents.push(evts[i]);

This comment has been minimized.

@lazd

lazd Jan 9, 2014

Given that you're always pushing an object inside of on(), and that splice is used in off() to remove items from the array, it's not necessary to check if evts[i] is truthy. Maybe this was a workaround for an error that resulted from #5?

This comment has been minimized.

@scottcorgan

scottcorgan Jan 9, 2014

Owner

I believe it's there to handle calling off() for an event that was never bound to.

This comment has been minimized.

@lazd

lazd Jan 9, 2014

If it was never bound to, evts would be undefined, so this code wouldn't run.

This comment has been minimized.

@scottcorgan

scottcorgan Jan 9, 2014

Owner

You were right. FIxed it in e750ab9

}
}
else if (evts) {
e[name] = [];
}

e[name] = liveEvents;

This comment has been minimized.

@lazd

lazd Jan 9, 2014

You might as well delete the property if liveEvents.length === 0 if you're concerned about memory usage. The only reason to keep an empty array around in this case would be if you expected listeners to be re-added to this event, which would incur the creation of another empty array in on().


return this;
}
@@ -1,6 +1,6 @@
{
"name": "tiny-emitter",
"version": "0.1.8",
"version": "0.1.9",
"description": "A tiny (less than 1k) event emitter library",
"main": "index.js",
"scripts": {
@@ -100,6 +100,46 @@ test('unsubscribes single event with name and callback', function (t) {
});
});

test('unsubscribes single event with name and callback when subscribed twice', function (t) {
var emitter = new Emitter();
var fn = function () {
t.ok(false, 'should not get called');
};

emitter.on('test', fn);
emitter.on('test', fn);

emitter.off('test', fn);
emitter.emit('test');

process.nextTick(function () {
t.equal(emitter.e['test'].length, 0, 'removes all events');
t.end();
});
});

test('unsubscribes single event with name and callback when subscribed twice out of order', function (t) {
var emitter = new Emitter();
var calls = 0;
var fn = function () {
t.ok(false, 'should not get called');
};
var fn2 = function () {
calls++;
};

emitter.on('test', fn);
emitter.on('test', fn2);
emitter.on('test', fn);
emitter.off('test', fn);
emitter.emit('test');

process.nextTick(function () {
t.equal(calls, 1, 'callback was called');
t.end();
});
});

test('removes an event inside another event', function (t) {
var emitter = new Emitter();

@@ -139,4 +179,4 @@ test('event is emitted even if unsubscribed in the event callback', function (t)
});

emitter.emit('test');
});
});

0 comments on commit c6ebfaa

Please sign in to comment.