@sheerid/jslib-nightly
Version:
SheerID JavaScript Library
38 lines (37 loc) • 1.35 kB
TypeScript
export type Unsubscribe = () => void;
/**
* List of valid event args given `K`.
*/
export type EventArg<T, K extends keyof T> = T[K];
/**
* An event emitter class we can use to manage hooks or other emittable data.
* Take a type argument of shape Record<eventName, callBackData>
*
* A classified version of the MIT licensed https://robinpokorny.github.io/dead-simple/
* With some type safety inspiration from https://github.com/serviejs/events
*/
export declare class EventEmitter<T> {
private events;
/**
* Registers a listener that will be called when the named event is emitted
* @returns An unsubscribe function that will turn off this listener.
*/
on<K extends keyof T>(name: K, fn: (data: EventArg<T, K>) => void): Unsubscribe;
/**
* Registers a listener that will be called a single time when the named event
* is emitted
* @returns An unsubscribe function that will turn off this listener.
*/
once<K extends keyof T>(name: K, fn: (data: EventArg<T, K>) => void): Unsubscribe;
/**
* Removes all event listeners for the given key
* @returns void
*/
off<K extends keyof T>(name: K): void;
/**
* Used to emit events to all registered hook listeners
*
* @private
*/
emit<K extends keyof T>(name: K, data: EventArg<T, K>): void;
}