import { assert } from "../../../shared/src/asserts.js";
import { must } from "../../../shared/src/must.js";
export class UnionFanOut {
    #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) {
            output.push(change, this);
        }
        must(this.#unionFanIn).fanOutDonePushing(change.type);
    }
    setOutput(output) {
        this.#outputs.push(output);
    }
    getSchema() {
        return this.#input.getSchema();
    }
    fetch(req) {
        return this.#input.fetch(req);
    }
    cleanup(_req) {
        // Cleanup is going away. Not implemented.
        return [];
    }
    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');
        }
    }
}
//# sourceMappingURL=union-fan-out.js.map