import { AsyncIterableX } from './asynciterablex.js'; import { throwIfAborted } from '../aborterror.js'; class AnonymousAsyncIterable extends AsyncIterableX { private _fn: (signal?: AbortSignal) => AsyncIterator | Promise>; constructor(fn: (signal?: AbortSignal) => AsyncIterator | Promise>) { super(); this._fn = fn; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const it = await this._fn(signal); let next: IteratorResult | undefined; while (!(next = await it.next()).done) { yield next.value; } } } /** * Creates a new iterable using the specified function implementing the members of AsyncIterable * * @template T The type of the elements returned by the enumerable sequence. * @param {((signal?: AbortSignal) => AsyncIterator | Promise>)} fn The function that creates the [Symbol.asyncIterator]() method * @returns {AsyncIterableX} A new async-iterable instance. */ export function create( fn: (signal?: AbortSignal) => AsyncIterator | Promise> ): AsyncIterableX { return new AnonymousAsyncIterable(fn); }