import { Observable } from "./observable"; /** * @category JDOM */ export type EventHandler = (...args: any[]) => void; /** * Base interface for evented nodes in Jacdac * @category JDOM */ export interface IEventSource { readonly nodeId: number; changeId: number; /** * Subscribes to an event and returns the unsubscription handler * @param eventName * @param next */ subscribe(eventName: string | string[], next: (value: T) => void): () => void; } /** * Given a node or set of nodes, generate a stable string that can be used to track dependencies in frameworks like React. * @param nodes * @returns * @category JDOM */ export declare function dependencyId(nodes: IEventSource | IEventSource[]): string | number; /** * Collects unsubscribe handlers and unmounts them */ export declare class JDCancellationToken { private subscriptions; constructor(); mount(...items: (() => void)[]): void; unmount(): void; } /** * Base class for evented nodes in Jacdac * @category JDOM */ export declare class JDEventSource implements IEventSource { /** * Gets an internal unique node identifier, mostly used for debugging. * @category JDOM */ readonly nodeId: number; private readonly listeners; /** * Gets a counter of event emit calls. * @category JDOM */ readonly eventStats: Record; /** * Gets a counter map from events to new listener counts * @category JDOM */ newListenerStats: Record; /** * @internal */ constructor(); /** * Registers a handler for one or more events * @param eventName name or names of the events to subscribe * @param handler handler to register * @returns current object instance * @category JDOM */ on(eventName: string | string[], handler: EventHandler): this; /** * Unregisters a handler for one or more events * @param eventName name or names of the events to subscribe * @param handler handler to unregister * @returns current object instance * @category JDOM */ off(eventName: string | string[], handler: EventHandler): this; /** * Registers a handler for one or more events to run only once. * @param eventName name or names of the events to subscribe * @param handler handler to execute * @returns current object instance * @category JDOM */ once(eventName: string | string[], handler: EventHandler): this; /** * Awaits an event with a timeout. Throws JacdacError with timeout if operation does not return. * @param eventName * @param timeout */ awaitOnce(eventName: string | string[], token?: JDCancellationToken): Promise; private addListenerInternal; private removeListenerInternal; /** * Synchronously calls each of the listeners registered for the event named eventName, * in the order they were registered, passing the supplied arguments to each. * @param eventName * @param args * @category JDOM */ emit(eventName: string, ...args: unknown[]): boolean; /** * Gets the number of listeners for a given event * @param eventName name of the event * @returns number of registered handlers * @category JDOM */ listenerCount(eventName: string): number; /** * Gets the list stack trace where an event was registered. Only enabled if ``Flags.debug`` is true. * @param eventName name of the event * @returns stack traces where a listener was added * @category JDOM */ listenerStackTraces(eventName: string): string[]; /** * Returns an array listing the events for which the emitter has registered listeners. * @category JDOM */ eventNames(): string[]; /** * Creates an observable from the given event * @param eventName * @category JDOM */ observe(eventName: string | string[]): Observable; /** * Subscribes to an event and returns the unsubscription handler * @param eventName * @param next * @category JDOM */ subscribe(eventName: string | string[], next: (value: T) => void): () => void; /** * Gets a counter for the ``CHANGE`` event. * @category JDOM */ get changeId(): number; } export declare class JDSubscriptionScope { private unsubscribers; private _unmounted; get unmounted(): boolean; mount(unsubscribe: () => void): () => void; unmount(): void; } //# sourceMappingURL=eventsource.d.ts.map