Comparing version 1.0.1 to 1.0.2
12
index.js
@@ -83,5 +83,10 @@ var EventEmitter = require('events').EventEmitter; | ||
self.EventHook = function(obj){ | ||
var ret = self.EventShim(obj); | ||
var ret; | ||
if(self.IsHooked(obj)) | ||
ret = self.GetShim(obj); | ||
else | ||
ret = self.EventShim(obj); | ||
Object.keys(EventEmitter.prototype).forEach(function(funcName){ | ||
if(funcName.toLowerCase() == "emit") //Don't hook emit otherwise what the hell are we doing here... | ||
//Don't hook emit otherwise what the hell are we doing here... | ||
if(funcName.toLowerCase() == "emit" || typeof(obj[funcName]) != "function" || obj[funcName].__evtHookMarker) | ||
return; | ||
@@ -95,3 +100,4 @@ obj[funcName] = function(){ | ||
return ret[funcName].apply(ret, arguments); | ||
} | ||
}; | ||
obj[funcName].__evtHookMarker = true; | ||
}); | ||
@@ -98,0 +104,0 @@ obj.__eventHookShim = ret; |
{ | ||
"name": "event-hook", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Allows installation of intermediate procesing functions between an event being emitted on an object and it's listeners receiving the events.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -210,1 +210,38 @@ var evtTools = require('../'); | ||
}; | ||
exports.testHookAsync = function(test){ | ||
test.expect(2); | ||
var testObj = new events.EventEmitter(); | ||
var shim = evtTools.EventHook(testObj); | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
cb(magicNo + 10); | ||
}); | ||
testObj.on("testEvent2", function(magicNo){ | ||
test.equal(magicNo, 52, "Incorrect argument was propagated"); | ||
test.equal(this, testObj, "Incorrect 'this' context in hook mode") | ||
}); | ||
process.nextTick(function(){ | ||
testObj.emit("testEvent2",42); | ||
test.done(); | ||
}); | ||
}; | ||
exports.testHookReapply = function(test){ | ||
test.expect(2); | ||
var testObj = new events.EventEmitter(); | ||
var origOn = testObj.on; | ||
var shim = evtTools.EventHook(testObj); | ||
testObj.on = origOn; | ||
evtTools.EventHook(testObj); | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
cb(magicNo + 10); | ||
}); | ||
testObj.on("testEvent2", function(magicNo){ | ||
test.equal(magicNo, 52, "Incorrect argument was propagated"); | ||
test.equal(this, testObj, "Incorrect 'this' context in hook mode") | ||
}); | ||
testObj.emit("testEvent2",42); | ||
test.done(); | ||
}; |
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
16641
321