UNPKG

1.24 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { wrapWithAbort } from './operators/withabort';
3import { throwIfAborted } from '../aborterror';
4class DeferAsyncIterable extends AsyncIterableX {
5 constructor(fn) {
6 super();
7 this._fn = fn;
8 }
9 async *[Symbol.asyncIterator](signal) {
10 throwIfAborted(signal);
11 const items = await this._fn(signal);
12 for await (const item of wrapWithAbort(items, signal)) {
13 yield item;
14 }
15 }
16}
17/**
18 * Returns an async-iterable sequence that invokes the specified factory function whenever a call to [Symbol.asyncIterator] has been made.
19 *
20 * @export
21 * @template TSource The type of the elements in the sequence returned by the factory function, and in the resulting sequence.
22 * @param {((signal?: AbortSignal) => AsyncIterable<TSource> | Promise<AsyncIterable<TSource>>)} factory Async-iterable factory function to
23 * invoke for each call to [Symbol.asyncIterator].
24 * @returns {AsyncIterableX<TSource>} An async-iterable sequence whose observers trigger an invocation of the given async-iterable factory function.
25 */
26export function defer(factory) {
27 return new DeferAsyncIterable(factory);
28}
29
30//# sourceMappingURL=defer.mjs.map