import { MuxAsyncIterator } from "../async/mux_async_iterator.ts";
import { deferred } from "../async/deferred.ts";
export function signal(...signals) {
    const mux = new MuxAsyncIterator();
    if (signals.length < 1) {
        throw new Error("No signals are given. You need to specify at least one signal to create a signal stream.");
    }
    const streams = signals.map(createSignalStream);
    streams.forEach((stream) => {
        mux.add(stream);
    });
    const dispose = () => {
        streams.forEach((stream) => {
            stream.dispose();
        });
    };
    return Object.assign(mux, { dispose });
}
function createSignalStream(signal) {
    let streamContinues = deferred();
    const handler = () => {
        streamContinues.resolve(true);
    };
    Deno.addSignalListener(signal, handler);
    const gen = async function* () {
        while (await streamContinues) {
            streamContinues = deferred();
            yield undefined;
        }
    };
    return Object.assign(gen(), {
        dispose() {
            streamContinues.resolve(false);
            Deno.removeSignalListener(signal, handler);
        },
    });
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 4564 characters]