import { IterableX } from './iterablex.js'; class DeferIterable extends IterableX { private _fn: () => Iterable; constructor(fn: () => Iterable) { super(); this._fn = fn; } *[Symbol.iterator]() { for (const item of this._fn()) { yield item; } } } /** * Returns an iterable sequence that invokes the specified factory function whenever a call to [Symbol.iterator] has been made. * * @template TSource The type of the elements in the sequence returned by the factory function, and in the resulting sequence. * @param {(() => Iterable)} factory iterable factory function to invoke for each call to [Symbol.iterator]. * @returns {AsyncIterableX} An iterable sequence whose observers trigger an invocation of the given iterable factory function. */ export function defer(factory: () => Iterable): IterableX { return new DeferIterable(factory); }