UNPKG

weak-event

Version:

C#-Style Typescript Events/Weak Events

108 lines 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.invokeEventHandlersAsync = exports.invokeEventHandlers = exports.eventHandlerSafeInvokeAsync = exports.eventHandlerSafeInvoke = void 0; const tslib_1 = require("tslib"); /** * Safely invokes the given event handler * * @export * @template TSender The type of the event's sender (source) * @template TArgs The type of the arguments provided to the event handler * @param {TypedEventHandler<TSender, TArgs>} handler The handler to invoke * @param {TSender} sender the 'sender' to provide to the handler * @param {TArgs} args The arguments to provide to the handler * @return {*} {EventInvocationResult} The handler invocation result */ function eventHandlerSafeInvoke(handler, sender, args) { try { handler(sender, args); return { succeeded: true }; } catch (error) { return { error, succeeded: false }; } } exports.eventHandlerSafeInvoke = eventHandlerSafeInvoke; /** * Safely and asynchronously invokes the given event handler * * @export * @template TSender The type of the event's sender (source) * @template TArgs The type of the arguments provided to the event handler * @param {TypedEventHandler<TSender, TArgs>} handler The handler to invoke * @param {TSender} sender the 'sender' to provide to the handler * @param {TArgs} args The arguments to provide to the handler * @return {*} {Promise<EventInvocationResult>} A promise that is fulfilled when the handler execution concluded. * Contains a boolean specifying whether the invocation was successful and, if not, what the error was. */ function eventHandlerSafeInvokeAsync(handler, sender, args) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { try { yield handler(sender, args); return { succeeded: true }; } catch (error) { return { error, succeeded: false }; } }); } exports.eventHandlerSafeInvokeAsync = eventHandlerSafeInvokeAsync; /** * Invokes all given event handlers in accordance with the given `EventInvocationOpts` * * @export * @template TSender The type of the event's sender (source) * @template TArgs The type of the arguments provided to the event handlers * @param {Iterable<TypedEventHandler<TSender, TArgs>>} handlers * @param {TSender} sender the 'sender' to provide to the handlers * @param {TArgs} args The arguments to provide to the handlers * @param {EventInvocationOpts} options Options to control the exact behavior of the invocation */ function invokeEventHandlers(handlers, sender, args, options) { for (const handler of handlers) { const { succeeded, error } = eventHandlerSafeInvoke(handler, sender, args); if (!succeeded && (options === null || options === void 0 ? void 0 : options.swallowExceptions) !== true) { throw error; } } } exports.invokeEventHandlers = invokeEventHandlers; /** * Asynchronously invokes all given event handlers in accordance with the given `EventInvocationOpts` * * @export * @template TSender The type of the event's sender (source) * @template TArgs The type of the arguments provided to the event handlers * @param {Iterable<TypedEventHandler<TSender, TArgs>>} handlers * @param {TSender} sender the 'sender' to provide to the handlers * @param {TArgs} args The arguments to provide to the handlers * @param {EventInvocationOpts} options Options to control the exact behavior of the invocation * @return {*} {Promise<void>} A Promise that is fulfilled when all event handlers have been invoked */ function invokeEventHandlersAsync(handlers, sender, args, options) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if ((options === null || options === void 0 ? void 0 : options.parallelize) === false) { for (const handler of handlers) { // eslint-disable-next-line no-await-in-loop const { succeeded, error } = yield eventHandlerSafeInvokeAsync(handler, sender, args); if (!succeeded && (options === null || options === void 0 ? void 0 : options.swallowExceptions) !== true) { throw error; } } } else { const handlerPromises = []; for (const handler of handlers) { // Otherwise, invoke them asynchronously and stop on failure (if required) handlerPromises.push(eventHandlerSafeInvokeAsync(handler, sender, args).then(({ succeeded, error }) => { if (!succeeded && (options === null || options === void 0 ? void 0 : options.swallowExceptions) !== true) { throw error; } })); } yield Promise.all(handlerPromises); } }); } exports.invokeEventHandlersAsync = invokeEventHandlersAsync; //# sourceMappingURL=typed-event-functional.js.map