@uwdata/mosaic-core
Version:
Scalable and extensible linked data views.
121 lines • 4.71 kB
TypeScript
type EventCallback<T = unknown> = (value: T) => void | Promise<unknown>;
interface DispatchEntry<T = unknown> {
callbacks: Set<EventCallback<T>>;
pending: Promise<unknown> | null;
queue: DispatchQueue<T>;
}
interface QueueNode<T = unknown> {
value: T;
next?: QueueNode<T> | null;
}
/**
* Event dispatcher supporting asynchronous updates. If an event handler
* callback returns a Promise, the dispatcher waits for all such Promises
* to settle before dispatching future events of the same type.
*/
export declare class AsyncDispatch<T> {
_callbacks: Map<string, DispatchEntry<T>>;
/**
* Create a new asynchronous dispatcher instance.
*/
constructor();
/**
* Add an event listener callback for the provided event type.
* @param type The event type.
* @param callback The event handler
* callback function to add. If the callback has already been
* added for the event type, this method has no effect.
*/
addEventListener(type: string, callback: EventCallback<T>): void;
/**
* Remove an event listener callback for the provided event type.
* @param type The event type.
* @param callback The event handler
* callback function to remove.
*/
removeEventListener(type: string, callback: EventCallback<T>): void;
/**
* Lifecycle method that returns the event value to emit.
* This default implementation simply returns the input value as-is.
* Subclasses may override this method to implement custom transformations
* prior to emitting an event value to all listeners.
* @param type The event type.
* @param value The event value.
* @returns The (possibly transformed) event value to emit.
*/
willEmit(type: string, value: T): T;
/**
* Lifecycle method that returns a filter function for updating the
* queue of unemitted event values prior to enqueueing a new value.
* This default implementation simply returns null, indicating that
* unknown other unemitted event values should be dropped (that is, all
* queued events are filtered).
* @param type The event type.
* @param value The new event value that will be enqueued.
* @returns A dispatch queue filter
* function, or null if all unemitted event values should be filtered.
*/
emitQueueFilter(_type: string, // eslint-disable-line @typescript-eslint/no-unused-vars
_value: unknown): ((value: unknown) => boolean | null) | null;
/**
* Cancel all unemitted event values for the given event type.
* @param type The event type.
*/
cancel(type: string): void;
/**
* Returns a promise that resolves when unknown pending updates complete for
* the event of the given type currently being processed. The Promise will
* resolve immediately if the queue for the given event type is empty.
* @param type The event type to wait for.
* @returns A pending event promise.
*/
pending(type: string): Promise<void>;
/**
* Emit an event value to listeners for the given event type.
* If a previous emit has not yet resolved, the event value
* will be queued to be emitted later.
* The actual event value given to listeners will be the result
* of passing the input value through the emitValue() method.
* @param type The event type.
* @param value The event value.
*/
emit(type: string, value: T): void;
}
/**
* Queue for managing unemitted event values.
*/
export declare class DispatchQueue<T = unknown> {
next: QueueNode<T> | null;
/**
* Create a new dispatch queue instance.
*/
constructor();
/**
* Clear the queue state of all event values.
*/
clear(): void;
/**
* Indicate if the queue is empty.
* @returns True if queue is empty, false otherwise.
*/
isEmpty(): boolean;
/**
* Add a new value to the queue, and optionally filter the
* current queue content in response.
* @param value The value to add.
* @param filter An optional filter
* function to apply to existing queue content. If unspecified
* or falsy, all previously queued values are removed. Otherwise,
* the provided function is applied to all queue entries. The
* entry is retained if the filter function returns a truthy value,
* otherwise the entry is removed.
*/
enqueue(value: T, filter?: ((value: T) => boolean | null) | null): void;
/**
* Remove and return the next queued event value.
* @returns The next event value in the queue.
*/
dequeue(): T | undefined;
}
export {};
//# sourceMappingURL=AsyncDispatch.d.ts.map