Eventz
Async events inspired by $.Callbacks
and written in ES6
Why?
You may be wonder why not just use EventEmitter
from events
package? So, i've been excited by how does $.Callbacks
do the job, also i
- Fully ES6
- Event options
once
,memory
,stop
- Events namespacing
- Can be used as extension for ES6 classes
Install
npm install event-z --save
Usage
There are two ways to use Eventz
:
- You can invoke
new Events(eventsList[, options])
by itslef - You can extend your own class from
Eventz
Self-invoking
// Create instance of Eventzconst events = 'helloSaid' 'goodbyeSaid' // Attach event handlersevents events // Invoke events events events
Constructor options
You can pass setup object as a second argument of the constructor.
Available options:
context
– all the handlers will be invoked with this context, defaults toEventz
instanceexpose
– if true thenEventz
will expose methods like.on()
and.emit()
to selected context
Extending class
// Create your own class eventsList = 'helloSaid' 'goodbyeSaid' ; { this this } // Create instance of your classconst cls = // Now you can attach events directly// to instance of your classcls cls // Invoke events from inside of classcls // Or invoke events directlycls cls
extends
ES6 classes without When you don't want to extend your class but still need to have easy way to bind/enit/detach your events you can use Eventz
this way:
// Create your own class { 'helloSaid' 'goodbyeSaid' context : this expose : true } { this this } // Create instance of your classconst cls = // Now you can attach events directly// to instance of your classcls cls // Invoke events from inside of classcls // Or invoke events directlycls cls
Event modifiers
When setting up events list you may configure how each of your events will behave. You'll have 3 options:
once
– each handler will be invoked only oncememory
– event handlers which were attached after event invocation will be called immediatellystop
– will stop handlers execution if handler returnsfalse
Options should be passed with events separated by semicolon: [eventName]:[option1]:[option2]
, for example
helloSaid:once
or goodbyeSaid:memory:stop
.
const events = 'fireOnce:once' 'memorized:memory' 'shouldStop:stop' // Example of `once`events events// => 'Callback 1' events events// => 'Callback 2' events // nothing will happen, all handlers were invoked once // Example of `memory`events events// => Works just as normal events// => Will be invoked immediatelly // Example of `stop` // worksevents // also works but will stop everything afterevents // will never workevents // will never workevents events
Passing arguments
You can pass any arguments to the event handler while ivoking event
events events// => I'am Tim Cook
Multiple events invocation
You also can attach/emit/detach multiple events at once. To do this you should pass event namses as single string separated by space
events
Then emit your events separately
events events
or simultaneously
events
You also can detach events this way
events
Events namespacing
Any event can be namespaced. Namespaces separated by .
from the event name. You don't need to define namespace when initializing Eventz
.
const events = 'myEvent' events events events// => Without namespace// => With namespace events// => With namespace events// will remove only namespaced handler events// Nothing happens events// => Without namespace
Public methods
These methods will be available on Eventz
instance or on the instance of class extended from Eventz
.
.on(eventNames, handlerFunction)
Attaches handler to Eventz
instance
eventNames
– one or more event names separated by spacehandlerFunction
– function which will be invoked after firing the event
.off([eventNames, handlerFunction])
Removes handler from Eventz
instance.
If no arguments passed, then all the handlers for all the events will be removed
If no handlerFunction
passed then all the handlers for specified events will be removed
eventNames
– one or more event names separated by spacehandlerFunction
– function which will be invoked after firing the event
.emit(eventNames, [...arguments])
Invokes specified event(s)
eventNames
– one or more event names separated by spacearguments
– any arguments to pass to handler when event was fired
License
Copyright © 2016 Nicholas Strife nr@fenelon.ru
This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See LICENSE for full details.