import { assert } from "../../../shared/src/asserts.js";
import { must } from "../../../shared/src/must.js";
//#region ../zql/src/ivm/union-fan-out.ts
var UnionFanOut = class {
	#destroyCount = 0;
	#unionFanIn;
	#input;
	#outputs = [];
	constructor(input) {
		this.#input = input;
		input.setOutput(this);
	}
	setFanIn(fanIn) {
		assert(!this.#unionFanIn, "FanIn already set for this FanOut");
		this.#unionFanIn = fanIn;
	}
	*push(change) {
		must(this.#unionFanIn).fanOutStartedPushing();
		for (const output of this.#outputs) yield* output.push(change, this);
		yield* must(this.#unionFanIn).fanOutDonePushing(change.type);
	}
	setOutput(output) {
		this.#outputs.push(output);
	}
	getSchema() {
		return this.#input.getSchema();
	}
	fetch(req) {
		return this.#input.fetch(req);
	}
	destroy() {
		if (this.#destroyCount < this.#outputs.length) {
			++this.#destroyCount;
			if (this.#destroyCount === this.#outputs.length) this.#input.destroy();
		} else throw new Error("FanOut already destroyed once for each output");
	}
};
//#endregion
export { UnionFanOut };

//# sourceMappingURL=union-fan-out.js.map