UNPKG

weak-event

Version:

C#-Style Typescript Events/Weak Events

50 lines 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypedEvent = void 0; const tslib_1 = require("tslib"); const typed_event_functional_1 = require("./typed-event-functional"); /** * The default invocation options for the `WeakEvent` class. * @type {*} */ const DEFAULT_INVOCATION_OPTS = { swallowExceptions: false, parallelize: true }; /** * A basic, typed event * * @export * @class TypedEvent * @implements {IEventSource<TSender, TArgs>} * @param {TSender} sender The event's source (usually the particular instance raising the event) * as should be provided to the event handlers * @param {TArgs} args The arguments to provide to the handlers */ class TypedEvent { constructor() { this._handlers = []; } attach(handler) { this._handlers.push(handler); } detach(handler) { this.tryRemoveHandler(handler); } invoke(sender, args, options = DEFAULT_INVOCATION_OPTS) { (0, typed_event_functional_1.invokeEventHandlers)(this._handlers, sender, args, options); } invokeAsync(sender, args, options = DEFAULT_INVOCATION_OPTS) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { yield (0, typed_event_functional_1.invokeEventHandlersAsync)(this._handlers, sender, args, options); }); } tryRemoveHandler(handlerToRemove) { const handlerIdx = this._handlers.findIndex((handler) => handler === handlerToRemove); if (handlerIdx >= 0) { this._handlers.splice(handlerIdx, 1); } } } exports.TypedEvent = TypedEvent; //# sourceMappingURL=base-typed-event.js.map