Function callbackAdapter

  • A non-currying variant of 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.

    Returns

    An IterableQueue populated with values from the provided factory function.

    Example

    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");

    Type Parameters

    • U extends unknown[]

      Tuple type representing the values passed to the queue.

    Parameters

    • factory: FactoryFn<undefined, U>

      A function that sets up the event listeners or other mechanisms to pass values to the queue.

    • Optional cleanup: CleanupFn<undefined, U>

      An optional function that performs cleanup operations when the queue is sealed.

    Returns IterableQueue<U>

Generated using TypeDoc