events
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -69,2 +69,8 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
function checkListener(listener) { | ||
if (typeof listener !== 'function') { | ||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); | ||
} | ||
} | ||
Object.defineProperty(EventEmitter, 'defaultMaxListeners', { | ||
@@ -104,3 +110,3 @@ enumerable: true, | ||
function $getMaxListeners(that) { | ||
function _getMaxListeners(that) { | ||
if (that._maxListeners === undefined) | ||
@@ -112,3 +118,3 @@ return EventEmitter.defaultMaxListeners; | ||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() { | ||
return $getMaxListeners(this); | ||
return _getMaxListeners(this); | ||
}; | ||
@@ -165,5 +171,3 @@ | ||
if (typeof listener !== 'function') { | ||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); | ||
} | ||
checkListener(listener); | ||
@@ -205,3 +209,3 @@ events = target._events; | ||
// Check for listener leak | ||
m = $getMaxListeners(target); | ||
m = _getMaxListeners(target); | ||
if (m > 0 && existing.length > m && !existing.warned) { | ||
@@ -238,8 +242,8 @@ existing.warned = true; | ||
function onceWrapper() { | ||
var args = []; | ||
for (var i = 0; i < arguments.length; i++) args.push(arguments[i]); | ||
if (!this.fired) { | ||
this.target.removeListener(this.type, this.wrapFn); | ||
this.fired = true; | ||
ReflectApply(this.listener, this.target, args); | ||
if (arguments.length === 0) | ||
return this.listener.call(this.target); | ||
return this.listener.apply(this.target, arguments); | ||
} | ||
@@ -257,5 +261,3 @@ } | ||
EventEmitter.prototype.once = function once(type, listener) { | ||
if (typeof listener !== 'function') { | ||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); | ||
} | ||
checkListener(listener); | ||
this.on(type, _onceWrap(this, type, listener)); | ||
@@ -267,5 +269,3 @@ return this; | ||
function prependOnceListener(type, listener) { | ||
if (typeof listener !== 'function') { | ||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); | ||
} | ||
checkListener(listener); | ||
this.prependListener(type, _onceWrap(this, type, listener)); | ||
@@ -280,5 +280,3 @@ return this; | ||
if (typeof listener !== 'function') { | ||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); | ||
} | ||
checkListener(listener); | ||
@@ -285,0 +283,0 @@ events = this._events; |
@@ -0,1 +1,20 @@ | ||
# 3.1.0 (2020-01-08) | ||
`events` now matches the Node.js 11.12.0 API. | ||
- pass through return value in wrapped `emitter.once()` listeners | ||
Now, this works: | ||
```js | ||
emitter.once('myevent', function () { return 1; }); | ||
var listener = emitter.rawListeners('myevent')[0] | ||
assert(listener() === 1); | ||
``` | ||
Previously, `listener()` would return undefined regardless of the implementation. | ||
Ported from https://github.com/nodejs/node/commit/acc506c2d2771dab8d7bba6d3452bc5180dff7cf | ||
- Reduce code duplication in listener type check ([#67](https://github.com/Gozala/events/pull/67) by [@friederbluemle](https://github.com/friederbluemle)). | ||
- Improve `emitter.once()` performance in some engines | ||
# 3.0.0 (2018-05-25) | ||
@@ -2,0 +21,0 @@ |
{ | ||
"name": "events", | ||
"id": "events", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Node's event emitter for all engines.", | ||
@@ -26,3 +26,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"airtap": "0.0.6", | ||
"airtap": "^1.0.0", | ||
"isarray": "^2.0.2", | ||
@@ -29,0 +29,0 @@ "tape": "^4.8.0" |
@@ -7,3 +7,3 @@ # events [![Build Status](https://travis-ci.org/Gozala/events.png?branch=master)](https://travis-ci.org/Gozala/events) | ||
> `events` currently matches the **Node.js 10.1** API. | ||
> `events` currently matches the **Node.js 11.12.0** API. | ||
@@ -38,3 +38,3 @@ Note that the `events` module uses ES5 features. If you need to support very old browsers like IE8, use a shim like [`es5-shim`](https://www.npmjs.com/package/es5-shim). You need both the shim and the sham versions of `es5-shim`. | ||
See the [Node.js EventEmitter docs](http://nodejs.org/api/events.html). `events` currently matches the Node.js 10.1 API. | ||
See the [Node.js EventEmitter docs](http://nodejs.org/api/events.html). `events` currently matches the Node.js 11.12.0 API. | ||
@@ -41,0 +41,0 @@ ## Contributing |
@@ -27,4 +27,11 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
var listener = function listener() {}; | ||
var listener2 = function listener2() {}; | ||
function listener() {} | ||
function listener2() {} | ||
function listener3() { | ||
return 0; | ||
} | ||
function listener4() { | ||
return 1; | ||
} | ||
function TestStream() {} | ||
@@ -151,1 +158,13 @@ util.inherits(TestStream, events.EventEmitter); | ||
} | ||
{ | ||
var ee = new events.EventEmitter(); | ||
ee.once('foo', listener3); | ||
ee.on('foo', listener4); | ||
var rawListeners = ee.rawListeners('foo'); | ||
assert.strictEqual(rawListeners.length, 2); | ||
assert.strictEqual(rawListeners[0](), 0); | ||
var rawListener = ee.rawListeners('foo'); | ||
assert.strictEqual(rawListener.length, 1); | ||
assert.strictEqual(rawListener[0](), 1); | ||
} |
Sorry, the diff of this file is not supported yet
73681
31
1690