/** * Event Dispatcher module is used for registering listeners and dispatching * events across amCharts system. */ /** * ============================================================================ * IMPORTS * ============================================================================ * @hidden */ import { IDisposer } from "./Disposer"; /** * @ignore */ export declare type Events = { [K in keyof T]: T[K] & { type: K; target: Target; }; }; /** * A universal interface for event listeners. * * @ignore */ export interface EventListener { killed: boolean; once: boolean; type: any | null; callback: (event: any) => void; context: unknown; shouldClone: boolean; dispatch: (type: any, event: any) => void; disposer: IDisposer; _debounceDelay?: number; _debounceTimeout?: number; } /** * Universal Event Dispatcher. * * @see {@link https://www.amcharts.com/docs/v5/concepts/events/} for more info */ export declare class EventDispatcher implements IDisposer { protected _listeners: Array; protected _killed: Array; protected _disabled: { [key in keyof T]?: number; }; protected _iterating: number; protected _enabled: boolean; protected _disposed: boolean; /** * Constructor */ constructor(); /** * Returns `true` if the dispatcher has been disposed. * * @return Disposed? */ isDisposed(): boolean; /** * Disposes the dispatcher and all its listeners. */ dispose(): void; /** * Checks if the dispatcher has any listeners. * * @return Has listeners? */ hasListeners(): boolean; /** * Checks if the dispatcher has listeners for `type`, counting the ones added * with `onAll()`. * * @param type Event type * @return Has particular event listeners? */ hasListenersByType(type: Key): boolean; /** * Enable dispatching of events if they were previously disabled by * `disable()`. */ enable(): void; /** * Disable dispatching of events until re-enabled by `enable()`. */ disable(): void; /** * Enable dispatching particular event, if it was disabled before by * `disableType()`. * * @param type Event type */ enableType(type: Key): void; /** * Stops dispatching events of `type`, until `enableType()` is called or, if * `amount` is set, for that many dispatches. * * @param type Event type * @param amount Number of event dispatches to skip */ disableType(type: Key, amount?: number): void; /** * Removes listener from dispatcher. * * Will throw an exception if such listener does not exist. * * @param listener Listener to remove */ protected _removeListener(listener: EventListener): void; /** * Removes existing listener by certain parameters. * * @param once Listener's once setting * @param type Listener's type * @param callback Callback function * @param context Callback context */ protected _removeExistingListener(once: boolean, type: Key | null, callback?: (this: C, event: T[Key]) => void, context?: C): void; /** * Checks if an event of `type` would reach a listener: events are enabled, * the type is not disabled, and it has listeners. * * @param type Event type * @return Enabled? */ isEnabled(type: Key): boolean; /** * Removes all listeners of a particular event type. * * @param type Listener's type */ removeType(type: Key): void; /** * Checks if there's already a listener with specific parameters. Listeners * added with `once()` are not counted. * * @param type Listener's type * @param callback Callback function * @param context Callback context * @return Has listener? */ has(type: Key, callback?: (this: C, event: T[Key]) => void, context?: C): boolean; /** * Checks whether event of the particular type should be dispatched. * * @param type Event type * @return Dispatch? */ protected _shouldDispatch(type: Key): boolean; /** * Calls `fn` for each listener. Listeners removed meanwhile are taken out * of the list after the loop. * * @param fn Function to call */ protected _eachListener(fn: (listener: EventListener) => void): void; /** * Calls the listeners of `type` right away; debounced listeners are called * after their delay. `event` must include `type` and `target`. * * @param type Event type * @param event Event object */ dispatch(type: Key, event: T[Key]): void; /** * Shelves the event to be dispatched within next update cycle. * * @param type Event type * @param event Event object * @todo automatically add in type and target properties if they are missing */ /** * Creates, catalogs and returns an [[EventListener]], replacing an existing * one with the same parameters. * * @param once Listener's once setting * @param type Listener's type * @param callback Callback function * @param context Callback context * @param shouldClone Whether the listener should be copied when the EventDispatcher is copied * @param dispatch Function that calls `callback` for an event * @param debounceDelay Debounce delay in milliseconds * @returns An event listener */ protected _on(once: boolean, type: Key | null, callback: any, context: C, shouldClone: boolean, dispatch: (type: Key, event: T[Key]) => void, debounceDelay?: number): EventListener; /** * Creates an event listener to be invoked on **any** event. * * @param callback Callback function * @param context Value of `this` inside `callback` * @param shouldClone Whether the listener should be copied when the EventDispatcher is copied * @returns A disposable event listener */ onAll(callback: (this: C, event: T[K]) => void, context?: C, shouldClone?: boolean): IDisposer; /** * Creates an event listener to be invoked on a specific event type. * * ```TypeScript * button.events.on("click", (ev) => { * console.log("Button clicked"); * }, this); * ``` * ```JavaScript * button.events.on("click", function(ev) { * console.log("Button clicked"); * }, this); * ``` * * The above logs every click on the button. To remove the listener, call * `dispose()` on the returned object, or use `off()`. * * @param type Listener's type * @param callback Callback function * @param context Value of `this` inside `callback` * @param shouldClone Whether the listener should be copied when the EventDispatcher is copied * @returns A disposable event listener */ on(type: Key, callback: (this: C | undefined, event: T[Key]) => void, context?: C, shouldClone?: boolean): IDisposer; /** * Creates a debounced event listener to be invoked on a specific event type. * * ```TypeScript * button.events.onDebounced("click", (ev) => { * console.log("Button clicked"); * }, 500, this); * ``` * ```JavaScript * button.events.onDebounced("click", function(ev) { * console.log("Button clicked"); * }, 500, this); * ``` * * The above logs once, 500 ms after the last of a series of clicks. Each new * event restarts the delay, and `callback` gets the last one. * * @param type Listener's type * @param callback Callback function * @param debounceDelay Debounce delay in milliseconds * @param context Value of `this` inside `callback` * @param shouldClone Whether the listener should be copied when the EventDispatcher is copied * @returns A disposable event listener * @see {@link https://www.amcharts.com/docs/v5/concepts/events/#Debounced_events} for more info * @since 5.14.0 */ onDebounced(type: Key, callback: (this: C | undefined, event: T[Key]) => void, debounceDelay: number, context?: C, shouldClone?: boolean): IDisposer; /** * Creates an event listener that is invoked only the first time the event * happens, and is then disposed. * * ```TypeScript * button.events.once("click", (ev) => { * console.log("Button clicked"); * }, this); * ``` * ```JavaScript * button.events.once("click", function(ev) { * console.log("Button clicked"); * }, this); * ``` * * The above logs the first click on the button only. `off()` does not * remove such a listener: call `dispose()` on the returned object instead. * * @param type Listener's type * @param callback Callback function * @param context Value of `this` inside `callback` * @param shouldClone Whether the listener should be copied when the EventDispatcher is copied * @returns A disposable event listener */ once(type: Key, callback: (this: C | undefined, event: T[Key]) => void, context?: C, shouldClone?: boolean): IDisposer; /** * Removes listeners added with `on()` or `onDebounced()` that match `type`, * `callback` and `context`. Without `callback`, removes all of them for * `type` and `context`. Listeners added with `once()` are not removed. * * @param type Listener's type * @param callback Callback function * @param context Callback context, the same as passed to `on()` */ off(type: Key, callback?: (this: C, event: T[Key]) => void, context?: C): void; /** * Copies all dispatcher parameters, including listeners, from another event * dispatcher. * * @param source Source event dispatcher * @ignore */ copyFrom(source: this): IDisposer; } /** * A version of the [[EventDispatcher]] that dispatches events for a specific * target object. * * @ignore */ export declare class TargetedEventDispatcher extends EventDispatcher { /** * A target object which is originating events using this dispatcher. */ target: Target; /** * Constructor * * @param target Event dispatcher target */ constructor(target: Target); /** * Copies all dispatcher parameters, including listeners, from another event * dispatcher. * * @param source Source event dispatcher * @ignore */ copyFrom(source: this): IDisposer; } //# sourceMappingURL=EventDispatcher.d.ts.map