UNPKG

@creejs/commons-events

Version:
150 lines (149 loc) 6.52 kB
/** * An Event definition * 1. listeners are grouped by owner * * if not specified, group it into one Default owner * 2. one listener may belong to multiple owners * 3. one owner may have multiple listeners */ export default class Event { static get DefaultOwner(): string; /** * Creates a new Event instance with the specified name. * @param {string|Symbol} eventName - The name of the event. */ constructor(eventName: string | Symbol); _name: string | Symbol; /** * use Set to store unique listeners * @type {Set<function>} */ _callbacks: Set<Function>; /** * @type {Array<Listener>} */ _listeners: Array<Listener>; /** * @type {Map<function, Set<Listener>>} */ _callback2Listeners: Map<Function, Set<Listener>>; /** * use listener to index owners. One Listener Instance only belongs to one owner * @type {Map<Listener, *>} */ _listener2Owner: Map<Listener, any>; /** * use "owner" to group listeners; one owner may have multiple listeners * @type {Map<*, Set<Listener>>} */ _owner2Listeners: Map<any, Set<Listener>>; /** * Name of the event. * @returns {string|Symbol} */ get name(): string | Symbol; isEmpty(): boolean; /** * Returns a copy of the raw listeners array. * @returns {Array<Listener>} A shallow copy of the listeners array. */ rawListeners(): Array<Listener>; /** * Returns the number of listeners listening for the event. * If callback is provided, it will return how many times the callback is found in the list of the listeners * @param {Function} [callback] - The callback function to count * @returns {number} */ listenerCount(callback?: Function): number; /** * Returns a shallow copy of the registered callbacks array. * if one callback function is added multiple times, it will be returned multiple times. * @returns {Array<function>} A new array containing all registered callbacks. */ callbacks(): Array<Function>; /** * Emits current event, invoking all registered listeners by order. * @param {...*} args - Arguments to be passed to each listener. * @returns {boolean} True if the event had listeners, false otherwise. */ emit(...args: any[]): boolean; /** * Emits current event, invoking all registered listeners by order. * @param {string|Symbol} owner * @param {...*} args - Arguments to be passed to each listener. * @returns {boolean} True if the event had listeners, false otherwise. */ emitOwner(owner: string | Symbol, ...args: any[]): boolean; /** * Checks if listener is registered with this event. * @param {function} callback - The listener function to check. * @returns {boolean} True if the listener is registered, false otherwise. */ hasListener(callback: Function): boolean; /** * Checks if owner has any registered listeners. * @param {*} owner - The owner to check for registered listeners. * @returns {boolean} True if the owner has listeners, false otherwise. */ hasOwner(owner: any): boolean; /** * Adds an event listener * @param {function} callback - The callback function to be invoked when the event occurs. * @param {*} [owner] - use "owner" to group listeners, then can remove all listeners for an owner. * @returns {boolean} true if added, false otherwise. */ addListener(callback: Function, owner?: any): boolean; /** * Prepends a listener callback to the beginning of the listeners array. * No checks are made to see if the listener has already been added. * Multiple calls passing the same combination of eventName and listener will result in the listener being added, * and called, multiple times. * @param {Function} callback - The callback function to be executed when the event is emitted. * @param {Object} owner - The owner object to which the listener belongs. * @returns {boolean} */ prependListener(callback: Function, owner: Object): boolean; /** * Adds a one-time listener for the event. The listener is automatically removed after being invoked once. * @param {Function} callback - The function to call when the event is triggered. * @param {Object} [owner] - The object that owns the callback (used for binding `this` context). * @returns {boolean} */ addOnceListener(callback: Function, owner?: Object): boolean; /** * Adds a one-time event listener that will be automatically removed after being triggered once. * The listener will only be triggered if the event is pretended (simulated). * @param {Function} callback - The function to call when the event is triggered. * @param {Object} owner - The object that owns the listener (used for reference tracking). * @returns {boolean} The listener function that was added. */ prependOnceListener(callback: Function, owner: Object): boolean; /** * Adds a listener for the event. * @param {Function} callback - The callback function to be executed when the event occurs * @param {*} [owner] - The owner object that the listener belongs to (defaults to DeaultOwner) * @param {boolean} [isOnce=false] - Whether the listener should be removed after first invocation * @param {boolean} [isPrepend=false] - Whether the listener should be inert at the beginning of the listeners array * @returns {boolean} Returns true if listener was added successfully, false if callback is nil or duplicate * @protected */ protected _addListener(callback: Function, owner?: any, isOnce?: boolean, isPrepend?: boolean): boolean; /** * Removes a callback * @param {Function} callback - The callback function to remove. * @returns {boolean} true if removed, false otherwise. */ removeListener(callback: Function): boolean; /** * Removes a listener from both global and owner indexes. * @param {Listener} listener - The listener function to remove */ _remove(listener: Listener): void; /** * Removes all event listeners * @param {*} [owner] - Without owner, all listeners will be removed; otherwise, only listeners of "owner" will be removed. * @returns {this} */ removeAllListeners(owner?: any): this; } export { Event as EventType }; import Listener from './listener.js';