UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

139 lines • 5.59 kB
import { Disposable, DisposableGroup } from './disposable'; import { MaybePromise } from './types'; /** * Represents a typed event. */ export interface Event<T> { /** * * @param listener The listener function will be call when the event happens. * @param thisArgs The 'this' which will be used when calling the event listener. * @param disposables An array to which a {{IDisposable}} will be added. * @return a disposable to remove the listener again. */ (listener: (e: T) => any, thisArgs?: any, disposables?: DisposableGroup): Disposable; } export declare namespace Event { function getMaxListeners(event: Event<unknown>): number; function setMaxListeners<N extends number>(event: Event<unknown>, maxListeners: N): N; function addMaxListeners(event: Event<unknown>, add: number): number; const None: Event<any>; /** * Given an event, returns another event which only fires once. */ function once<T>(event: Event<T>): Event<T>; function toPromise<T>(event: Event<T>): Promise<T>; function filter<T>(event: Event<T>, predicate: (e: T) => unknown): Event<T>; function filter<T, S extends T>(event: Event<T>, predicate: (e: T) => e is S): Event<S>; /** * Given an event and a `map` function, returns another event which maps each element * through the mapping function. */ function map<I, O>(event: Event<I>, mapFunc: (i: I) => O): Event<O>; /** * Given a collection of events, returns a single event which emits whenever any of the provided events emit. */ function any<T>(...events: Event<T>[]): Event<T>; function any(...events: Event<any>[]): Event<void>; } type Callback = (...args: any[]) => any; declare class CallbackList implements Iterable<Callback> { private readonly errorHandling; private _callbacks; private _contexts; constructor(errorHandling?: ErrorHandlingStrategy); get length(): number; add(callback: Function, context?: any, bucket?: Disposable[]): void; remove(callback: Function, context?: any): void; [Symbol.iterator](): ArrayIterator<(...args: any[]) => any>; invoke(...args: any[]): any[]; isEmpty(): boolean; dispose(): void; } /** * A strategy for handling errors in firing an emitter's event, one of * * - `'log'` (default): errors are caught and logged via `console.error` * - `'propagate'`: all listeners are called; if any throw, the errors are collected * and re-thrown after all listeners complete (single error re-thrown directly, * multiple errors wrapped in an `AggregateError`) * - `(error: unknown) => void`: a custom callback invoked for each error */ export type ErrorHandlingStrategy = 'log' | 'propagate' | ((error: unknown) => void); export interface EmitterOptions { onFirstListenerAdd?: Function; onLastListenerRemove?: Function; /** * How errors thrown by event listeners are handled during {@link Emitter.fire}. */ errorHandling?: ErrorHandlingStrategy; } export declare class Emitter<T = any> { private _options?; private static LEAK_WARNING_THRESHHOLD; private static _noop; private _event; protected _callbacks: CallbackList | undefined; private _disposed; private _leakingStacks; private _leakWarnCountdown; constructor(_options?: EmitterOptions | undefined); /** * For the public to allow to subscribe * to events from this Emitter */ get event(): Event<T>; protected checkMaxListeners(maxListeners: number): (() => void) | undefined; protected pushLeakingStack(): () => void; protected popLeakingStack(stack: string): void; /** * To be kept private to fire an event to * subscribers */ fire(event: T): any; /** * Process each listener one by one. * Return `false` to stop iterating over the listeners, `true` to continue. */ sequence(processor: (listener: (e: T) => any) => MaybePromise<boolean>): Promise<void>; dispose(): void; } export type WaitUntilData<T> = Omit<T, 'waitUntil' | 'token'>; export interface WaitUntilEvent { /** * A cancellation token. */ token: CancellationToken; /** * Allows to pause the event loop until the provided thenable resolved. * * *Note:* It can only be called during event dispatch and not in an asynchronous manner * * @param thenable A thenable that delays execution. */ waitUntil(thenable: Promise<any>): void; } export declare namespace WaitUntilEvent { /** * Fire all listeners in the same tick. * * Use `AsyncEmitter.fire` to fire listeners async one after another. */ function fire<T extends WaitUntilEvent>(emitter: Emitter<T>, event: WaitUntilData<T>, timeout?: number, token?: CancellationToken): Promise<void>; } import { CancellationToken } from './cancellation'; export declare class AsyncEmitter<T extends WaitUntilEvent> extends Emitter<T> { protected deliveryQueue: Promise<void> | undefined; /** * Fire listeners async one after another. */ fire(event: WaitUntilData<T>, token?: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>; protected deliver(listeners: Callback[], event: WaitUntilData<T>, token: CancellationToken, promiseJoin?: (p: Promise<any>, listener: Function) => Promise<any>): Promise<void>; } export declare class QueueableEmitter<T> extends Emitter<T[]> { currentQueue?: T[]; queue(...arg: T[]): void; fire(): void; } export {}; //# sourceMappingURL=event.d.ts.map