From b587eb94d1f95cba98814d4319e734dda27c085a Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Thu, 8 Aug 2013 17:36:35 +0100 Subject: [PATCH 1/4] Added a failing test for recursive once calls. Taken from issue #54 --- tests/tests.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/tests.js b/tests/tests.js index 1c5b4a3..f5086f3 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -152,6 +152,15 @@ ee.removeListener('foo', fn1); assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); }); + + test('can not cause infinite recursion', function () { + ee.addOnceListener('foo', function() { + counter += 1; + this.emitEvent('foo'); + }); + ee.trigger('foo'); + assert.strictEqual(counter, 1); + }); }); suite('removeListener', function() { From 53a28759584ab31c8a3629cc6f679da5ea2bf023 Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Thu, 8 Aug 2013 17:45:28 +0100 Subject: [PATCH 2/4] Added a fix for the recursive once calls. Now once methods are removed from the listener list before the method is actually called. This prevents infinite recursion when using addOnceListener. It will not prevent the once listeners that are indicated with a return value, there is no way around that one. Fixes #54 --- EventEmitter.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/EventEmitter.js b/EventEmitter.js index ca80b06..88702fe 100644 --- a/EventEmitter.js +++ b/EventEmitter.js @@ -365,8 +365,14 @@ // If the listener returns true then it shall be removed from the event // The function is executed either with a basic call or an apply if there is an args array listener = listeners[key][i]; + + if (listener.once === true) { + this.removeListener(evt, listener.listener); + } + response = listener.listener.apply(this, args || []); - if (response === this._getOnceReturnValue() || listener.once === true) { + + if (response === this._getOnceReturnValue()) { this.removeListener(evt, listener.listener); } } From 6b009740d87384c3dd29d5c50c3f7af95ece26e7 Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Thu, 8 Aug 2013 17:52:45 +0100 Subject: [PATCH 3/4] Bumped the version number to 4.2.3 --- EventEmitter.js | 2 +- bower.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/EventEmitter.js b/EventEmitter.js index 88702fe..0443d1f 100644 --- a/EventEmitter.js +++ b/EventEmitter.js @@ -1,5 +1,5 @@ /*! - * EventEmitter v4.2.2 - git.io/ee + * EventEmitter v4.2.3 - git.io/ee * Oliver Caldwell * MIT license * @preserve diff --git a/bower.json b/bower.json index fc386c9..5a0dbde 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "eventEmitter", "description": "Event based JavaScript for the browser", - "version": "4.2.2", + "version": "4.2.3", "main": [ "./EventEmitter.js" ], diff --git a/package.json b/package.json index 49a31a4..83bb74c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wolfy87-eventemitter", - "version": "4.2.2", + "version": "4.2.3", "description": "Event based JavaScript for the browser", "main": "EventEmitter.js", "directories": { From 3470e51014805ed83e65207e049accded31c0d71 Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Thu, 8 Aug 2013 17:53:25 +0100 Subject: [PATCH 4/4] Built v4.2.3. --- EventEmitter.min.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EventEmitter.min.js b/EventEmitter.min.js index c558764..5ba2c37 100644 --- a/EventEmitter.min.js +++ b/EventEmitter.min.js @@ -1,7 +1,7 @@ /*! - * EventEmitter v4.2.2 - git.io/ee + * EventEmitter v4.2.3 - git.io/ee * Oliver Caldwell * MIT license * @preserve */ -!function(){"use strict";function t(){}function r(t,n){for(var e=t.length;e--;)if(t[e].listener===n)return e;return-1}function n(e){return function(){return this[e].apply(this,arguments)}}var e=t.prototype;e.getListeners=function(n){var r,e,t=this._getEvents();if("object"==typeof n){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.flattenListeners=function(t){var e,n=[];for(e=0;e