/** Cache the results of the iterator */ export class OnceIter implements Iterable { constructor(source: Iterable) { this.#iterator = source[Symbol.iterator]() } readonly #cache: T[] = []; readonly #iterator: Iterator *[Symbol.iterator](): Iterator { const cache = this.#cache const iterator = this.#iterator let i = 0 for (; ;) { if (i < cache.length) { for (let j = i; j < cache.length; j++, i++) { yield cache[j] } } const r = iterator.next() if (r.done) return cache.push(r.value) yield r.value i++ } } }