diff --git a/.travis.yml b/.travis.yml index a4596ab..e64616c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ -node_js: -- "0.8" -- "0.10" -- "0.12" -- "iojs" language: node_js +node_js: +- "6.14" +- "7.10" +- "8.12" \ No newline at end of file diff --git a/History.md b/History.md index 2a49b63..e9fb4bc 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,11 @@ +1.3.0 / 2018-04-15 +================== + + * removed bower support + * expose emitter on `exports` + * prevent de-optimization from using `arguments` + 1.2.1 / 2016-04-18 ================== diff --git a/bower.json b/bower.json deleted file mode 100644 index 87bb065..0000000 --- a/bower.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "emitter", - "description": "Event emitter", - "keywords": [ - "emitter", - "events" - ], - "version": "1.2.1", - "license": "MIT", - "main": "index.js", - "homepage": "https://github.com/component/emitter", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "Makefile", - "package.json", - "component.json" - ] -} diff --git a/component.json b/component.json index 009d846..b9b6a7b 100644 --- a/component.json +++ b/component.json @@ -6,7 +6,7 @@ "emitter", "events" ], - "version": "1.2.0", + "version": "1.3.0", "scripts": [ "index.js" ], diff --git a/index.js b/index.js index fa4faee..6d7ed0a 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,13 @@ Emitter.prototype.removeEventListener = function(event, fn){ break; } } + + // Remove event specific arrays for event types that no + // one is subscribed for to avoid memory leak. + if (callbacks.length === 0) { + delete this._callbacks['$' + event]; + } + return this; }; @@ -124,9 +131,14 @@ Emitter.prototype.removeEventListener = function(event, fn){ Emitter.prototype.emit = function(event){ this._callbacks = this._callbacks || {}; - var args = [].slice.call(arguments, 1) + + var args = new Array(arguments.length - 1) , callbacks = this._callbacks['$' + event]; + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + if (callbacks) { callbacks = callbacks.slice(0); for (var i = 0, len = callbacks.length; i < len; ++i) { diff --git a/package.json b/package.json index ae5f7ad..403b8c0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "component-emitter", "description": "Event emitter", - "version": "1.2.1", + "version": "1.3.0", "license": "MIT", "devDependencies": { "mocha": "*", @@ -20,5 +20,8 @@ "scripts": { "test": "make test" }, - "files": ["index.js", "LICENSE"] + "files": [ + "index.js", + "LICENSE" + ] } diff --git a/test/emitter.js b/test/emitter.js index 02c3a0b..a1f1dda 100644 --- a/test/emitter.js +++ b/test/emitter.js @@ -142,6 +142,32 @@ describe('Emitter', function(){ calls.should.eql([]); }) + + it('should remove event array to avoid memory leak', function() { + var emitter = new Emitter; + var calls = []; + + function cb() {} + + emitter.on('foo', cb); + emitter.off('foo', cb); + + emitter._callbacks.should.not.have.property('$foo'); + }) + + it('should only remove the event array when the last subscriber unsubscribes', function() { + var emitter = new Emitter; + var calls = []; + + function cb1() {} + function cb2() {} + + emitter.on('foo', cb1); + emitter.on('foo', cb2); + emitter.off('foo', cb1); + + emitter._callbacks.should.have.property('$foo'); + }) }) describe('.off()', function(){