Comparing version 1.0.0 to 1.0.1
18
index.js
@@ -21,6 +21,7 @@ var EventEmitter = require('events').EventEmitter; | ||
var eventProcessors = {}; | ||
var registeredEvents = []; | ||
var origOn = obj.on; | ||
var origRemoveAll = obj.removeAllListeners; | ||
ret.getObject = function(){ return obj; }; | ||
@@ -49,7 +50,8 @@ | ||
var forwardEvent = function(evt){ | ||
//Crude but necessary | ||
origRemoveAll.apply(obj, [evt]); | ||
origOn.apply(obj, [evt, function(){ | ||
executeProcessor(evt, arguments, 0); | ||
}]); | ||
if(registeredEvents.indexOf(evt) < 0){ | ||
origOn.apply(obj, [evt, function(){ | ||
executeProcessor(evt, arguments, 0); | ||
}]); | ||
registeredEvents.push(evt); | ||
} | ||
return ret; | ||
@@ -61,3 +63,3 @@ }; | ||
forwardEvent(arguments[0]); | ||
oldOn.apply(ret, arguments); | ||
return oldOn.apply(ret, arguments); | ||
}; | ||
@@ -94,3 +96,3 @@ | ||
} | ||
ret[funcName].apply(ret, arguments); | ||
return ret[funcName].apply(ret, arguments); | ||
} | ||
@@ -97,0 +99,0 @@ }); |
{ | ||
"name": "event-hook", | ||
"version": "1.0.0", | ||
"description": "", | ||
"version": "1.0.1", | ||
"description": "Allows installation of intermediate procesing functions between an event being emitted on an object and it's listeners receiving the events.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,2 +0,9 @@ | ||
node-event-hook | ||
=============== | ||
event-hook | ||
========== | ||
This library provides a mechanism to intercept and perform intermediate processing and mutation on an object's events before they're dispatched out to the object's listeners. For an example or two of how this might be used please see my framed-tcp module on npm or github. | ||
##Usage | ||
The module exports two functions that can be used to perform the event management, let's begin with the EventShim function. Simply pass in an object which is an EventEmitter and it will return a 'shim' object. This object should be used to bind event listeners to instead of the original object, in the usual style of shim.on('eventname', function(...){...}). Events that are emitted on the original object will then be dispatched to listeners on the shim object. The shim object has a method addEventProcessor(event, processorFunc) which inserts an intermediate processing function between the object and the shim for that particular event type. The processor will receive a callback function as it's first argument, and subsequent arguments will be the same as the ones passed to emit (excluding the event name). The processor function may do whatever it likes in the meantime, but should execute the callback function to propagate the event out to the shim's listeners. The callback may be executed multiple times if desired, and each call will result in a separate event being emitted on the shim. The callback may be executed with no arguments meaning that the event arguments will be passed on unmodified, or it may be executed by passing one or more arguments which will replace the event arguments. Multiple processors may be registered and they will be executed in the order they are registered. If a previous event modifies the argument list, prevents propagation of the event, or causes multiple events to be emitted then subsequent processors in the chain will see these effects as well as the listeners. | ||
The EventHook function extends that of EventShim but takes it a step further, replacing the EventEmitter-related functions on the object with those of the shim (except for emit). The result of this is that when listeners are attached to the object they will behave as if they were attached to the shim, that is to say the events will have been processed, and possibly modified/suppressed/duplicated along the way. This will not affect any existing listeners attached to the object. |
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
15381
282
9