NodeJS like EventEmitter. See also NodeJS events documentation
Starting from UB@5.23.9 constructor can accept optional traceAs: string
parameter. Is specified,
all event handlers calls will be logged using console.traceFunc
on Trace
level.
const EventEmitter = require('events').EventEmitter
const myObject = {}
// add EventEmitter to myObject
EventEmitter.call(myObject, 'MyObject') // traceAs = 'muObject'
Object.assign(myObject, EventEmitter.prototype)
// In case object created via constructor function
//
// const util = require('util')
// function MyObject() {
// EventEmitter.call(this, MyObject)
// }
// util.inherits(MyObject, EventEmitter)
//
// var myObject = new MyObject()
// usage
myObject.on('myEvent', function logSumAndNum(num, str){ console.log(num, 'is', str) })
myObject.emit('myEvent', 1, 'two') // output: 1 is two
// Since we specify a `traceAs` parameter, if `Trace` level is enabled in server logging, will add log line:
// YYYYMMDD HHMMSS Trace MyObject.on(myEvent)->logSumAndNum
// to prevent tracing of some handlers, for example, a handler that already logs its call, `_skipEmitterTrace:true`
// can be added to handler:
function logSumAndNumWOTrace (num, sum) { console.debug('I trace my call by myself'); console.log(num, 'is (wo trace)', str) }
logSumAndNumWOTrace._skipEmitterTrace = true
myObject.on('myEvent', logSumAndNumWOTrace)
# new EventEmitter (traceAsopt: string)
Arguments:
traceAs
: stringif specified, all event handlers calls will be logged using
console.traceFunc
onTrace
level
Methods
# listenerCount (emitter: EventEmitter, type: String) → Number static
Return the number of listeners for a given event.
Arguments:
emitter
: EventEmittertype
: String
# addListener (type: String, listener: function) → EventEmitter instance
Adds a listener to the end of the listeners array for the specified event.
Will emit newListener
event on success.
Usage sample:
Session.on('login', function () {
console.log('someone connected!');
});
Returns emitter, so calls can be chained.
# emit (type: String, eventArgs: *) → boolean instance
Execute each of the listeners in order with the supplied arguments. Returns true if event had listeners, false otherwise.
Arguments:
type
: StringEvent name
eventArgs
: *Arguments, passed to listeners
# getMaxListeners () → Number instance
# listeners
(type:
String)
→
Array. instance
Returns an array of listeners for the specified event.
Arguments:
type
: StringEvent name
# on (type: String, listener: function) → EventEmitter instance
Alias for addListener
# once (type: String, listener: function) → EventEmitter instance
Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.
# prependListener (type: String, listener: function) → EventEmitter instance
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.
# prependOnceListener (type: String, listener: function) → EventEmitter instance
Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.
# removeAllListeners (type: String) → EventEmitter instance
Removes all listeners, or those of the specified event. It's not a good idea to remove listeners that were added elsewhere in the code, especially when it's on an emitter that you didn't create (e.g. sockets or file streams).
Returns emitter, so calls can be chained.
Arguments:
type
: StringEvent name
# removeListener (type: String, listener: function) instance
Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener. Emits a 'removeListener' event if the listener was removed.
# setMaxListeners (n: Number) instance
Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
Arguments:
n
: Number