@difizen/mana-common
Version:
75 lines • 2.52 kB
TypeScript
import { Disposable } from './disposable';
import { LinkedList } from './linkedList';
import type { MaybePromise } from './types';
/**
* Represents a typed event.
*/
export type Event<T> = {
/**
*
* @param listener The listener function will be call when the event happens.
* @param context The 'this' which will be used when calling the event listener.
* @return a disposable to remove the listener again.
*/
(listener: (e: T) => any, context?: any): Disposable;
};
export declare namespace Event {
const None: Event<void>;
/**
* 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>;
}
type Callback = (...args: any[]) => any;
export declare class CallbackList implements Iterable<Callback> {
protected _callbacks: [Callback, any][] | undefined;
get length(): number;
add(callback: Callback, context?: any): void;
remove(callback: Callback, context?: any): void;
[Symbol.iterator](): IterableIterator<(...args: any[]) => any>;
invoke(...args: any[]): any[];
isEmpty(): boolean;
dispose(): void;
}
export type EmitterOptions = {
onFirstListenerAdd?: (ctx: any) => void;
onLastListenerRemove?: (ctx: any) => void;
};
export declare class Emitter<T = any> {
protected _event?: Event<T>;
protected _callbacks: CallbackList | undefined;
protected _disposed: boolean;
protected _options?: EmitterOptions | undefined;
constructor(_options?: EmitterOptions);
/**
* For the public to allow to subscribe
* to events from this Emitter
*/
get event(): Event<T>;
/**
* To be kept protected 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 declare class PauseableEmitter<T> extends Emitter<T> {
protected _isPaused: number;
protected _eventQueue: LinkedList<T>;
protected _mergeFn: undefined | ((input: T[]) => T);
protected _listeners: any;
constructor(options?: EmitterOptions & {
merge?: (input: T[]) => T;
});
pause(): void;
resume(): void;
fire(event: T): void;
}
export {};
//# sourceMappingURL=event.d.ts.map