import { constructAbstractClass, invalidOperation, } from "../../../../utils/internal/exceptions.mjs"; /** * @internal */ export abstract class ImmutableIterator implements Iterable { #changes = 0; constructor() { if (new.target === ImmutableIterator) { return constructAbstractClass("ImmutableIterator"); } Object.freeze(this); } *[Symbol.iterator](): Iterator { const initChanges = this.#changes; const iterator = this.getIterator(); for ( let iteration = iterator.next(); !iteration.done; iteration = iterator.next() ) { yield iteration.value; if (this.#changes !== initChanges) { return invalidOperation("Container modified in iteration."); } } } /** * @internal */ protected abstract getIterator(): Iterator; /** * @internal */ protected markAsChanged(): void { this.#changes++; } }