UNPKG

thorish

Version:

This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.

68 lines (67 loc) 2.62 kB
import type { AbortSignalArgs } from './types.ts'; /** * Combines the N passed async generators into a single generator which yields items in order, * including the index of the generator that emitted it. If no generators are passed, returns * immediately. * * This also supports merging synchronous generators, but always returns a {@link AsyncGenerator}. * * This returns all return values of the passed generators only once all are done. This does not * support the `TNext` template. */ export declare function combineAsyncGenerators<T, Y = void>(gen: (AsyncGenerator<T, Y, void> | Generator<T, Y, void>)[]): AsyncGenerator<{ index: number; value: T; }, Y[], void>; /** * Builds a {@link AsyncGenerator} which is the result of repeatedly calling the passed handler * function. * * Returns if a rejected {@link AbortSignal} promise is yielded, as generated by * {@link promiseForSignal} or {@link AbortSignal.prototype.throwIfAborted}. */ export declare function asyncGeneratorForHandler<T>(handler: () => Promise<T>, args?: AbortSignalArgs): AsyncGenerator<T, void, void>; /** * The return type of {@link asyncGeneratorQueue}. */ export type AsyncGeneratorQueueReturn<T, Y> = { generator: AsyncGenerator<T, Y, void>; /** * Push a value into the generator. */ push: (arg: T | Promise<T>) => void; /** * Mark the generator as done, optionally providing a return value. */ done: (arg: Y | Promise<Y>) => void; }; /** * Creates an async generator which emits values pushed into it. * * This a much simpler version of {@link WorkQueue}. */ export declare function asyncGeneratorQueue<T, Y = void>(): AsyncGeneratorQueueReturn<T, Y>; /** * Given a {@link AsyncGenerator}, provides a helper which returns 'clones' that will eventually * consume the underlying generator and cache the entire result. * * This should not be used for generators which never complete as every value will be cached here. */ export declare class AsyncGeneratorCache<T, Y> { private _knownValues; private _done; private doneValue; private pendingPromise; private gen; constructor(gen: AsyncGenerator<T, Y, void>); private waitFor; read(): AsyncGenerator<Awaited<T>, Awaited<Y> | undefined, unknown>; get done(): boolean; knownValues(): Readonly<T[]>; } /** * Helper which iterates over a {@link AsyncGenerator}. * * Allows relatively easy access to the return value. */ export declare function forEachAsync<T, TReturn, TNext>(async: AsyncGenerator<T, TReturn, TNext>, cb: (x: T) => Promise<TNext> | TNext): Promise<TReturn>;