import { ThrowableError, AsyncIteratorClass } from '@orpc/shared'; interface PublisherOptions { /** * Maximum number of events to buffer for async iterator subscribers. * * If the buffer exceeds this limit, the oldest event is dropped. * This prevents unbounded memory growth if consumers process events slowly. * * Set to: * - `0`: Disable buffering. Events must be consumed before the next one arrives. * - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters. * - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage. * * @default 100 */ maxBufferedEvents?: number; } interface PublisherSubscribeListenerOptions { /** * Resume from a specific event ID */ lastEventId?: string | undefined; /** * Triggered when an error occur */ onError?: (error: ThrowableError) => void; } interface PublisherSubscribeIteratorOptions extends Pick, Pick { /** * Abort signal, automatically unsubscribes on abort */ signal?: AbortSignal | undefined | null; } declare abstract class Publisher> { private readonly maxBufferedEvents; constructor(options?: PublisherOptions); /** * Publish an event to subscribers */ abstract publish(event: K, payload: T[K]): Promise; /** * Subscribes to a specific event using a callback function. * Returns an unsubscribe function to remove the listener. * * @remarks * This method should be protected to avoid conflicts with `subscribe` method */ protected abstract subscribeListener(event: K, listener: (payload: T[K]) => void, options?: PublisherSubscribeListenerOptions): Promise<() => Promise>; /** * Subscribes to a specific event using a callback function. * Returns an unsubscribe function to remove the listener. * * @example * ```ts * const unsubscribe = publisher.subscribe('event', (payload) => { * console.log(payload) * }, { * lastEventId, * onError: (error) => { * // handle error (consider unsubscribe if error can't be recovered) * } * }) * * // Later * unsubscribe() * ``` */ subscribe(event: K, listener: (payload: T[K]) => void, options?: PublisherSubscribeListenerOptions): Promise<() => Promise>; /** * Subscribes to a specific event using an async iterator. * Useful for `for await...of` loops with optional buffering and abort support. * * @example * ```ts * for await (const payload of publisher.subscribe('event', { signal, lastEventId })) { * console.log(payload) * } * ``` */ subscribe(event: K, options?: PublisherSubscribeIteratorOptions): AsyncIteratorClass; } export { Publisher }; export type { PublisherOptions, PublisherSubscribeIteratorOptions, PublisherSubscribeListenerOptions };