summaryrefslogtreecommitdiff
path: root/lib/events.js
AgeCommit message (Collapse)Author
2017-07-17lib: update indentation of ternariesRich Trott
In preparation for stricter indentation linting and to increase code clarity, update indentation for ternaries in lib. PR-URL: https://github.com/nodejs/node/pull/14247 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
2017-06-06lib: "iff" changed to "if and only if"Jacob Jones
PR-URL: https://github.com/nodejs/node/pull/13496 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-06-05events: fix potential permanent deoptBrian White
PR-URL: https://github.com/nodejs/node/pull/13384 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-04-21events: remove unreachable codecjihrig
Commit 8d386ed7e1301c869bbc266ce73650b280c9ae26 stopped the Event Emitter implementation from storing arrays containing a single listener. This change left a section of code in removeListener() as unreachable. This commit removes the unreachable code. Refs: https://github.com/nodejs/node/pull/12043 PR-URL: https://github.com/nodejs/node/pull/12501 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2017-04-04events: do not keep arrays with a single listenerLuigi Pinca
Use the remaining listener directly if the array of listeners has only one element after running `EventEmitter.prototype.removeListener()`. Advantages: - Better memory usage and better performance if no new listeners are added for the same event. Disadvantages: - A new array must be created if new listeners are added for the same event. PR-URL: https://github.com/nodejs/node/pull/12043 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl>
2017-03-24events: update and clarify error messageChris Burkhart
Update error message that's thrown when no error listeners are attached to an emitter. PR-URL: https://github.com/nodejs/node/pull/10387 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-03-24lib: use Object.create(null) directlyTimothy Gu
After V8 5.6, using Object.create(null) directly is now faster than using a constructor for map-like objects. PR-URL: https://github.com/nodejs/node/pull/11930 Refs: https://github.com/emberjs/ember.js/issues/15001 Refs: https://crrev.com/532c16eca071df3ec8eed394dcebb932ef584ee6 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2017-03-22events, doc: check input in defaultMaxListenersDavidCai
PR-URL: https://github.com/nodejs/node/pull/11938 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Brian White <mscdex@mscdex.net>
2017-03-10meta: restore original copyright headerJames M Snell
A prior io.js era commit inappropriately removed the original copyright statements from the source. This restores those in any files still remaining from that edit. Ref: https://github.com/nodejs/TSC/issues/174 Ref: https://github.com/nodejs/node/pull/10599 PR-URL: https://github.com/nodejs/node/pull/10155 Note: This PR was required, reviewed-by and approved by the Node.js Foundation Legal Committee and the TSC. There is no `Approved-By:` meta data.
2017-02-22domain,events: support non-object 'error' argumentBen Noordhuis
Fix a TypeError when emitting an 'error' argument with a non-object argument (like a string) when domains are active. Fixes: https://github.com/nodejs/help/issues/501 PR-URL: https://github.com/nodejs/node/pull/11438 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2017-01-05events: avoid emit() eager deoptVictor Felder
This commit makes sure EventEmitter.emit() doesn't get deoptimized by V8. The deopt happens when accessing out of bound indexes of the `arguments` object. This issue has been raised here: #10323 and this specific case might become a more serious performance issue in upcoming V8 releases. PR-URL: https://github.com/nodejs/node/pull/10568 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-01-05events: improve removeListener() performanceBrian White
array.shift() seems to be faster than arrayClone() when the item to remove is at the front (at least with V8 5.4). PR-URL: https://github.com/nodejs/node/pull/10572 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-01-04events: optimize arrayClone by copying forwardBenedikt Meurer
Optimize arrayClone by copying forward. It's slightly faster (and more readable) to copy array elements in forward direction. This way it also avoids the ToBoolean and the postfix count operation. PR-URL: https://github.com/nodejs/node/pull/10571 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
2016-12-28events: improve once() performanceBrian White
This commit takes advantage of the performance improvements V8 has made to function.bind() in V8 5.4 and uses it to avoid constant recompilation/reoptimization of the wrapper closure used in once(). This change results in ~27% performance increase for once(). PR-URL: https://github.com/nodejs/node/pull/10445 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2016-10-31events: remove unnecessary checkscjihrig
This commit removes two truthy checks for object properties that are immediately followed by a strict equality check. The other item in the comparison is guaranteed to be a function by this point in the code, so the truthy check is redundant. PR-URL: https://github.com/nodejs/node/pull/9330 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
2016-10-26events,test: fix TypeError in EventEmitter warningjseagull
Allows Symbol to be converted to String so it can be included in the error. Fixes: https://github.com/nodejs/node/issues/9003 PR-URL: https://github.com/nodejs/node/pull/9021 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
2016-09-04events: make memory leak warning name more verboseAnna Henningsen
Switch from a generic `Warning` to the more specific `MaxListenersExceededWarning`. Ref: https://github.com/nodejs/node/pull/8298 PR-URL: https://github.com/nodejs/node/pull/8341 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
2016-08-30events: make memory leak warning more accessibleAnna Henningsen
This makes the famous `EventEmitter memory leak` warnings occurring when the listener count for a given event exceeds a specified number more programatically accessible, by giving them properties referring to the event emitter instance and the event itself. This can be useful for debugging the origins of such a warning when the stack itself doesn’t reveal enough information about the event emitter instance itself, e.g. when manual inspection of the already-registered listeners is expected to be useful. PR-URL: https://github.com/nodejs/node/pull/8298 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <Fishrock123@rocketmail.com>
2016-08-09events: unwrap #once listeners in #listenersOwen Smith
Fixes: https://github.com/nodejs/node/issues/6873 PR-URL: https://github.com/nodejs/node/pull/6881 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
2016-04-29events: pass the original listener added by onceDavidCai
When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: https://github.com/nodejs/node/pull/6394 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2016-04-22events: add prependListener() and prependOnceListener()James M Snell
A handful of modules (including readable-streams) make inappropriate use of the internal _events property. One such use is to prepend an event listener to the front of the array of listeners. This adds EE.prototype.prependListener() and EE.prototype.prependOnceListener() methods to add handlers to the *front* of the listener array. Doc update and test case is included. Fixes: https://github.com/nodejs/node/issues/1817 PR-URL: https://github.com/nodejs/node/pull/6032 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
2016-04-21events: make eventNames() use Reflect.ownKeys()Luigi Pinca
Use `Reflect.ownKeys()` instead of `Object.keys()` and `Object.getOwnPropertySymbols()`. PR-URL: https://github.com/nodejs/node/pull/5822 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2016-04-18events: don't inherit from Object.prototypeBrian White
This commit safely allows event names that are named the same as properties that are ordinarily inherited from Object.prototype such as __proto__. Fixes: https://github.com/nodejs/node/issues/728 PR-URL: https://github.com/nodejs/node/pull/6092 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2016-03-24process: add 'warning' event and process.emitWarning()James M Snell
In several places throughout the code we write directly to stderr to report warnings (deprecation, possible eventemitter memory leak). The current design of simply dumping the text to stderr is less than ideal. This PR introduces a new "process warnings" mechanism that emits 'warning' events on the global process object. These are invoked with a `warning` argument whose value is an Error object. By default, these warnings will be printed to stderr. This can be suppressed using the `--no-warnings` and `--no-deprecation` command line flags. For warnings, the 'warning' event will still be emitted by the process, allowing applications to handle the warnings in custom ways. The existing `--no-deprecation` flag will continue to supress all deprecation output generated by the core lib. The `--trace-warnings` command line flag will tell Node.js to print the full stack trace of warnings as part of the default handling. The existing `--no-deprecation`, `--throw-deprecation` and `--trace-deprecation` flags continue to work as they currently do, but the exact output of the warning message is modified to occur on process.nextTick(). The stack trace for the warnings and deprecations preserve and point to the correct call site. A new `process.emitWarning()` API is provided to permit userland to emit warnings and deprecations using the same consistent mechanism. Test cases and documentation are included. PR-URL: https://github.com/nodejs/node/pull/4782 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2016-03-15events: add eventNames() methodJames M Snell
Per https://github.com/nodejs/node/issues/1817, there are many modules that currently abuse the private `_events` property on EventEmitter. One of the ways it is used is to determine if a particular event is being listened for. This adds a simple `eventNames()` method that returns an array of the events with currently registered listeners. PR-URL: https://github.com/nodejs/node/pull/5617 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
2016-01-16lib,test: remove extra semicolonsMichaël Zasso
PR-URL: https://github.com/nodejs/node/pull/2205 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
2016-01-14events: make sure console functions existDave
If there's no global console cached, initialize it. Fixes: https://github.com/nodejs/node/issues/4467 PR-URL: https://github.com/nodejs/node/pull/4479 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com>
2015-12-03lib,src: ensure '(node:pid)' prefix for stdout loggingMinwoo Jung
Add '(node:pid)' prefix message for stdout logging PR-URL: https://github.com/nodejs/node/pull/3833 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
2015-11-09lib: Consistent error messages in all modulesmicnic
This commit fixes some error messages that are not consistent with some general rules which most of the error messages follow. PR-URL: https://github.com/nodejs/node/pull/3374 Reviewed-By: Roman Reiss <me@silverwind.io>
2015-09-06events,lib: don't require EE#listenerCount()Jeremiah Senkpiel
Now parts of our public and public-ish APIs fall back to old-style listenerCount() if the emitter does not have a listenerCount function. Fixes: https://github.com/nodejs/node/issues/2655 Refs: 8f58fb92fff904a6ca58fd0df9ee5a1816e5b84e PR-URL: https://github.com/nodejs/node/pull/2661 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
2015-08-20events: deprecate static listenerCount functionSakthipriyan Vairamani
As per the discussion in #734, this patch deprecates the usage of `EventEmitter.listenerCount` static function in the docs, and introduces the `listenerCount` function in the prototype of `EventEmitter` itself. PR-URL: https://github.com/nodejs/node/pull/2349 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
2015-05-12events: provide better error message for unhandled errorEvan Lucas
Previously, in the event of an unhandled error event, if the error is a not an actual Error, then a default error is thrown. Now, the argument is appended to the error message and added as the `context` property of the error. PR-URL: https://github.com/iojs/io.js/pull/1654 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2015-05-09lib: fix eslint stylesYosuke Furukawa
PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-03-24lib: add missing `new` for errors lib/*.jsMayhem
Not including `new` adds a useless frame and removes a potentially useful frame. PR-URL: https://github.com/iojs/io.js/pull/1246 Reviewed-By: Petka Antonov <petka_antonov@hotmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
2015-03-04events: optimize listener array cloningBrian White
This both switches to a single algorithm for array cloning and also speeds up (by ~100% in the ee-listeners-many benchmark) the "many elements" case that was previously handled by `array.slice()`. PR-URL: https://github.com/iojs/io.js/pull/1050 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Julian Duque <julianduquej@gmail.com>
2015-02-11events: optimize adding and removing of listenersBrian White
These optimizations result in >2x speedup in the ee-add-remove benchmark: * Don't mutate array.length when removing the last listener for an event * Don't bother checking max listeners if listeners isn't an array * Don't call delete when removing the last event in _events, just re-assign a new object instead PR-URL: https://github.com/iojs/io.js/pull/785 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com>
2015-02-11events: move slow path to separate function tooBrian White
This keeps in line with how things are done for the fast path and *might* even provide a *slight* performance increase. PR-URL: https://github.com/iojs/io.js/pull/785 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com>
2015-02-09events: optimize various functionsBrian White
Cache events and listeners objects where possible and loop over Object.keys() instead of using for..in. These changes alone give ~60-65% improvement in the ee-add-remove benchmark. The changes to EventEmitter.listenerCount() gives ~14% improvement and changes to emitter.listeners() gives significant improvements for <50 listeners (~195% improvement for 10 listeners). The changes to emitter.emit() gives 3x speedup for the fast cases with multiple handlers and a minor speedup for the slow case with multiple handlers. The swapping out of the util.is* type checking functions with inline checks gives another ~5-10% improvement. PR-URL: https://github.com/iojs/io.js/pull/601 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com>
2015-01-31lib: reduce util.is*() usagecjihrig
Many of the util.is*() methods used to check data types simply compare against a single value or the result of typeof. This commit replaces calls to these methods with equivalent checks. This commit does not touch calls to the more complex methods (isRegExp(), isDate(), etc.). Fixes: https://github.com/iojs/io.js/issues/607 PR-URL: https://github.com/iojs/io.js/pull/647 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-21lib: use const to define constantscjihrig
This commit replaces a number of var statements throughout the lib code with const statements. PR-URL: https://github.com/iojs/io.js/pull/541 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-20lib: fix TypeError with EventEmitter#on() abuseBen Noordhuis
Commit 2931348 added EventEmitter#getMaxListeners() but introduced a regression when people abuse EventEmitter.prototype.on.call() to call EventEmitter#on() on a non-EE object. Add a workaround for that. Fixes: https://github.com/iojs/io.js/issues/523 PR-URL: https://github.com/iojs/io.js/pull/527 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2015-01-15src: fix jslint errorsYosuke Furukawa
PR-URL: https://github.com/iojs/io.js/pull/449 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-12Remove excessive copyright/license boilerplateisaacs
The copyright and license notice is already in the LICENSE file. There is no justifiable reason to also require that it be included in every file, since the individual files are not individually distributed except as part of the entire package.
2014-12-20lib: micro-optimize EventEmitter#removeListener()Ben Noordhuis
Replace the call to Array#splice() with a faster open-coded version that creates less garbage. Add a new benchmark to prove it. With the change applied, it scores a whopping 40% higher. PR-URL: https://github.com/iojs/io.js/pull/185 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2014-12-05events: implement EventEmitter#getMaxListeners()Christian Tellnes
Fixes https://github.com/joyent/node/issues/8237. PR-URL: https://github.com/iojs/io.js/pull/82 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2014-11-22lib: turn on strict modeBen Noordhuis
Turn on strict mode for the files in the lib/ directory. It helps catch bugs and can have a positive effect on performance. PR-URL: https://github.com/node-forward/node/pull/64 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Fedor Indutny <fedor@indutny.com>
2014-07-01events: Output the event that is leakingArnout Kazemier
Signed-off-by: Fedor Indutny <fedor@indutny.com>
2014-06-05Merge branch 'v0.10'Fedor Indutny
Conflicts: ChangeLog lib/events.js lib/tls.js src/node_constants.cc src/node_crypto.cc src/node_crypto.h src/node_version.h
2014-05-07lib: name EventEmitter prototype methodsBen Noordhuis
Before this commit the EventEmitter methods were anonymous functions. V8 tries to infer names for anonymous functions based on the execution context but it frequently gets it wrong and when that happens, the stack trace is usually confusing and unhelpful. This commit names all methods so V8 can fall back to the method.name property. The above gotcha applies to all anonymous functions but is exacerbated for EventEmitter methods because those are invoked with a plenitude of different receivers. Signed-off-by: Trevor Norris <trev.norris@gmail.com>
2014-04-15events: check if _events is an own propertyVladimir Kurchatkin
Without this check it is possible to have the _events object shared amongst instances. Fixes #7157 Signed-off-by: Trevor Norris <trev.norris@gmail.com>