UNPKG

4.53 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 /**
27 * Given an event and a `map` function, returns another event which maps each element
28 * through the mapping function.
29 */
30 function map<I, O>(event: Event<I>, mapFunc: (i: I) => O): Event<O>;
31 /**
32 * Given a collection of events, returns a single event which emits whenever any of the provided events emit.
33 */
34 function any<T>(...events: Event<T>[]): Event<T>;
35 function any(...events: Event<any>[]): Event<void>;
36}
37declare type Callback = (...args: any[]) => any;
38declare class CallbackList implements Iterable<Callback> {
39 private _callbacks;
40 private _contexts;
41 get length(): number;
42 add(callback: Function, context?: any, bucket?: Disposable[]): void;
43 remove(callback: Function, context?: any): void;
44 [Symbol.iterator](): IterableIterator<(...args: any[]) => any>;
45 invoke(...args: any[]): any[];
46 isEmpty(): boolean;
47 dispose(): void;
48}
49export interface EmitterOptions {
50 onFirstListenerAdd?: Function;
51 onLastListenerRemove?: Function;
52}
53export declare class Emitter<T = any> {
54 private _options?;
55 private static LEAK_WARNING_THRESHHOLD;
56 private static _noop;
57 private _event;
58 protected _callbacks: CallbackList | undefined;
59 private _disposed;
60 private _leakingStacks;
61 private _leakWarnCountdown;
62 constructor(_options?: EmitterOptions | undefined);
63 /**
64 * For the public to allow to subscribe
65 * to events from this Emitter
66 */
67 get event(): Event<T>;
68 protected checkMaxListeners(maxListeners: number): (() => void) | undefined;
69 protected pushLeakingStack(): () => void;
70 protected popLeakingStack(stack: string): void;
71 /**
72 * To be kept private to fire an event to
73 * subscribers
74 */
75 fire(event: T): any;
76 /**
77 * Process each listener one by one.
78 * Return `false` to stop iterating over the listeners, `true` to continue.
79 */
80 sequence(processor: (listener: (e: T) => any) => MaybePromise<boolean>): Promise<void>;
81 dispose(): void;
82}
83export declare type WaitUntilData<T> = Omit<T, 'waitUntil' | 'token'>;
84export interface WaitUntilEvent {
85 /**
86 * A cancellation token.
87 */
88 token: CancellationToken;
89 /**
90 * Allows to pause the event loop until the provided thenable resolved.
91 *
92 * *Note:* It can only be called during event dispatch and not in an asynchronous manner
93 *
94 * @param thenable A thenable that delays execution.
95 */
96 waitUntil(thenable: Promise<any>): void;
97}
98export declare namespace WaitUntilEvent {
99 /**
100 * Fire all listeners in the same tick.
101 *
102 * Use `AsyncEmitter.fire` to fire listeners async one after another.
103 */
104 function fire<T extends WaitUntilEvent>(emitter: Emitter<T>, event: WaitUntilData<T>, timeout?: number, token?: CancellationToken): Promise<void>;
105}
106import { CancellationToken } from './cancellation';
107export declare class AsyncEmitter<T extends WaitUntilEvent> extends Emitter<T> {
108 protected deliveryQueue: Promise<void> | undefined;
109 /**
110 * Fire listeners async one after another.
111 */
112 fire(event: WaitUntilData<T>, token?: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
113 protected deliver(listeners: Callback[], event: WaitUntilData<T>, token: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
114}
115export {};
116//# sourceMappingURL=event.d.ts.map
\No newline at end of file