eventize.js
yet another fantastic event emitter micro framework for javascript!
FEATURES
- :sparkles: wildcards & priorities :exclamation:
- has typescript types included :tada:
- :rocket: powerful api (partial similar to node.js events)
- all api-calls and downstream-listener-calls are 100% synchronous :boom: no async! :stuck_out_tongue_closed_eyes:
- supports all major browsers and Node.js environments
- very small footsprint ~2.8k gzip'd
- no runtime dependencies
- apache-2.0 license
Getting Started
Attach the eventizer api to any javascript object you want.
import eventize from 'eventize-js';
const say = hello => world => console.log(hello, world);
const obj = eventize({});
obj.on('foo', say('hello'));
obj.once(['foo', 'bar'], PRIO_A, {
foo: say('hej'),
});
obj.on(['foo', 'bar'], PRIO_LOW, say('moin moin'))
obj.emit('foo', 'world');
obj.on('foo', () => obj.off('foo'));
obj.emit(['foo', 'bar'], 'eventize');
Installation
Install the latest version with: npm install --save eventize-js
API Reference
The eventize API
eventize( obj ) // alias for eventize.inject()
eventize.inject( obj ) // => eventizer === obj
eventize.extend( obj ) // => eventizer (prototype is obj)
eventize.create( obj ) // => eventizer
.. or if you like a more class based approach ..
import {Eventize} from 'eventize-js';
class Foo extends Eventize {
}
The eventizer API
.on( eventName*, [ priority, ] listenerFunc [, listenerObject] )
.on( eventName*, [ priority, ] listenerFuncName, listenerObject )
.on( eventName*, [ priority, ] listenerObject )
.on( [ priority, ] listenerFunc [, listenerObject] ) => listenerObject.on( '*', listenerFunc )
.on( [ priority, ] listenerObject ) => listenerObject.on( '*', listenerObject )
eventName*: eventName | Array<eventName>
eventName: string
listenerFunc: function
listenerFuncName: string
listenerObject: object
eventizer.once( ... )
eventizer.emit( eventName* [, args... ] )
eventizer.off( listenerFunc [, listenerObject] )
eventizer.off( listenerFuncName, listenerObject )
eventizer.off( listenerObject )
eventizer.off( eventName )
eventizer.off()
retain()
eventizer.retain( eventName* )
Additional API Helpers
eventize.is( obj )
Check if obj
is an eventizer (object has the eventizer api implemented). Returns true
or false
eventize.PRIO_MAX
eventize.PRIO_A
eventize.PRIO_B
eventize.PRIO_C
eventize.PRIO_DEFAULT = 0
eventize.PRIO_LOW
eventize.PRIO_MIN
Some predefined priorities. Use it or not. They are defined just for convenience.