shimmer
Advanced tools
Comparing version 0.8.1 to 0.9.0
26
index.js
@@ -127,2 +127,18 @@ 'use strict'; | ||
function wrapListeners(unwrapped) { | ||
if (!unwrapped) return; | ||
var wrapped = unwrapped; | ||
if (typeof unwrapped === 'function') { | ||
wrapped = prepare(unwrapped); | ||
} | ||
else if (Array.isArray(unwrapped)) { | ||
wrapped = []; | ||
for (var i = 0; i < unwrapped.length; i++) { | ||
wrapped[i] = prepare(unwrapped[i]); | ||
} | ||
} | ||
return wrapped; | ||
} | ||
/* Ensure that if removeListener gets called, it's working with the | ||
@@ -139,3 +155,3 @@ * unwrapped listeners. | ||
unwrapped = this._events[event]; | ||
this._events[event] = prepare(unwrapped); | ||
this._events[event] = wrapListeners(unwrapped); | ||
} | ||
@@ -151,3 +167,3 @@ }; | ||
*/ | ||
this._events[event] = prepare(unwrapped); | ||
this._events[event] = wrapListeners(unwrapped); | ||
return emit.apply(this, arguments); | ||
@@ -170,5 +186,5 @@ } | ||
emitter.__unwrap = function () { | ||
if (emitter.addListener.__unwrap) emitter.addListener.__unwrap(); | ||
if (emitter.on.__unwrap) emitter.on.__unwrap(); | ||
if (emitter.emit.__unwrap) emitter.emit.__unwrap(); | ||
unwrap(emitter, 'addListener'); | ||
unwrap(emitter, 'on'); | ||
unwrap(emitter, 'emit'); | ||
delete emitter.__wrapped; | ||
@@ -175,0 +191,0 @@ }; |
{ | ||
"name": "shimmer", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Safe(r) monkeypatching for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -75,8 +75,12 @@ ## Safer monkeypatching for Node.js | ||
Change how an individual EventEmitter runs its listeners. Each listener | ||
will be passed to `mark`, and `wrapEmitter` assumes that whatever changes are | ||
made happen either on the listener itself or somewhere else. When emit is | ||
called, all of the listeners for the event type will be passed to `prepare`, | ||
which should return the list of prepared listeners. The wrapped EE can be | ||
restored to its pristine state by using emitter.__unwrap(), but this should | ||
only be used if you *really* know what you're doing. | ||
Wrap an EventEmitter's event listeners. Each listener will be passed to | ||
`mark` when it is registered with `.addListener()` or `.on()`, and then | ||
each listener is passed to `prepare` to be wrapped before it's called | ||
by the `.emit()` call. `wrapListener` deals with the single listener | ||
vs array of listeners logic, and also ensures that edge cases like | ||
`.removeListener()` being called from within an `.emit()` for the same | ||
event type is handled properly. | ||
The wrapped EE can be restored to its pristine state by using | ||
emitter.__unwrap(), but this should only be used if you *really* know | ||
what you're doing. |
@@ -74,3 +74,3 @@ 'use strict'; | ||
t.test("when a listener removes another listener", function (t) { | ||
t.plan(6); | ||
t.plan(4); | ||
@@ -92,14 +92,4 @@ shimmer({logger : function (message) { | ||
} | ||
function prepare(unwrapped) { | ||
t.ok(Array.isArray(unwrapped), "have a list of handlers"); | ||
var replacements = []; | ||
shimmer.wrapEmitter(ee, nop, wrap); | ||
for (var i = 0; i < unwrapped.length; i++) { | ||
replacements[i] = wrap(unwrapped[i]); | ||
} | ||
return replacements; | ||
} | ||
shimmer.wrapEmitter(ee, nop, prepare); | ||
ee.on('listen', listener1); | ||
@@ -106,0 +96,0 @@ ee.on('listen', listener2); |
26197
646
86