Emitting is a simple event emitter designed for TypeScript and Promises. There are some differences from other emitters:
new EventEmitter<Events>()
.take("event"): Promise<Payload>
and samethrow
an error when you emit an error
event and nobody is listeningthis
.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.off(eventName)
— remove all listeners# NPM
npm instal --save emitting
# Yarn
yarn add emitting
Module built to ECMAScript 5.
Be sure to add polyfills if neccessary for:
Promise
Set
Map
After installation the only thing you need to do is require the module:
const { EventEmitter } = require("emitting")
const emitter = new EventEmitter()
After installation you need to import module and define events:
import { EventEmitter } from "emitting"
type Events = {
hello: { name: string }
bye: void
}
const emitter = new EventEmitter<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.
type Events = {
eventName: PayloadType
}
Pass Events
to constructor generic parameter:
import { EventEmitter } from "emitting"
type Events = {
hello: { name: string }
bye: void
}
const emitter = new EventEmitter<Events>()
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.
.on()
returns unsubscribe
function to remove created subscribtion.
function helloHandler(payload: { name: string }) {
console.log(payload.name)
}
function byeHandler() {
console.log("Goodbye!")
}
emitter.on("hello", helloHandler)
emitter.on("bye", byeHandler)
.once(eventName, handler)
— listen event onceSubscribes to event, and when it received immediately unsubscribe.
Subscribtion can be canceled at any time.
const cancel = emitter.on("hello", helloHandler)
cancel() // subscription cancelled
.emit(eventName, payload)
— send event to listenersExecutes 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.
emitter.emit("hello", { name: "world" })
.emitCallback(eventName)
— create emitter functionCreate function that emit event when called.
Payload should be passed to returned callback.
const hello = emitter.emitCallback("hello")
hello({ name: "world" }) // emitted "hello" event
.take(event): Promise
— wait for eventCreates promise that resolves when specified event is received.
Returns Promise
resolved with payload of the passed event.
Listeners removed after event is received.
type Events = {
example: number
}
const emitter = new EventEmitter<Events>()
emitter.take("example").then((payload) => {
console.log("Received", payload)
})
emitter.emit("example", 10) // Received 10
With async/await
:
const payload: number = await emitter.take("example")
.takeTimeout(event, ms): Promise
— wait for event or rejectCreates 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.
try {
const { name } = await emitter.takeTimeout("hello", 300)
} catch (_) {
console.info("Event `hello` is not emitted in 300 ms after subscribing to")
}
.takeEither(successEvent, failureEvent): Promise
— resolve or reject promise with eventsReturns promise that resolves when successEvent
is emitted with the payload of event.
Promise rejected when failureEvent
is emitted, as error passed payload of the failureEvent
.
Listeners removed when successEvent
or failureEvent
is received and promise resolves just once.
emitter
.takeEither("success", "failure")
.then((payload) => console.log("Yeeah", payload))
.catch((error) => console.log("Noooo", error))
emitter.emit("success", 500) // Yeeah 500
// or
emitter.emit("failure", "Vader is my father!") // Noooo Vader is my father
The .on()
, .once()
and same returns unsubscribe
function, that can be called multiple times at any time.
const unsubscribeHello = emitter.on("hello", helloHandler)
const unsubscribeBye = emitter.on("bye", helloHandler)
unsubscribeHello() // now helloHandler will not be called when "hello" is emitted
.off(eventName)
— remove all listenersNote: 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
emitter.off(eventName, handler)
*
event to listen all eventsemitter.events(): string[]
emitting::listenerAdded
, emitting::listenerRemoved
.subscribe
).iterate(eventName)
).public(): PublicEmitter
and .private(): PrivateEmitter
Generated using TypeDoc