Comparing version 0.0.1 to 0.0.2
42
index.js
@@ -26,23 +26,25 @@ var EventEmitter = require('events').EventEmitter; | ||
var executeProcessor = function(evt, arglist, pid){ | ||
var newArgList = [null]; | ||
for(var i=0;i<arglist.length;i++){ | ||
newArgList.push(arglist[i]); | ||
} | ||
if(!(evt in eventProcessors) || pid >= eventProcessors[evt].length){ | ||
newArgList[0] = evt; | ||
ret.emit.apply(ret, newArgList); | ||
}else{ | ||
var func = eventProcessors[evt][pid]; | ||
newArgList[0] = function(){ | ||
var newArgs = arguments; | ||
if(!arguments.length) | ||
newArgs = arglist; | ||
executeProcessor(evt, newArgs, pid+1); | ||
}; | ||
func.apply(func, newArgList); | ||
} | ||
}; | ||
ret.forwardEvent = function(evt){ | ||
origOn.apply(obj, [evt, function(){ | ||
var arglist = []; | ||
for(var i=0;i<arguments.length;i++){ | ||
arglist.push(arguments[i]); | ||
} | ||
var continueProcessing = true; | ||
if(evt in eventProcessors){ | ||
eventProcessors[evt].forEach(function(func){ | ||
if(!continueProcessing) | ||
return; | ||
var newArgList = func.apply(func, arglist); | ||
if(newArgList instanceof Array) | ||
arglist = newArgList; | ||
else if(newArgList === false) | ||
continueProcessing = false; | ||
}); | ||
} | ||
arglist.unshift(evt); | ||
if(continueProcessing) | ||
ret.emit.apply(ret, arglist); | ||
executeProcessor(evt, arguments, 0); | ||
}]); | ||
@@ -92,2 +94,2 @@ return ret; | ||
return ("__eventHookShim" in obj); | ||
}; | ||
}; |
{ | ||
"name": "event-hook", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,9 +70,9 @@ var evtTools = require('../'); | ||
var shim = evtTools.EventShim(testObj, ["testEvent2"]); | ||
shim.addEventProcessor("testEvent1", function(magicNo){ | ||
shim.addEventProcessor("testEvent1", function(cb, magicNo){ | ||
test.ok(false, "Incorrect processor was invoked"); | ||
return [magicNo - 10]; | ||
cb(magicNo - 10); | ||
}); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(true, "Correct processor was invoked"); | ||
return [magicNo + 10]; | ||
cb(magicNo + 10); | ||
}); | ||
@@ -92,9 +92,9 @@ shim.on("testEvent2", function(magicNo){ | ||
var shim = evtTools.EventShim(testObj, ["testEvent2"]); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(true, "Processor 1 was invoked"); | ||
return [magicNo + 10]; | ||
cb(magicNo + 10); | ||
}); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(true, "Processor 2 was invoked"); | ||
return [magicNo * 2]; | ||
cb(magicNo * 2); | ||
}); | ||
@@ -115,10 +115,10 @@ shim.on("testEvent2", function(magicNo){ | ||
var shim = evtTools.EventShim(testObj, ["testEvent2"]); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(true, "Processor 1 was invoked"); | ||
cb(); | ||
}); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.equals(magicNo, 42, "Correct argument was dispatched to second processor"); | ||
return false; | ||
}); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(false, "Event was propagated to next processor"); | ||
@@ -133,2 +133,36 @@ }); | ||
//Ensures when using a processor which emits an event multiple times that: | ||
// 1. the previous processors in the chain aren't affected - this one will fail assertion if called multiple times | ||
// 2. that multiple emissions work, even when async (two sync emissions from second processor with async then tested via setTimeout) | ||
// 3. that subsequent processors are called for each emission (number of assertions tested) | ||
// 4. that the event listener gets called with the correct argument and the correct number of times (number of assertions tested, plus comparison assertion for agument) | ||
exports.testShimProcessorAsyncAndMultiEmit = function(test){ | ||
test.expect(7); | ||
var testObj = new events.EventEmitter(); | ||
var shim = evtTools.EventShim(testObj, ["testEvent2"]); | ||
var firstProcessorCalled = false; | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(!firstProcessorCalled, "First processor was called more than once"); | ||
firstProcessorCalled = true; | ||
cb(); | ||
}); | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
cb(magicNo); | ||
cb(magicNo+1); | ||
setTimeout(function(){ cb(magicNo+2); },10); | ||
}); | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
test.ok(true, "Marker assertion to verify number of subsequent processor calls"); | ||
cb(); | ||
}); | ||
var expectedNumber = 42; | ||
shim.on("testEvent2", function(magicNo){ | ||
test.equals(magicNo, expectedNumber, "Incorrect argument provided"); | ||
expectedNumber++; | ||
}); | ||
testObj.emit("testEvent2",42); | ||
setTimeout(function(){ test.done(); }, 20); | ||
//test.done(); | ||
}; | ||
//Verifies that hook causes the object itself to emit the event and the arguments are processed | ||
@@ -142,4 +176,4 @@ //Also ensures that 'this' holds the correct value when called | ||
var shim = evtTools.EventHook(testObj, ["testEvent2"]); | ||
shim.addEventProcessor("testEvent2", function(magicNo){ | ||
return [magicNo + 10]; | ||
shim.addEventProcessor("testEvent2", function(cb, magicNo){ | ||
cb(magicNo + 10); | ||
}); | ||
@@ -146,0 +180,0 @@ testObj.on("testEvent2", function(magicNo){ |
11784
248