UNPKG

ste-core

Version:
79 lines (78 loc) 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromiseDispatcherBase = void 0; const PromiseSubscription_1 = require("../events/PromiseSubscription"); const EventManagement_1 = require("../management/EventManagement"); const DispatcherBase_1 = require("./DispatcherBase"); const DispatchError_1 = require("./DispatchError"); /** * Dispatcher base for dispatchers that use promises. Each promise * is awaited before the next is dispatched, unless the event is * dispatched with the executeAsync flag. * * @export * @abstract * @class PromiseDispatcherBase * @extends {DispatcherBase<TEventHandler>} * @template TEventHandler The type of event handler. */ class PromiseDispatcherBase extends DispatcherBase_1.DispatcherBase { /** * The normal dispatch cannot be used in this class. * * @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 */ _dispatch(executeAsync, scope, args) { throw new DispatchError_1.DispatchError("_dispatch not supported. Use _dispatchAsPromise."); } /** * Crates a new subscription. * * @protected * @param {TEventHandler} handler The handler. * @param {boolean} isOnce Indicates if the handler should only run once. * @returns {ISubscription<TEventHandler>} The subscription. * * @memberOf PromiseDispatcherBase */ createSubscription(handler, isOnce) { return new PromiseSubscription_1.PromiseSubscription(handler, isOnce); } /** * 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 */ async _dispatchAsPromise(executeAsync, scope, args) { //execute on a copy because of bug #9 for (let sub of [...this._subscriptions]) { let ev = new EventManagement_1.EventManagement(() => this.unsub(sub.handler)); let nargs = Array.prototype.slice.call(args); nargs.push(ev); let ps = sub; await ps.execute(executeAsync, scope, nargs); //cleanup subs that are no longer needed this.cleanup(sub); if (!executeAsync && ev.propagationStopped) { return { propagationStopped: true }; } } if (executeAsync) { return null; } return { propagationStopped: false }; } } exports.PromiseDispatcherBase = PromiseDispatcherBase;