-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a copy of
listeners
for hijacking events.
Node.js v0.7+ `event.listeners()` returns a reference that continues to be used internally after calling `removeAllListeners()`. Make a copy before calling the original functions to prevent recursion on v0.7+. Since this pattern is common in Yeti, move this event hijacking into its own module.
- Loading branch information
Showing
3 changed files
with
71 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use strict"; | ||
|
||
/** | ||
* @module hijack | ||
*/ | ||
|
||
/** | ||
* Attach the provided function as the first | ||
* listener of the given EventEmitter event. | ||
* | ||
* All listeners of the event will be removed | ||
* and replaced with a listener that will run | ||
* the provided function first, followed by | ||
* the original listeners if the function | ||
* returned false. | ||
* | ||
* Works with Node.js v0.7+ where the array | ||
* returned by `ee.listeners()` is a reference. | ||
* | ||
* @method hijack | ||
* @param {EventEmitter} ee Emitter. | ||
* @param {String} event Event name. | ||
* @param {Function} firstFn Function to run first. | ||
*/ | ||
module.exports = function hijack(ee, event, firstFn) { | ||
var listeners = ee.listeners(event), | ||
i = 0, | ||
length = listeners.length, | ||
originalListeners = []; | ||
|
||
// Note: listeners is a reference in Node v0.7. | ||
// Calling `removeAllListeners` no longer destroys | ||
// the listener array, which causes it survive | ||
// as a reference. See joyent/node commits: | ||
// - 78dc13fbf97e2e3003e6f3baacdd5ff60e8de3f7 | ||
// - 928ea564d16da47e615ddac627e0b4d4a40d8196 | ||
// | ||
// Make a copy first. | ||
for (; i < length; i += 1) { | ||
originalListeners[i] = listeners[i]; | ||
} | ||
|
||
ee.removeAllListeners(event); | ||
|
||
ee.on(event, function () { | ||
var args = Array.prototype.slice.call(arguments), | ||
stack = [firstFn].concat(originalListeners), | ||
handled; | ||
|
||
handled = firstFn.apply(ee, args); | ||
|
||
if (!handled) { | ||
originalListeners.forEach(function (fn) { | ||
fn.apply(ee, args); | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters