// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
 * Higher level API for dealing with OS signals.
 *
 * @module
 */ import { MuxAsyncIterator } from "../async/mux_async_iterator.ts";
import { deferred } from "../async/deferred.ts";
/**
 * Generates an AsyncIterable which can be awaited on for one or more signals.
 * `dispose()` can be called when you are finished waiting on the events.
 *
 * Example:
 *
 * ```ts
 *       import { signal } from "https://deno.land/std@$STD_VERSION/signal/mod.ts";
 *
 *       const sig = signal("SIGUSR1", "SIGINT");
 *       setTimeout(() => {}, 5000); // Prevents exiting immediately
 *
 *       for await (const _ of sig) {
 *         console.log("interrupt or usr1 signal received");
 *       }
 *
 *       // At some other point in your code when finished listening:
 *       sig.dispose();
 * ```
 *
 * @param signals - one or more signals to listen to
 */ 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);
    });
    // Create dispose method for the muxer of signal streams.
    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, 4212 characters]