Patch for Events.EventEmitter, Mongoose.Document, Mongoose.Schema
The patch adds methods to mentioned above classes that allow to emit events and handle them by functions with callback.
The original EventEmitter.emit also supports handling by functions with callback, but the key feature of method EventEmitter.emitAsync added by this patch is that callback function specified in the method call will be called after all handlers processed.
npm install shema-emit-async
Development and testing
git clone https://github.com/DScheglov/schema-emit-async.gitnpm installnpm test
The patch adds the following methods:
events.EventEmitter.prototype.emitAsync - emits event and calls callback after all handlers processed
mongoose.Document.prototype.emitAsync - duck-style inheritance of EventEmitter.eventAsync -- allows to emit event and pass one or more params to the handlers
mongoose.Schema.prototype.when -- adds handler as function with callback that binds to the Documents emitted the event
EventEmitter#emitAsync(event, ..., callback)
Emits the event specified by
event parameter and calls the
callback function after all handlers processed
String, The event to be emitted
any type, zero or any number of parameters that will be passed to the event handlers.
Function, The function which is called when all handlers have finished or some handler have fired the exception
Boolean, Returns true if there is at least one handler, otherwise returns false
Error|*, the exception raised in one of handlers. The handler raised the exception stops the further event handling. If no exception was thrown the err is
Array, the array of results passed by each handler to the
;var EventEmitter = EventEmitter;var emitter = ;emitter;......emitter;......emitter;
Hello world!!!Hello world again!!!Error: no errorsResults: "The first handler processed." & "The second handler processed."
Instead of the Example above the code uses the original EventEmitter.emit:
var EventEmitter = EventEmitter;var emitter = ;emitter;......emitter;......emitter;
produces the following output:
Hello world!!!Error: no errorsResults: "The first handler processed."Hello world again!!!Error: no errorsResults: "The second handler processed."
Document.prototype.emitAsync - the wrapper method of EventEmitter that is attached to the mongoose.Document
Schema.prototype.when - adds the asynchronious handler for event and binds the handler to the first arguments passed in emitAsync call. The difference between Schema.prototype.on and Schema.prototype.when is method on attaches the handler to the Schema that is an instanceof EventEmitter, instead of method when adds listener to the internal EventEmitter of the Document instance.
string, event that should be handled
handlerof the event
mongoose.Schema, the Schema
any, the zero or more parameters passed to the handler in a emitAsync call, excluding the first arguments that is available in the handler as
Function- the callback that should be called after handler ends
;var mongoose = ;var Schema = mongooseSchema;var xSchema =x: Number;xSchemavar xySchema =y: Number;xySchema;var xModel = mongoose;var xyModel = xModel;var X = x:2;var XY = x: 2 y: 3;Xx = 3;Xx = 0;XYx = 5;XYx = 0;XYx = 7;
x = 2x = 2x = 3x = 0x = 5