UNPKG

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