2020-02-07 01:53:15 -05:00
|
|
|
import { MuxAsyncIterator } from "../util/async.ts";
|
|
|
|
|
|
|
|
export function signal(
|
|
|
|
...signos: [number, ...number[]]
|
|
|
|
): AsyncIterable<void> & { dispose: () => void } {
|
|
|
|
const mux = new MuxAsyncIterator<void>();
|
|
|
|
|
|
|
|
if (signos.length < 1) {
|
|
|
|
throw new Error(
|
|
|
|
"No signals are given. You need to specify at least one signal to create a signal stream."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const streams = signos.map(Deno.signal);
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
streams.forEach((stream) => {
|
2020-02-07 01:53:15 -05:00
|
|
|
mux.add(stream);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create dispose method for the muxer of signal streams.
|
|
|
|
const dispose = (): void => {
|
2020-03-28 13:03:49 -04:00
|
|
|
streams.forEach((stream) => {
|
2020-02-07 01:53:15 -05:00
|
|
|
stream.dispose();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return Object.assign(mux, { dispose });
|
|
|
|
}
|