Skip to content
Browse files

EventEmitter: Fix "emit / once" edge case

  • Loading branch information...
1 parent 799affa commit 470409f5fc2970505e038e392e97820e65c0ca7e @basecode basecode committed Feb 28, 2014
Showing with 19 additions and 5 deletions.
  1. +6 −5 src/event_emitter.js
  2. +13 −0 test/event_emitter-spec.js
View
11 src/event_emitter.js
@@ -149,15 +149,16 @@ define([
context = listener.context;
listenerFunc = listener.listener;
- if (typeof listenerFunc === 'string') {
- listenerFunc = context[listenerFunc];
- }
- listenerFunc.apply(context || this, args);
-
listenerTimes = listener.times -= 1;
if (listenerTimes <= 0) {
this.removeListener(type, context, listener.listener);
}
+
+ if (typeof listenerFunc === 'string') {
+ listenerFunc = context[listenerFunc];
+ }
+
+ listenerFunc.apply(context || this, args);
}
return this;
View
13 test/event_emitter-spec.js
@@ -264,6 +264,19 @@ define([
expect(remove).not.toHaveBeenCalled();
}
);
+
+ it('should not invoke a listener within its own callback added through `once`', function() {
+ var e = emitter;
+ var callCount = 0;
+
+ e.once('foo', function() {
+ callCount++;
+ e.emit('foo');
+ });
+ e.emit('foo');
+
+ expect(callCount).toBe(1);
+ });
});
describe('removeListener', function() {

0 comments on commit 470409f

Please sign in to comment.
Something went wrong with that request. Please try again.