Skip to content

Instantly share code, notes, and snippets.

@slavarazum
Last active July 21, 2023 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavarazum/35909f0b562596bdf36e79a1ed6f6a54 to your computer and use it in GitHub Desktop.
Save slavarazum/35909f0b562596bdf36e79a1ed6f6a54 to your computer and use it in GitHub Desktop.
A TypeScript `EventBus` class for type-safe event handling and dispatching, including support for one-time event listeners.
type Listener<T extends any[]> = (...args: T) => void;
export default class EventBus<EventMap extends Record<keyof EventMap, any[]>> {
private listeners: Partial<Record<keyof EventMap, Listener<EventMap[keyof EventMap]>[]>> = {};
once<K extends keyof EventMap>(event: K, listener: Listener<EventMap[K]>): void {
const onceListener: Listener<EventMap[K]> = (...args: EventMap[K]) => {
listener(...args);
this.off(event, onceListener);
};
this.on(event, onceListener);
}
on<K extends keyof EventMap>(event: K, listener: Listener<EventMap[K]>): void {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event]?.push(listener);
}
emit<K extends keyof EventMap>(event: K, ...args: EventMap[K]): void {
this.listeners[event]?.slice().forEach(listener => listener(...args));
}
off<K extends keyof EventMap>(event: K, listener: Listener<EventMap[K]>): void {
const listeners = this.listeners[event];
if (!listeners) return;
const index = listeners.indexOf(listener);
if (index > -1) {
listeners.splice(index, 1);
}
}
}
@slavarazum
Copy link
Author

Usage

TypeScript EventBus class providing a way to define event maps for structured, type-safe event handling. Events are declared in a specified format using an interface:

interface EventMap {
    connected: [id: string];
    open: [response: Response];
    reconnect: [];
    close: [];
}

Each event is associated with a tuple representing the parameters of the event listener callback, enabling strict type-checking for event dispatching and handling.

const bus = new EventBus<EventMap>();

bus.on('connected', (id: string) => id);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment