UNPKG

4.83 kBTypeScriptView Raw
1import { Disposable, DisposableGroup } from './disposable';
2import { MaybePromise } from './types';
3/**
4 * Represents a typed event.
5 */
6export interface Event<T> {
7 /**
8 *
9 * @param listener The listener function will be call when the event happens.
10 * @param thisArgs The 'this' which will be used when calling the event listener.
11 * @param disposables An array to which a {{IDisposable}} will be added.
12 * @return a disposable to remove the listener again.
13 */
14 (listener: (e: T) => any, thisArgs?: any, disposables?: DisposableGroup): Disposable;
15}
16export declare namespace Event {
17 function getMaxListeners(event: Event<unknown>): number;
18 function setMaxListeners<N extends number>(event: Event<unknown>, maxListeners: N): N;
19 function addMaxListeners(event: Event<unknown>, add: number): number;
20 const None: Event<any>;
21 /**
22 * Given an event, returns another event which only fires once.
23 */
24 function once<T>(event: Event<T>): Event<T>;
25 function toPromise<T>(event: Event<T>): Promise<T>;
26 function filter<T>(event: Event<T>, predicate: (e: T) => unknown): Event<T>;
27 function filter<T, S extends T>(event: Event<T>, predicate: (e: T) => e is S): Event<S>;
28 /**
29 * Given an event and a `map` function, returns another event which maps each element
30 * through the mapping function.
31 */
32 function map<I, O>(event: Event<I>, mapFunc: (i: I) => O): Event<O>;
33 /**
34 * Given a collection of events, returns a single event which emits whenever any of the provided events emit.
35 */
36 function any<T>(...events: Event<T>[]): Event<T>;
37 function any(...events: Event<any>[]): Event<void>;
38}
39type Callback = (...args: any[]) => any;
40declare class CallbackList implements Iterable<Callback> {
41 private _callbacks;
42 private _contexts;
43 get length(): number;
44 add(callback: Function, context?: any, bucket?: Disposable[]): void;
45 remove(callback: Function, context?: any): void;
46 [Symbol.iterator](): IterableIterator<(...args: any[]) => any>;
47 invoke(...args: any[]): any[];
48 isEmpty(): boolean;
49 dispose(): void;
50}
51export interface EmitterOptions {
52 onFirstListenerAdd?: Function;
53 onLastListenerRemove?: Function;
54}
55export declare class Emitter<T = any> {
56 private _options?;
57 private static LEAK_WARNING_THRESHHOLD;
58 private static _noop;
59 private _event;
60 protected _callbacks: CallbackList | undefined;
61 private _disposed;
62 private _leakingStacks;
63 private _leakWarnCountdown;
64 constructor(_options?: EmitterOptions | undefined);
65 /**
66 * For the public to allow to subscribe
67 * to events from this Emitter
68 */
69 get event(): Event<T>;
70 protected checkMaxListeners(maxListeners: number): (() => void) | undefined;
71 protected pushLeakingStack(): () => void;
72 protected popLeakingStack(stack: string): void;
73 /**
74 * To be kept private to fire an event to
75 * subscribers
76 */
77 fire(event: T): any;
78 /**
79 * Process each listener one by one.
80 * Return `false` to stop iterating over the listeners, `true` to continue.
81 */
82 sequence(processor: (listener: (e: T) => any) => MaybePromise<boolean>): Promise<void>;
83 dispose(): void;
84}
85export type WaitUntilData<T> = Omit<T, 'waitUntil' | 'token'>;
86export interface WaitUntilEvent {
87 /**
88 * A cancellation token.
89 */
90 token: CancellationToken;
91 /**
92 * Allows to pause the event loop until the provided thenable resolved.
93 *
94 * *Note:* It can only be called during event dispatch and not in an asynchronous manner
95 *
96 * @param thenable A thenable that delays execution.
97 */
98 waitUntil(thenable: Promise<any>): void;
99}
100export declare namespace WaitUntilEvent {
101 /**
102 * Fire all listeners in the same tick.
103 *
104 * Use `AsyncEmitter.fire` to fire listeners async one after another.
105 */
106 function fire<T extends WaitUntilEvent>(emitter: Emitter<T>, event: WaitUntilData<T>, timeout?: number, token?: CancellationToken): Promise<void>;
107}
108import { CancellationToken } from './cancellation';
109export declare class AsyncEmitter<T extends WaitUntilEvent> extends Emitter<T> {
110 protected deliveryQueue: Promise<void> | undefined;
111 /**
112 * Fire listeners async one after another.
113 */
114 fire(event: WaitUntilData<T>, token?: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
115 protected deliver(listeners: Callback[], event: WaitUntilData<T>, token: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
116}
117export declare class QueueableEmitter<T> extends Emitter<T[]> {
118 currentQueue?: T[];
119 queue(...arg: T[]): void;
120 fire(): void;
121}
122export {};
123//# sourceMappingURL=event.d.ts.map
\No newline at end of file