Create a new Emittery instance with the specified options.
An instance of Emittery that you can use to listen for and emit events.
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
Call return()
on the iterator to remove the subscription.
In the same way as for events
, you can subscribe by using the for await
statement.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.anyEvent();
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🌟', '🌈2'); // Buffered
iterator.next()
.then(({value, done}) => {
// done is false
// value is ['🦄', '🌈1']
return iterator.next();
})
.then(({value, done}) => {
// done is false
// value is ['🌟', '🌈2']
// revoke subscription
return iterator.return();
})
.then(({done}) => {
// done is true
});
Bind the given methodNames
, or all Emittery
methods if methodNames
is not defined, into the target
object.
Optional
methodNames: readonly string[]import Emittery from 'emittery';
const object = {};
new Emittery().bindMethods(object);
object.emit('event');
Clear all event listeners on the instance.
If eventName
is given, only the listeners for that event are cleared.
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
A promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
Same as emit()
, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit()
whenever possible.
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.
A promise that resolves when all the event listeners are done.
Get an async iterator which buffers data each time an event is emitted.
Call return()
on the iterator to remove the subscription.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events('🦄');
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
In practice you would usually consume the events using the for await statement. In that case, to revoke the subscription simply break the loop.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events('🦄');
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered
// In an async context.
for await (const data of iterator) {
if (data === '🌈2') {
break; // Revoke the subscription when we see the value `🌈2`.
}
}
It accepts multiple event names.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events(['🦄', '🦊']);
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦊', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
The number of listeners for the eventName
or all events if not specified.
Remove one or more event subscriptions.
import Emittery from 'emittery';
const emitter = new Emittery();
const listener = data => {
console.log(data);
};
emitter.on(['🦄', '🐶', '🦊'], listener);
await emitter.emit('🦄', 'a');
await emitter.emit('🐶', 'b');
await emitter.emit('🦊', 'c');
emitter.off('🦄', listener);
emitter.off(['🐶', '🦊'], listener);
await emitter.emit('🦄', 'a'); // nothing happens
await emitter.emit('🐶', 'b'); // nothing happens
await emitter.emit('🦊', 'c'); // nothing happens
Subscribe to one or more events.
Using the same listener multiple times for the same event will result in only one method call per emitted event.
An unsubscribe method.
import Emittery from 'emittery';
const emitter = new Emittery();
emitter.on('🦄', data => {
console.log(data);
});
emitter.on(['🦄', '🐶'], data => {
console.log(data);
});
emitter.emit('🦄', '🌈'); // log => '🌈' x2
emitter.emit('🐶', '🍖'); // log => '🍖'
Subscribe to be notified about any event.
A method to unsubscribe.
Subscribe to one or more events only once. It will be unsubscribed after the first event.
The promise of event data when eventName
is emitted. This promise is extended with an off
method.
import Emittery from 'emittery';
const emitter = new Emittery();
emitter.once('🦄').then(data => {
console.log(data);
//=> '🌈'
});
emitter.once(['🦄', '🐶']).then(data => {
console.log(data);
});
emitter.emit('🦄', '🌈'); // Logs `🌈` twice
emitter.emit('🐶', '🍖'); // Nothing happens
Static
mixinIn TypeScript, it returns a decorator which mixins Emittery
as property emitteryPropertyName
and methodNames
, or all Emittery
methods if methodNames
is not defined, into the target class.
Optional
methodNames: readonly string[]import Emittery from 'emittery';
@Emittery.mixin('emittery')
class MyClass {}
const instance = new MyClass();
instance.emit('event');
Debugging options for the current instance.
Static
isToggle debug mode for all instances.
Default: true
if the DEBUG
environment variable is set to emittery
or *
, otherwise false
.
import Emittery from 'emittery';
Emittery.isDebugEnabled = true;
const emitter1 = new Emittery({debug: {name: 'myEmitter1'}});
const emitter2 = new Emittery({debug: {name: 'myEmitter2'}});
emitter1.on('test', data => {
// …
});
emitter2.on('otherTest', data => {
// …
});
emitter1.emit('test');
//=> [16:43:20.417][emittery:subscribe][myEmitter1] Event Name: test
// data: undefined
emitter2.emit('otherTest');
//=> [16:43:20.417][emittery:subscribe][myEmitter2] Event Name: otherTest
// data: undefined
Static
Readonly
listenerFires when an event listener was added.
An object with listener
and eventName
(if on
or off
was used) is provided as event data.
import Emittery from 'emittery';
const emitter = new Emittery();
emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
console.log(listener);
//=> data => {}
console.log(eventName);
//=> '🦄'
});
emitter.on('🦄', data => {
// Handle data
});
Static
Readonly
listenerFires when an event listener was removed.
An object with listener
and eventName
(if on
or off
was used) is provided as event data.
import Emittery from 'emittery';
const emitter = new Emittery();
const off = emitter.on('🦄', data => {
// Handle data
});
emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
console.log(listener);
//=> data => {}
console.log(eventName);
//=> '🦄'
});
off();
Generated using TypeDoc
Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with
on
oronce
, and events can be emitted withemit
.Emittery
has a genericEventData
type that can be provided by users to strongly type the list of events and the data passed to the listeners for those events. Pass an interface of {[eventName]: undefined |undefined
if there isn't.Example