UNPKG

ecspresso

Version:

A minimal Entity-Component-System library for typescript and javascript.

30 lines (29 loc) 1.27 kB
export default class EventBus<EventTypes> { private handlers; /** * Subscribe to an event */ subscribe<E extends keyof EventTypes>(eventType: E, callback: (data: EventTypes[E]) => void): () => void; /** * Subscribe to an event once */ once<E extends keyof EventTypes>(eventType: E, callback: (data: EventTypes[E]) => void): () => void; /** * Unsubscribe a specific callback from an event by reference * @returns true if the callback was found and removed, false otherwise */ unsubscribe<E extends keyof EventTypes>(eventType: E, callback: (data: EventTypes[E]) => void): boolean; /** * Internal method to add an event handler */ private addHandler; /** * Publish an event. Data is required unless EventTypes[E] extends void | undefined. * Zero-allocation hot path: uses index-based iteration with a snapshot length * so handlers added mid-publish are not called in the same publish cycle. */ publish<E extends keyof EventTypes>(eventType: EventTypes[E] extends void | undefined ? E : never): void; publish<E extends keyof EventTypes>(eventType: E, data: EventTypes[E]): void; clear(): void; clearEvent<E extends keyof EventTypes>(eventType: E): void; }