subscribe
Version:
A simple better event emitter
57 lines • 2.15 kB
TypeScript
type EventHandler = (event?: any) => void;
type WildCardEventHandler = (type: string, event?: any) => void;
/**
* An array of all currently registered event handlers for a type
*/
type EventHandlerList = Array<EventHandler>;
type WildCardEventHandlerList = Array<WildCardEventHandler>;
/**
* A map of event types and their corresponding event handlers.
*/
type EventHandlerMap = {
'*'?: WildCardEventHandlerList;
[type: string]: EventHandlerList;
};
/**
* Emitter class that powers the subscribe packet
*/
export declare class Emitter<EventNames = string> {
listeners: EventHandlerMap;
/**
* Use the constructor to initiate the eventbus with handlers
* @param {EventHandlerMap} listeners
*/
constructor(listeners?: EventHandlerMap);
/**
* Register an event handler for the given type.
*
* @param {String} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to given event
*/
on(type: (string & EventNames) | "*", handler: EventHandler | WildCardEventHandler): void;
/**
* Invoke this listener only once
* @param {string & EventNames} type Type of event to emit, or `"*"` for all events
* @param {EventHandler} handler
*/
once(type: string & EventNames, handler: EventHandler): void;
/**
* Remove an event handler for the given type.
*
* @param {String} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
*/
off(type: string & EventNames, handler?: EventHandler | WildCardEventHandler): void;
/**
* Invoke this.listeners handlers for the given type.
* If present, `"*"` handlers are invoked after type-matched handlers.
*
* Note: Manually firing "*" handlers is not supported.
*
* @param {String} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
*/
emit(type: string & EventNames, evt?: any): void;
}
export {};
//# sourceMappingURL=index.d.ts.map