UNPKG

ste-promise-simple-events

Version:

Add the power of Simple Events to your projects. Every event has an argument with its data. Every handler can be a promise.

52 lines (51 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromiseSimpleEventDispatcher = void 0; const ste_core_1 = require("ste-core"); /** * The dispatcher handles the storage of subsciptions and facilitates * subscription, unsubscription and dispatching of a simple event * * @export * @class PromiseSimpleEventDispatcher * @extends {PromiseDispatcherBase<IPromiseSimpleEventHandler<TArgs>>} * @implements {IPromiseSimpleEvent<TArgs>} * @template TArgs */ class PromiseSimpleEventDispatcher extends ste_core_1.PromiseDispatcherBase { /** * Creates a new SimpleEventDispatcher instance. */ constructor() { super(); } /** * Dispatches the event. * @param args The arguments object. * @returns {IPropagationStatus} The status of the dispatch. * @memberOf PromiseSimpleEventDispatcher */ async dispatch(args) { const result = await this._dispatchAsPromise(false, this, arguments); if (result == null) { throw new ste_core_1.DispatchError("Got `null` back from dispatch."); } return result; } /** * Dispatches the event without waiting for it to complete. * @param args The argument object. * @memberOf PromiseSimpleEventDispatcher */ dispatchAsync(args) { this._dispatchAsPromise(true, this, arguments); } /** * Creates an event from the dispatcher. Will return the dispatcher * in a wrapper. This will prevent exposure of any dispatcher methods. */ asEvent() { return super.asEvent(); } } exports.PromiseSimpleEventDispatcher = PromiseSimpleEventDispatcher;