Emitting is a simple event emitter designed for TypeScript and Promises. There are some differences from other emitters:
- Exactly typing for event payloads
- Waiting for event with
.take("event"): Promise<Payload>and same
- Do not
throwan error when you emit an
errorevent and nobody is listening
- Functional — methods don't rely on
- Small size. No dependencies. Size Limit controls the size.
Table of contents
.on(eventName, handler)— add event listener
.once(eventName, handler)— listen event once
.emit(eventName, payload)— send event to listeners
.emitCallback(eventName)— create emitter function
.take(event): Promise— wait for event
.takeTimeout(event, ms): Promise— wait for event or reject
.takeEither(successEvent, failureEvent): Promise— resolve or reject promise with events
- Unsubscribe from events
.off(eventName)— remove all listeners
# NPMnpm instal --save emitting# Yarnyarn add emitting
Module built to ECMAScript 5.
Be sure to add polyfills if neccessary for:
After installation the only thing you need to do is require the module:
const EventEmitter =const emitter =
After installation you need to import module and define events:
// Now you have typed event emitter 🚀 yay!
Documentation for API generated by TypeDOC — emitting.sova.dev
Receives type parameter
Events that should be object type or interface, where key is an event name and value is an payload as a single parameter type.
Events to constructor generic parameter:
Now you can emit events and subscribe to.
.on(eventName, handler) — add event listener
.on() receives an event name and an event handler.
Event handler should be a function that receives a payload.
unsubscribe function to remove created subscribtion.
emitter.on"hello", helloHandleremitter.on"bye", byeHandler
.once(eventName, handler) — listen event once
Subscribes to event, and when it received immediately unsubscribe.
Subscribtion can be canceled at any time.
cancel // subscription cancelled
.emit(eventName, payload) — send event to listeners
Executes all listeners with passed payload.
Accepts only one payload parameter. Use object type or tuple type to pass multiple payloads.
If no listeners nothing happens.
.emitCallback(eventName) — create emitter function
Create function that emit event when called.
Payload should be passed to returned callback.
hello // emitted "hello" event
.take(event): Promise — wait for event
Creates promise that resolves when specified event is received.
Promise resolved with payload of the passed event.
Listeners removed after event is received.
emitter.take"example".thenemitter.emit"example", 10 // Received 10
.takeTimeout(event, ms): Promise — wait for event or reject
Creates a promise that resolves when specified event is received.
Promise resolves with payload of the received event.
Promise is rejected when timeout is reached.
Listeners removed after timeout is reached, and event is received.
.takeEither(successEvent, failureEvent): Promise — resolve or reject promise with events
Returns promise that resolves when
successEvent is emitted with the payload of event.
Promise rejected when
failureEvent is emitted, as error passed payload of the
Listeners removed when
failureEvent is received and promise resolves just once.
emitter.takeEither"success", "failure".thenconsole.log"Yeeah", payload.catchconsole.log"Noooo", erroremitter.emit"success", 500 // Yeeah 500// oremitter.emit"failure", "Vader is my father!" // Noooo Vader is my father
Unsubscribe from events
.once() and same returns
unsubscribe function, that can be called multiple times at any time.
unsubscribeHello // now helloHandler will not be called when "hello" is emitted
.off(eventName) — remove all listeners
Note: destructive operation
The method removes all listeners from emitter. Use it with caution, if called
.take methods, promises will never be fullfilled.
emitter.off"hello" // all listeners removed
*event to listen all events
- Add support for reactive (
- Add async iterators (
- Add benchmarks