yatem
Yet Another Tiny Event Emitter
Install
Node
npm install yatem --save
Browser
Usage
Node
var yatem = ; var e1 = yatem;var e2 = yatem;var e3 = yatem; yatem;// output:// > This is event: my-event1// > This is also event: my-event1, and args is: hello, world yatem;yatem; // or yatem.emit(e1)// output:// > This is event: my-event1 yatem;// output:// > This is event: my-event3 yatem; // again, it will do nothing
Browser
yatem; yatem;// output:// > This is event: my-event
Instance Methods
on(event, callback[, context])
Subscribe to an event
event
- the name of the event to subscribecallback
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback
If subscribe successful, return an object, that can use for yatem.off() and yatem.emit(). Otherwise return false.
var e1 = yatem; // typeof e1 === 'object'var e2 = yatem; // e2 === false yatem; // Okyatem; // Do nothing
once(event, callback[, context])
Subscribe to an event only once
event
- the name of the event to subscribecallback
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback
If subscribe successful, return an object, that can use for yatem.off() and yatem.emit(). Otherwise return false.
off(event)
Unsubscribe from an event.
event
- the name of the event to unsubscribe or value that return from yate.on() or yate.once()
If event is null or undefined, and not boolean type, it will unsubscribe all events.
var e1 = yatem;var e2 = yatem; yatem; // Unsubscribe all events, or use yatem.off(null)// Notice: yatem.off(false) WILL NOT unsubscribe all events, it will do nothing yatem; // Do nothingyatem; // Do nothing
emit(event[, arguments...])
Trigger a named event or all events
event
- the event name to emitarguments...
- any number of arguments to pass to the event subscribers
If event is null or undefined, and not boolean type, it will emit all events.
var e1 = yatem;var e2 = yatem; yatem; // Emit all events, or use yatem.emit(null[, arguments...])// Notice: yatem.emit(false) WILL NOT emit any events, it will do nothing
Test
npm install
npm test
License
GPL-3.0