ste-promise-events
Version:
Add the power of Events to your projects. They are styled after .Net using a sender and arguments. Handlers can be promises.
59 lines (58 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromiseEventDispatcher = void 0;
const ste_core_1 = require("ste-core");
/**
* Dispatcher implementation for events. Can be used to subscribe, unsubscribe
* or dispatch events. Use the ToEvent() method to expose the event.
*
* @export
* @class PromiseEventDispatcher
* @extends {PromiseDispatcherBase<IPromiseEventHandler<TSender, TArgs>>}
* @implements {IPromiseEvent<TSender, TArgs>}
* @template TSender
* @template TArgs
*/
class PromiseEventDispatcher extends ste_core_1.PromiseDispatcherBase {
/**
* Creates a new EventDispatcher instance.
*/
constructor() {
super();
}
/**
* Dispatches the event.
*
* @param {TSender} sender The sender object.
* @param {TArgs} args The argument object.
* @returns {Promise<IPropagationStatus>} The status.
*
* @memberOf PromiseEventDispatcher
*/
async dispatch(sender, 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 the result.
*
* @param {TSender} sender The sender object.
* @param {TArgs} args The argument object.
*
* @memberOf PromiseEventDispatcher
*/
dispatchAsync(sender, 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.PromiseEventDispatcher = PromiseEventDispatcher;