import type { IterableQueue } from "../../types"; /** * The context of the callback adapter. Contains functions that allow for passing values to the iterable and marking the end of an iterable. */ export interface CallbackAdapterContext { /** * Passes values to the iterable returned by the {@link callbackAdapter}. */ pass: (...values: T) => void; /** * Marks the end of the iterable returned by the {@link callbackAdapter}. */ kill: () => void; } export type FactoryFn = (context: CallbackAdapterContext, target: T) => unknown; export type CleanupFn = (context: CallbackAdapterContext, target: T) => unknown; /** * Creates an async iterable queue that is populated with values from a provided factory function. The factory function is passed a CallbackAdapterContext object, which contains methods to pass values to the queue and mark the end of the iterable. * The optional cleanup function can be used to perform cleanup operations when the queue is sealed. * * @group Adapters * @template T - Type of the input object. * @template U - Tuple type representing the values passed to the queue. * @param {FactoryFn} factory - A function that sets up the event listeners or other mechanisms to pass values to the queue. * @param {CleanupFn} [cleanup] - An optional function that performs cleanup operations when the queue is sealed. * @returns {(input: T) => IterableQueue} A function that takes an input object and returns an IterableQueue. * * @example * ```ts * const someTarget = new EventEmitter(); * const eventQueue = withCallbackAdapter( * (context, target) => target.on("someEvent", context.pass), * (context, target) => target.removeListener("someEvent", context.pass) * )(someTarget); * * (async () => { * for await (const value of eventQueue) { * console.log(value); // Logs event values as they are emitted * } * })(); * * // Emit some events * someTarget.emit("someEvent", "Hello"); * someTarget.emit("someEvent", "World"); * ``` */ export declare const withCallbackAdapter: (factory: FactoryFn, cleanup?: CleanupFn | undefined) => (input: T) => IterableQueue; /** * A non-currying variant of {@link withCallbackAdapter}. Creates an async iterable queue that is populated with values from a provided factory function without requiring an input object. The factory function is passed a CallbackAdapterContext object, which contains methods to pass values to the queue and mark the end of the iterable. * The optional cleanup function can be used to perform cleanup operations when the queue is sealed. * * @group Adapters * @template U - Tuple type representing the values passed to the queue. * @param {FactoryFn} factory - A function that sets up the event listeners or other mechanisms to pass values to the queue. * @param {CleanupFn} [cleanup] - An optional function that performs cleanup operations when the queue is sealed. * @returns {IterableQueue} An IterableQueue populated with values from the provided factory function. * * @example * ```ts * const someEmitter = new EventEmitter(); * const factory = (context: CallbackAdapterContext<[string]>) => { * someEmitter.on("someEvent", context.pass); * }; * const cleanup = (context: CallbackAdapterContext<[string]>) => { * someEmitter.removeListener("someEvent", context.pass); * }; * const eventQueue = callbackAdapter(factory, cleanup); * * (async () => { * for await (const value of eventQueue) { * console.log(value); // Logs event values as they are emitted * } * })(); * * // Emit some events * someEmitter.emit("someEvent", "Hello"); * someEmitter.emit("someEvent", "World"); * ``` */ export declare const callbackAdapter: (factory: FactoryFn, cleanup?: CleanupFn | undefined) => IterableQueue;