UNPKG

2.04 kBTypeScriptView Raw
1type EventHooksMomento<T extends Record<keyof T, unknown[]>, _R> = {
2 __eventHooksMomentoBrand: never;
3};
4/**
5 * Event emitter which allows listeners to return a value.
6 *
7 * This is beneficial for the themes since it allows plugins to modify the HTML output
8 * without doing unsafe text replacement.
9 *
10 * Very simple event emitter class which collects the return values of its listeners.
11 *
12 * @example
13 * ```ts
14 * const x = new EventHooks<{ a: [string] }, string>()
15 * x.on('a', a => a.repeat(123)) // ok, returns a string
16 * x.on('b', console.log) // error, 'b' is not assignable to 'a'
17 * x.on('a' a => 1) // error, returns a number but expected a string
18 * ```
19 */
20export declare class EventHooks<T extends Record<keyof T, unknown[]>, R> {
21 private _listeners;
22 /**
23 * Starts listening to an event.
24 * @param event the event to listen to.
25 * @param listener function to be called when an this event is emitted.
26 * @param order optional order to insert this hook with.
27 */
28 on<K extends keyof T>(event: K, listener: (...args: T[K]) => R, order?: number): void;
29 /**
30 * Listens to a single occurrence of an event.
31 * @param event the event to listen to.
32 * @param listener function to be called when an this event is emitted.
33 * @param order optional order to insert this hook with.
34 */
35 once<K extends keyof T>(event: K, listener: (...args: T[K]) => R, order?: number): void;
36 /**
37 * Stops listening to an event.
38 * @param event the event to stop listening to.
39 * @param listener the function to remove from the listener array.
40 */
41 off<K extends keyof T>(event: K, listener: (...args: T[K]) => R): void;
42 /**
43 * Emits an event to all currently subscribed listeners.
44 * @param event the event to emit.
45 * @param args any arguments required for the event.
46 */
47 emit<K extends keyof T>(event: K, ...args: T[K]): R[];
48 saveMomento(): EventHooksMomento<T, R>;
49 restoreMomento(momento: EventHooksMomento<T, R>): void;
50}
51export {};
52
\No newline at end of file