1.0.4 • Published 7 years ago
emit-kit v1.0.4
emit-kit
A wrapper on top of eventemitter3
with the convenient abstractions of event-kit
.
Usage
import {Emitter} from 'emit-kit';
// The event emitter
class Superhero {
constructor() {
this._emitter = new Emitter();
}
onFlight(callback) {
return this._emitter.on('flight', callback);
}
fly(time) {
this._emitter.emit('flight', time);
}
}
// The subscriber
class Supervillain {
plotToKill(superhero) {
this.surveillance = superhero.onFlight(time => console.log(time));
}
giveUp() {
this.surveillance.cancel();
}
}
API
Emitter
.on(event, callback, context)
- Subscribes a callback to an event.
event
- String. The name of the event.callback
- Function. Invoked with argumentscontext
(optional) - Object. Thethis
object to bind to thecallback
.
.once(event, callback, context)
- Same as
.on()
but with a one-time callback.
- Same as
.emit(event, ...args)
- Invokes all callbacks subscribed to
event
. event
- String. The name of the event.args
- Arguments to be passed to the callbacks.
- Invokes all callbacks subscribed to
.clear(event)
- If
event
is given, removes only the callbacks subscribed toevent
. Else removes all callbacks. event
(optional) - String. The name of the event.
- If
.count(event)
- If
event
is given, returns the number of callbacks registered toevent
. Else returns the total number of callbacks.
- If
Subscription
.cancel()
- Unsubscribes from the event emitter.
Subscriptions
.add(...subs)
- Groups
subs
together so that they can later be cancelled as a group.
- Groups
.cancel()
- Calls
cancel
asynchronously on all associatedsubs
. Returns a promise that fulfills upon completion.
- Calls
.cancelSync()
- Calls
cancel
synchronously on all associatedsubs
.
- Calls