Dependencyless simple ES6/5 event machine inspired by `$.Callbacks`
npm install event-z



Async events inspired by $.Callbacks and written in ES6
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
``bash`
npm install event-z --save
There are two ways to use Eventz:
1. You can invoke new Events(eventsList[, options]) by itslefEventz
2. You can extend your own class from
`javascript
import Eventz from 'event-z'
// Create instance of Eventz
const events = new Eventz([
'helloSaid',
'goodbyeSaid'
])
// Attach event handlers
events.on('helloSaid', function(){
console.log('hello said')
})
events.on('goodbyeSaid', function(){
console.log('hello said')
})
// Invoke events
events.emit('helloSaid')
events.emit('goodbyeSaid')
`
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 to Eventz instance expose
* – if true then Eventz will expose methods like .on() and .emit() to selected context
`javascript
import Eventz from 'event-z'
// Create your own class
class MyClass extends Eventz{
eventsList = [
'helloSaid',
'goodbyeSaid'
];
invokeAllEvents(){
this.emit('helloSaid')
this.emit('goodbyeSaid')
}
}
// Create instance of your class
const cls = new MyClass
// Now you can attach events directly
// to instance of your class
cls.on('helloSaid', function(){
console.log('hello said')
})
cls.on('goodbyeSaid', function(){
console.log('hello said')
})
// Invoke events from inside of class
cls.invokeAllEvents()
// Or invoke events directly
cls.emit('helloSaid')
cls.emit('goodbyeSaid')
`
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:
`javascript
import Eventz from 'event-z'
// Create your own class
class MyClass{
constructor(){
new Eventz([
'helloSaid',
'goodbyeSaid'
], {
context : this,
expose : true
})
}
invokeAllEvents(){
this.emit('helloSaid')
this.emit('goodbyeSaid')
}
}
// Create instance of your class
const cls = new MyClass
// Now you can attach events directly
// to instance of your class
cls.on('helloSaid', function(){
console.log('hello said')
})
cls.on('goodbyeSaid', function(){
console.log('hello said')
})
// Invoke events from inside of class
cls.invokeAllEvents()
// Or invoke events directly
cls.emit('helloSaid')
cls.emit('goodbyeSaid')
`
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 returns false
Options should be passed with events separated by semicolon: [eventName]:[option1]:[option2], for examplehelloSaid:once or goodbyeSaid:memory:stop.
`javascript
import Eventz from 'event-z'
const events = new Eventz([
'fireOnce:once',
'memorized:memory',
'shouldStop:stop'
])
// Example of once
events.on('fireOnce', function(){ console.log('Callback 1') })
events.emit('fireOnce')
// => 'Callback 1'
events.on('fireOnce', function(){ console.log('Callback 2') })
events.emit('fireOnce')
// => 'Callback 2'
events.emit('fireOnce') // nothing will happen, all handlers were invoked once
// Example of memory
events.on('memorized', function(){
console.log('Works just as normal')
})
events.emit('memorized')
// => Works just as normal
events.on('memorized', function(){
console.log('Will be invoked immediatelly')
})
// => Will be invoked immediatelly
// Example of stop
// works
events.on('shouldStop', function(){ console.log('One') })
// also works but will stop everything after
events.on('shouldStop', function(){ console.log('Two'); return false })
// will never work
events.on('shouldStop', function(){ console.log('Three') })
// will never work
events.on('shouldStop', function(){ console.log('Four') })
events.emit('shouldStop')
`
You can pass any arguments to the event handler while ivoking event
`javascriptI'am ${name} ${surname}
events.on('someEvent', function(name, surname){
console.log()
})
events.emit('someEvent', 'Tim', 'Cook')
// => I'am Tim Cook
`
You also can attach/emit/detach multiple events at once. To do this you should pass event namses as single string separated by space
`javascript`
events.on('firstEvent secondEvent', function(){
console.log('event invoked')
})
Then emit your events separately
`javascript
events.emit('firstEvent')
events.emit('secondEvent')
`
or simultaneously
`javascript`
events.emit('firstEvent secondEvent')
You also can detach events this way
`javascript`
events.off('firstEvent secondEvent')
Any event can be namespaced. Namespaces separated by . from the event name. You don't need to define namespace when initializing Eventz.
`javascript
const events = new Eventz([
'myEvent'
])
events.on('myEvent', function(){
console.log('Without namespace')
})
events.on('myEvent.myNamespace', function(){
console.log('With namespace')
})
events.emit('myEvent')
// => Without namespace
// => With namespace
events.emit('myEvent.myNamespace')
// => With namespace
events.off('myEvent.myNamespace')
// will remove only namespaced handler
events.emit('myEvent.myNamespace')
// Nothing happens
events.emit('myEvent')
// => Without namespace
`
These methods will be available on Eventz instance or on the instance of class extended from Eventz.
Attaches handler to Eventz instance
* eventNames – one or more event names separated by spacehandlerFunction
* – function which will be invoked after firing the event
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
Invokes specified event(s)
* eventNames – one or more event names separated by spacearguments` – any arguments to pass to handler when event was fired
*
This work is free. You can redistribute it and/or modify it under the
terms of the MIT License.
See LICENSE for full details.