UNPKG

3.95 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 interface WaitUntilEvent {
74 /**
75 * Allows to pause the event loop until the provided thenable resolved.
76 *
77 * *Note:* It can only be called during event dispatch and not in an asynchronous manner
78 *
79 * @param thenable A thenable that delays execution.
80 */
81 waitUntil(thenable: Promise<any>): void;
82}
83export declare namespace WaitUntilEvent {
84 /**
85 * Fire all listeners in the same tick.
86 *
87 * Use `AsyncEmitter.fire` to fire listeners async one after another.
88 */
89 function fire<T extends WaitUntilEvent>(emitter: Emitter<T>, event: Omit<T, 'waitUntil'>, timeout?: number | undefined): Promise<void>;
90}
91import { CancellationToken } from './cancellation';
92export declare class AsyncEmitter<T extends WaitUntilEvent> extends Emitter<T> {
93 protected deliveryQueue: Promise<void> | undefined;
94 /**
95 * Fire listeners async one after another.
96 */
97 fire(event: Omit<T, 'waitUntil'>, token?: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
98 protected deliver(listeners: Callback[], event: Omit<T, 'waitUntil'>, token: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>;
99}
100export {};
101//# sourceMappingURL=event.d.ts.map
\No newline at end of file