-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
events: remove reaches into _events internals #17440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -574,6 +574,39 @@ to indicate an unlimited number of listeners. | |
|
|
||
| Returns a reference to the `EventEmitter`, so that calls can be chained. | ||
|
|
||
| ### emitter.rawListeners(eventName) | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| - `eventName` {any} | ||
|
|
||
| Returns a copy of the array of listeners for the event named `eventName`, | ||
| including any wrappers (such as those created by `.once`). | ||
|
||
|
|
||
| ```js | ||
| const emitter = new EventEmitter(); | ||
| emitter.once('log', () => console.log('log once')); | ||
|
|
||
| // Returns a new Array with a function `onceWrapper` which has a property | ||
| // `listener` which contains the original listener bound above | ||
| const listeners = emitter.rawListeners('log'); | ||
| const logFnWrapper = listeners[0]; | ||
|
||
|
|
||
| // logs "log once" to the console and does not unbind the `once` event | ||
| logFnWrapper.listener(); | ||
|
|
||
| // logs "log once" to the console and removes the listener | ||
| logFnWrapper(); | ||
|
|
||
| emitter.on('log', () => console.log('log persistently')); | ||
| // will return a new Array with a single function bound by `on` above | ||
| const newListeners = emitter.rawListeners('log'); | ||
|
|
||
| // logs "log persistently" twice | ||
| newListeners[0](); | ||
| emitter.emit('log'); | ||
| ``` | ||
|
|
||
| [`--trace-warnings`]: cli.html#cli_trace_warnings | ||
| [`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners | ||
| [`domain`]: domain.html | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ module.exports = EventEmitter; | |
| EventEmitter.EventEmitter = EventEmitter; | ||
|
|
||
| EventEmitter.prototype._events = undefined; | ||
| EventEmitter.prototype._eventsCount = 0; | ||
| EventEmitter.prototype._maxListeners = undefined; | ||
|
|
||
| // By default EventEmitters will print a warning if more than 10 listeners are | ||
|
|
@@ -357,8 +358,8 @@ EventEmitter.prototype.removeAllListeners = | |
| return this; | ||
| }; | ||
|
|
||
| EventEmitter.prototype.listeners = function listeners(type) { | ||
| const events = this._events; | ||
| function _listeners(target, type, unwrap) { | ||
| const events = target._events; | ||
|
|
||
| if (events === undefined) | ||
| return []; | ||
|
|
@@ -368,9 +369,18 @@ EventEmitter.prototype.listeners = function listeners(type) { | |
| return []; | ||
|
|
||
| if (typeof evlistener === 'function') | ||
| return [evlistener.listener || evlistener]; | ||
| return unwrap ? [evlistener.listener || evlistener] : [evlistener]; | ||
|
|
||
| return unwrap ? | ||
| unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); | ||
| } | ||
|
|
||
| EventEmitter.prototype.listeners = function listeners(type) { | ||
| return _listeners(this, type, true); | ||
| }; | ||
|
|
||
| return unwrapListeners(evlistener); | ||
| EventEmitter.prototype.rawListeners = function rawListeners(type) { | ||
|
||
| return _listeners(this, type, false); | ||
| }; | ||
|
|
||
| EventEmitter.listenerCount = function(emitter, type) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3354,12 +3354,6 @@ void SetupProcessObject(Environment* env, | |
| env->SetMethod(process, "_setupNextTick", SetupNextTick); | ||
| env->SetMethod(process, "_setupPromises", SetupPromises); | ||
| env->SetMethod(process, "_setupDomainUse", SetupDomainUse); | ||
|
|
||
| // pre-set _events object for faster emit checks | ||
| Local<Object> events_obj = Object::New(env->isolate()); | ||
| CHECK(events_obj->SetPrototype(env->context(), | ||
| Null(env->isolate())).FromJust()); | ||
| process->Set(env->events_string(), events_obj); | ||
|
||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, shouldn't this be
{string|symbol}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It matches the rest of the docs and it is at least partially correct. We don't validate the input so if they pass in a number, it'll get automatically converted to a string.
Either way, if we're changing this then it should be changed in all of the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've filed #17657 to track that. This again LGTM.