weak-event
Version:
C#-Style Typescript Events/Weak Events
62 lines (61 loc) • 3.38 kB
TypeScript
import { EventInvocationOpts, TypedEventHandler } from '../typed-event-interfaces';
/**
* The result of an event invocation.
*
* Since handlers are 'void', this type contains only whether the invocation succeeded
* and, if it has not, what the error was
*/
export declare type EventInvocationResult = {
succeeded: boolean;
error?: unknown;
};
/**
* 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
*/
export declare function eventHandlerSafeInvoke<TSender, TArgs>(handler: TypedEventHandler<TSender, TArgs>, sender: TSender, args: TArgs): EventInvocationResult;
/**
* 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.
*/
export declare function eventHandlerSafeInvokeAsync<TSender, TArgs>(handler: TypedEventHandler<TSender, TArgs>, sender: TSender, args: TArgs): Promise<EventInvocationResult>;
/**
* 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
*/
export declare function invokeEventHandlers<TSender, TArgs>(handlers: Iterable<TypedEventHandler<TSender, TArgs>>, sender: TSender, args: TArgs, options: EventInvocationOpts): void;
/**
* 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
*/
export declare function invokeEventHandlersAsync<TSender, TArgs>(handlers: Iterable<TypedEventHandler<TSender, TArgs>>, sender: TSender, args: TArgs, options: EventInvocationOpts): Promise<void>;