UNPKG

ste-core

Version:
168 lines (167 loc) 5.42 kB
import type { ISubscribable } from "../events/ISubscribable"; import type { ISubscription } from "../events/ISubscription"; import type { SubscriptionChangeEventHandler } from "./SubscriptionChangeEventHandler"; import type { IPropagationStatus } from "./IPropagationStatus"; /** * Base class for implementation of the dispatcher. It facilitates the subscribe * and unsubscribe methods based on generic handlers. The TEventType specifies * the type of event that should be exposed. Use the asEvent to expose the * dispatcher as event. * * @export * @abstract * @class DispatcherBase * @implements {ISubscribable<TEventHandler>} * @template TEventHandler The type of event handler. */ export declare abstract class DispatcherBase<TEventHandler> implements ISubscribable<TEventHandler> { private _wrap; private _onSubscriptionChange; /** * The subscriptions. * * @protected * * @memberOf DispatcherBase */ protected _subscriptions: ISubscription<TEventHandler>[]; /** * Returns the number of subscriptions. * * @readonly * @type {number} * @memberOf DispatcherBase */ get count(): number; /** * Triggered when subscriptions are changed (added or removed). * * @readonly * @type {ISubscribable<SubscriptionChangeEventHandler>} * @memberOf DispatcherBase */ get onSubscriptionChange(): ISubscribable<SubscriptionChangeEventHandler>; /** * Subscribe to the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * @returns A function that unsubscribes the event handler from the event. * * @memberOf DispatcherBase */ subscribe(fn: TEventHandler): () => void; /** * Subscribe to the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * @returns A function that unsubscribes the event handler from the event. * * @memberOf DispatcherBase */ sub(fn: TEventHandler): () => void; /** * Subscribe once to the event with the specified name. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * @returns A function that unsubscribes the event handler from the event. * * @memberOf DispatcherBase */ one(fn: TEventHandler): () => void; /** * Checks it the event has a subscription for the specified handler. * * @param {TEventHandler} fn The event handler. * * @memberOf DispatcherBase */ has(fn: TEventHandler): boolean; /** * Unsubscribes the handler from the dispatcher. * * @param {TEventHandler} fn The event handler. * * @memberOf DispatcherBase */ unsubscribe(fn: TEventHandler): void; /** * Unsubscribes the handler from the dispatcher. * * @param {TEventHandler} fn The event handler. * * @memberOf DispatcherBase */ unsub(fn: TEventHandler): void; /** * Generic dispatch will dispatch the handlers with the given arguments. * * @protected * @param {boolean} executeAsync `True` if the even should be executed async. * @param {*} scope The scope of the event. The scope becomes the `this` for handler. * @param {IArguments} args The arguments for the event. * @returns {(IPropagationStatus | null)} The propagation status, or if an `executeAsync` is used `null`. * * @memberOf DispatcherBase */ protected _dispatch(executeAsync: boolean, scope: any, args: IArguments): IPropagationStatus | null; /** * Creates a subscription. * * @protected * @param {TEventHandler} handler The handler. * @param {boolean} isOnce True if the handler should run only one. * @returns {ISubscription<TEventHandler>} The subscription. * * @memberOf DispatcherBase */ protected createSubscription(handler: TEventHandler, isOnce: boolean): ISubscription<TEventHandler>; /** * Cleans up subs that ran and should run only once. * * @protected * @param {ISubscription<TEventHandler>} sub The subscription. * * @memberOf DispatcherBase */ protected cleanup(sub: ISubscription<TEventHandler>): void; /** * Creates an event from the dispatcher. Will return the dispatcher * in a wrapper. This will prevent exposure of any dispatcher methods. * * @returns {ISubscribable<TEventHandler>} * * @memberOf DispatcherBase */ asEvent(): ISubscribable<TEventHandler>; /** * Clears the subscriptions. * * @memberOf DispatcherBase */ clear(): void; /** * Triggers the subscription change event. * * @private * * @memberOf DispatcherBase */ private triggerSubscriptionChange; } /** * Dispatcher for subscription changes. * * @export * @class SubscriptionChangeEventDispatcher * @extends {DispatcherBase<SubscriptionChangeEventHandler>} */ export declare class SubscriptionChangeEventDispatcher extends DispatcherBase<SubscriptionChangeEventHandler> { /** * Dispatches the event. * * @param {number} count The currrent number of subscriptions. * * @memberOf SubscriptionChangeEventDispatcher */ dispatch(count: number): void; }