It is small and light weight javascript event listener library for for client side js and node js.
on function is used to takes 3 arguments.
- event_name
<string>
required, - callback
<function>
required, - options
<object>
optional
callback function receives a value passed into emit function.
if true listener will be removed after one call.
if callLimit value is > 0 listener will be removed after it called callLimit value times.
Listener.on('userLoggedIn', function(data){
...do some stuff
}, {callOnce: true});
on function is used to emit an event, it takes 3 arguments.
- event_name
<string>
required, - data
<any>
optional, - context
<context>
optional, - options
<object>
optional
user can pass a context in which they want the callback function to be executed.
data can be anything user wants to pass to the listener callback.
if true, the emitted listener will be removed after this.
Listener.emmit('userLoggedIn', {}, this, {removeAfter: true});
broadcast function is used to emit more then one event, it takes 3 arguments.
- events
<array>
required, - default_data
<object>
optional, - default_context
<context>
optional, - default_options
<object>
optional
events is an array of strings/objects. value of each item can be a string or an object.
if array item is string the event will be emitted with the default_data, default_context and default_options (if any).
if array item is an object the event will be emitted with the data, context and options given to the object if not default passed value will be used (if any).
this object has 3 keys:
- eventName
- data
- context
- options
default data can be anything user wants to pass to every listener given in first argument array.
default context for all the listeners given in first argument.
if true, the emitted listener will be removed after this.
Listener.broadcast(['userLoggedIn', 'userCreated', {eventName: 'showNotificatons', data: {}, options: {callLimit: 2}}, 'getUserData'], {}, this);