@haxiomic/event-signal
Version:
A single event emitter with priority support for TypeScript
41 lines (40 loc) • 1.7 kB
TypeScript
/**
* Event emitter with a notion of explicit ordering via priority
*/
export declare class EventSignal<Payload = undefined, E = EventSignal.Emitted<Payload>> {
private listeners;
addListener(listener: (event: E) => void, priority?: number): EventSignal.Listener;
/** Alias for addListener */
on(listener: (event: E) => void, priority?: number): EventSignal.Listener;
removeListener(listener: (event: E) => void): void;
once(listener: (event: E) => void, priority?: number): EventSignal.Listener;
/**
* Dispatch an event by providing a payload
* The underlying event object will be created and populated with the payload
*
* @param maxPriority If provided, only listeners with a priority equal or lower than this will be called
*/
dispatch(payload: Payload, maxPriority?: number): void;
/**
* Dispatch an event with an existing event object
*
* This is useful if you want to forward an event from another source like a DOM event
*/
dispatchWithExistingEvent(payload: Payload, maxPriority?: number): void;
hasListeners(): boolean;
private sortPriorityDescending;
private patchPayload;
}
export declare namespace EventSignal {
type Emitted<T> = T extends Event ? T & {
propagationStopped?: boolean;
} : T;
type Listener = {
/** Priority of the listener, higher numbers indicate higher priority, this is allowed to change at runtime */
priority: number;
listener: (event: any) => void;
remove: () => void;
/** Removes the listener when the provided signal is dispatched */
removeOnSignal: (signal: EventSignal<any, any>) => void;
};
}