Comparing version 0.9.1 to 0.9.2
56
index.js
@@ -87,7 +87,4 @@ 'use strict'; | ||
function wrapEmitter(emitter, onAddListener, onEmit) { | ||
if (!emitter || | ||
!emitter.on || | ||
!emitter.addListener || | ||
!emitter.removeListener || | ||
!emitter.emit) { | ||
if (!emitter || !emitter.on || !emitter.addListener || | ||
!emitter.removeListener || !emitter.emit) { | ||
logger("can only wrap real EEs"); | ||
@@ -107,2 +104,5 @@ return; | ||
// dunderscores are boring | ||
var SYMBOL = 'wrap@before'; | ||
/* Attach a context to a listener, and make sure that this hook stays | ||
@@ -114,3 +114,12 @@ * attached to the emitter forevermore. | ||
// set up the listener so that onEmit can do whatever it needs | ||
onAddListener(listener); | ||
var before = this[SYMBOL]; | ||
if (typeof before === 'function') { | ||
before(listener); | ||
} | ||
else if (Array.isArray(before)) { | ||
var length = before.length; | ||
for (var i = 0; i < length; i++) { | ||
before[i](listener); | ||
} | ||
} | ||
@@ -185,13 +194,28 @@ try { | ||
wrap(emitter, 'addListener', adding); | ||
wrap(emitter, 'on', adding); | ||
wrap(emitter, 'emit', emitting); | ||
// support multiple onAddListeners | ||
if (!emitter[SYMBOL]) { | ||
emitter[SYMBOL] = onAddListener; | ||
} | ||
else if (typeof emitter[SYMBOL] === 'function') { | ||
emitter[SYMBOL] = [emitter[SYMBOL], onAddListener]; | ||
} | ||
else if (Array.isArray(emitter[SYMBOL])) { | ||
emitter[SYMBOL].push(onAddListener); | ||
} | ||
emitter.__unwrap = function () { | ||
unwrap(emitter, 'addListener'); | ||
unwrap(emitter, 'on'); | ||
unwrap(emitter, 'emit'); | ||
delete emitter.__wrapped; | ||
}; | ||
emitter.__wrapped = true; | ||
// only wrap the core functions once | ||
if (!emitter.__wrapped) { | ||
wrap(emitter, 'addListener', adding); | ||
wrap(emitter, 'on', adding); | ||
wrap(emitter, 'emit', emitting); | ||
emitter.__unwrap = function () { | ||
unwrap(emitter, 'addListener'); | ||
unwrap(emitter, 'on'); | ||
unwrap(emitter, 'emit'); | ||
delete emitter[SYMBOL]; | ||
delete emitter.__wrapped; | ||
}; | ||
emitter.__wrapped = true; | ||
} | ||
} | ||
@@ -198,0 +222,0 @@ |
{ | ||
"name": "shimmer", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "Safe(r) monkeypatching for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ 'use strict'; | ||
test("bindEmitter", function (t) { | ||
t.plan(7); | ||
t.plan(8); | ||
@@ -163,2 +163,37 @@ t.test("with no parameters", function (t) { | ||
}); | ||
t.test("when wrapping the same emitter multiple times", function (t) { | ||
t.plan(6); | ||
shimmer({logger : function (message) { | ||
t.fail(message); | ||
t.end(); | ||
}}); | ||
var ee = new Emitter(); | ||
var values = []; | ||
shimmer.wrapEmitter( | ||
ee, | ||
function marker() { values.push(1); }, | ||
function passthrough(handler) { return handler; } | ||
); | ||
shimmer.wrapEmitter( | ||
ee, | ||
function marker() { values.push(2); }, | ||
function passthrough(handler) { return handler; } | ||
); | ||
ee.on('test', function (value) { | ||
t.equal(value, 31, "got expected value"); | ||
t.deepEqual(values, [1, 2], "both marker functions were called"); | ||
}); | ||
t.ok(ee.addListener.__wrapped, "addListener is wrapped"); | ||
t.ok(ee.on.__wrapped, "on is wrapped"); | ||
t.ok(ee.emit.__wrapped, "emit is wrapped"); | ||
t.notOk(ee.removeListener.__wrapped, "removeListener is not wrapped"); | ||
ee.emit('test', 31); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27943
700