@itwin/core-bentley
Version:
Bentley JavaScript core components
99 lines • 4.1 kB
TypeScript
/** @packageDocumentation
* @module Events
*/
/** A function invoked when a BeEvent is raised.
* @public
*/
export type Listener = (...arg: any[]) => void;
/**
* Manages a set of *listeners* for a particular event and notifies them when the event is raised.
* This class is usually instantiated inside of a container class and
* exposed as a property for others to *subscribe* via [[BeEvent.addListener]].
* @public
*/
export declare class BeEvent<T extends Listener> {
private _listeners;
private _insideRaiseEvent;
/** The number of listeners currently subscribed to the event. */
get numberOfListeners(): number;
/**
* Registers a Listener to be executed whenever this event is raised.
* @param listener The function to be executed when the event is raised.
* @param scope An optional object scope to serve as the 'this' pointer when listener is invoked.
* @returns A function that will remove this event listener.
* @see [[BeEvent.raiseEvent]], [[BeEvent.removeListener]]
*/
addListener(listener: T, scope?: any): () => void;
/**
* Registers a callback function to be executed *only once* when the event is raised.
* @param listener The function to be executed once when the event is raised.
* @param scope An optional object scope to serve as the `this` pointer in which the listener function will execute.
* @returns A function that will remove this event listener.
* @see [[BeEvent.raiseEvent]], [[BeEvent.removeListener]]
*/
addOnce(listener: T, scope?: any): () => void;
/**
* Un-register a previously registered listener.
* @param listener The listener to be unregistered.
* @param scope The scope that was originally passed to addListener.
* @returns 'true' if the listener was removed; 'false' if the listener and scope are not registered with the event.
* @see [[BeEvent.raiseEvent]], [[BeEvent.addListener]]
*/
removeListener(listener: T, scope?: any): boolean;
/**
* Raises the event by calling each registered listener with the supplied arguments.
* @param args This method takes any number of parameters and passes them through to the listeners.
* @see [[BeEvent.removeListener]], [[BeEvent.addListener]]
*/
raiseEvent(...args: Parameters<T>): void;
/** Determine whether this BeEvent has a specified listener registered.
* @param listener The listener to check.
* @param scope optional scope argument to match call to addListener
*/
has(listener: T, scope?: any): boolean;
/** Clear all Listeners from this BeEvent. */
clear(): void;
}
/** Specialization of BeEvent for events that take a single strongly typed argument, primarily used for UI events.
* @public
*/
export declare class BeUiEvent<TEventArgs> extends BeEvent<(args: TEventArgs) => void> {
/** Raises event with single strongly typed argument. */
emit(args: TEventArgs): void;
}
/**
* A list of BeEvent objects, accessible by an event name.
* This class may be used instead of explicitly declaring each BeEvent as a member of a containing class.
* @public
*/
export declare class BeEventList<T extends Listener> {
private _events;
/**
* Gets the event associated with the specified name, creating the event if it does not already exist.
* @param name The name of the event.
*/
get(name: string): BeEvent<T>;
/**
* Removes the event associated with a name.
* @param name The name of the event.
*/
remove(name: string): void;
}
/**
* Retrieves the type of the callback function for an event type like [[BeEvent]].
* For example:
* ```ts
* const event = new BeEvent<(x: number, y: string) => void>();
* const callback: ListenerType<typeof event> = (x, y) => {
* console.log(`${x}, ${y}`);
* };
* ```
*
* @public
*/
export type ListenerType<TEvent extends {
addListener(listener: Listener): () => void;
}> = TEvent extends {
addListener(listener: infer TListener): () => void;
} ? TListener : never;
//# sourceMappingURL=BeEvent.d.ts.map