UNPKG

ste-core

Version:
121 lines (120 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DispatcherWrapper = void 0; /** * Hides the implementation of the event dispatcher. Will expose methods that * are relevent to the event. * * @export * @class DispatcherWrapper * @implements {ISubscribable<TEventHandler>} * @template TEventHandler The type of event handler. */ class DispatcherWrapper { /** * Creates an instance of DispatcherWrapper. * @param {ISubscribable<TEventHandler>} dispatcher * * @memberOf DispatcherWrapper */ constructor(dispatcher) { this._subscribe = (fn) => dispatcher.subscribe(fn); this._unsubscribe = (fn) => dispatcher.unsubscribe(fn); this._one = (fn) => dispatcher.one(fn); this._has = (fn) => dispatcher.has(fn); this._clear = () => dispatcher.clear(); this._count = () => dispatcher.count; this._onSubscriptionChange = () => dispatcher.onSubscriptionChange; } /** * Triggered when subscriptions are changed (added or removed). * * @readonly * @type {ISubscribable<SubscriptionChangeEventHandler>} * @memberOf DispatcherWrapper */ get onSubscriptionChange() { return this._onSubscriptionChange(); } /** * Returns the number of subscriptions. * * @readonly * @type {number} * @memberOf DispatcherWrapper */ get count() { return this._count(); } /** * Subscribe to the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * @returns {() => void} A function that unsubscribes the event handler from the event. * * @memberOf DispatcherWrapper */ subscribe(fn) { return this._subscribe(fn); } /** * Subscribe to the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * @returns {() => void} A function that unsubscribes the event handler from the event. * * @memberOf DispatcherWrapper */ sub(fn) { return this.subscribe(fn); } /** * Unsubscribe from the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * * @memberOf DispatcherWrapper */ unsubscribe(fn) { this._unsubscribe(fn); } /** * Unsubscribe from the event dispatcher. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * * @memberOf DispatcherWrapper */ unsub(fn) { this.unsubscribe(fn); } /** * Subscribe once to the event with the specified name. * * @returns {() => void} A function that unsubscribes the event handler from the event. * * @memberOf DispatcherWrapper */ one(fn) { return this._one(fn); } /** * Checks it the event has a subscription for the specified handler. * * @param {TEventHandler} fn The event handler that is called when the event is dispatched. * * @memberOf DispatcherWrapper */ has(fn) { return this._has(fn); } /** * Clears all the subscriptions. * * @memberOf DispatcherWrapper */ clear() { this._clear(); } } exports.DispatcherWrapper = DispatcherWrapper;