eventbus-z
Version:
Simple event emitter. Global event emitter
171 lines (170 loc) • 8.99 kB
TypeScript
export type TCallback = (...restParams: any[]) => any;
export interface IEventBusParams {
timeCached?: number;
scopeName?: string;
single?: boolean;
callback: TCallback;
}
export interface IEventBusData {
once?: boolean;
emitted?: boolean;
timeCached?: number;
timeEffect?: number;
scopeName?: string;
callback: TCallback;
}
declare class EventBusBuilder {
/**
* Registers a callback function to be executed once when the specified event is triggered.
*
* @param {string} name - The name of the event.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @return {void}
*/
$once(name: string, callback: TCallback): void;
/**
* Registers a callback function to be executed when the specified event is triggered.
*
* @param {string} name - The name of the event.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @return {void}
*/
$on(name: string, callback: TCallback): void;
/**
* Registers a callback function to be executed when the specified event is triggered in the specified scope.
*
* @param {string} name - The name of the event.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @return {void}
*/
$onMultiple(name: string, callback: TCallback): void;
/**
* Emits an event to all registered listeners, caching the event for a specified amount of time to prevent multiple emissions within that time frame.
*
* @param {string} name - The name of the event to emit.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @param {number} timeCached - The amount of time in milliseconds to cache the event. Defaults to 100.
* @return {void}
*/
$onCached(name: string, callback: TCallback, timeCached?: number): void;
/**
* Emits an event to all registered listeners, caching the event for a specified amount of time to prevent multiple emissions within that time frame.
*
* @param {string} name - The name of the event to emit.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @param {number} timeCached - The amount of time in milliseconds to cache the event.
* @return {void}
*/
$onCachedMultiple(name: string, callback: TCallback, timeCached?: number): void;
/**
* Removes a specific event listener.
*
* @param {string} name - The name of the event.
* @param {TCallback} callback - The callback function to remove.
* @return {void}
*/
$off(name: string, callback: TCallback): void;
/**
* Removes all event listeners for the specified event name.
*
* @param {string} name - The name of the event to remove all listeners for.
* @return {void}
*/
$offAll(name: string): void;
/**
* Emits an event with the given name and parameters.
*
* @param {string} name - The name of the event to emit.
* @param {...any[]} params - The parameters to pass to the event listeners.
* @return {void}
*/
$emit(name: string, ...params: any[]): void;
/**
* Registers a callback function to be executed once when the specified event is triggered in the specified scope.
*
* @param {string} name - The name of the event.
* @param {string} scopeName - The name of the scope.
* @param {TCallback} callback - The callback function to be executed.
*/
$scopeOnce(name: string, scopeName: string, callback: TCallback): void;
/**
* Registers a callback function to be executed when the specified event is triggered
* in the specified scope, ensuring the callback is only executed once.
*
* @param {string} name - The name of the event.
* @param {string} scopeName - The name of the scope.
* @param {TCallback} callback - The callback function to be executed.
* @return {void}
*/
$scopeOn(name: string, scopeName: string, callback: TCallback): void;
/**
* Registers a callback function to be executed when the specified event is triggered
* in the specified scope.
*
* @param {string} name - The name of the event.
* @param {string} scopeName - The name of the scope.
* @param {TCallback} callback - The callback function to be executed.
*/
$scopeOnMultiple(name: string, scopeName: string, callback: TCallback): void;
/**
* Emits an event to all registered listeners within the specified scope,
* caching the event for a specified amount of time to prevent multiple
* emissions within that time frame.
*
* @param {string} name - The name of the event to emit.
* @param {string} scopeName - The name of the scope in which to emit the event.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @param {number} timeCached - The amount of time in milliseconds to cache the event.
* @return {void}
*/
$scopeOnCached(name: string, scopeName: string, callback: TCallback, timeCached?: number): void;
/**
* Emits an event to all registered listeners within the specified scope,
* caching the event for a specified amount of time to prevent multiple
* emissions within that time frame.
*
* @param {string} name - The name of the event to emit.
* @param {string} scopeName - The name of the scope in which to emit the event.
* @param {TCallback} callback - The callback function to be executed when the event is emitted.
* @param {number} timeCached - The amount of time in milliseconds to cache the event.
* @return {void}
*/
$scopeOnCachedMultiple(name: string, scopeName: string, callback: TCallback, timeCached?: number): void;
/**
* Removes the specified event listener from the given scope.
*
* @param {string} name - The name of the event.
* @param {string} scopeName - The name of the scope.
* @param {TCallback} callback - The callback function to remove.
* @return {void}
*/
$scopeOff(name: string, scopeName: string, callback: TCallback): void;
/**
* Removes all event listeners for the specified event within the given scope.
*
* @param {string} name - The name of the event to remove listeners for.
* @param {string} scopeName - The name of the scope in which to remove listeners.
* @return {void}
*/
$scopeOffAll(name: string, scopeName: string): void;
/**
* Emits an event to all registered listeners within the specified scope.
*
* @param {string} name - The name of the event to emit.
* @param {string} scopeName - The name of the scope in which to emit the event.
* @param {...any[]} params - The parameters to pass to the event listeners.
* @return {void}
*/
$scopeEmit(name: string, scopeName: string, ...params: any[]): void;
/**
* Removes all events associated with the specified scope name.
*
* @param {string} name - The name of the scope to remove events from.
* @return {void}
*/
$clearAllEventName(name: string): void;
}
declare const EventBus: EventBusBuilder;
declare const $once: (name: string, callback: TCallback) => void, $on: (name: string, callback: TCallback) => void, $off: (name: string, callback: TCallback) => void, $offAll: (name: string) => void, $emit: (name: string, ...params: any[]) => void, $onCached: (name: string, callback: TCallback, timeCached?: number) => void, $onMultiple: (name: string, callback: TCallback) => void, $scopeOnMultiple: (name: string, scopeName: string, callback: TCallback) => void, $scopeOnce: (name: string, scopeName: string, callback: TCallback) => void, $scopeOn: (name: string, scopeName: string, callback: TCallback) => void, $scopeOff: (name: string, scopeName: string, callback: TCallback) => void, $scopeOffAll: (name: string, scopeName: string) => void, $scopeEmit: (name: string, scopeName: string, ...params: any[]) => void, $scopeOnCached: (name: string, scopeName: string, callback: TCallback, timeCached?: number) => void, $scopeOnCachedMultiple: (name: string, scopeName: string, callback: TCallback, timeCached?: number) => void, $onCachedMultiple: (name: string, callback: TCallback, timeCached?: number) => void, $clearAllEventName: (name: string) => void;
export { $once, $on, $off, $offAll, $emit, $onCached, $onMultiple, $scopeOnMultiple, $scopeOnce, $scopeOn, $scopeOff, $scopeOffAll, $scopeEmit, $scopeOnCached, $scopeOnCachedMultiple, $onCachedMultiple, $clearAllEventName, EventBus as eventBus, };
export default EventBus;