diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2020-02-07 15:53:15 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 01:53:15 -0500 |
commit | 5a8ba3b114bbacf10f03abc0335a753585861c97 (patch) | |
tree | 8bd6d55736fd08d958ed81f447fcf87b33a76d32 /std/signal/mod.ts | |
parent | d9c84eb91eecdf1225091e4d2bea8e4c6a076504 (diff) |
feat: add std/signal/mod.ts (#3913)
Diffstat (limited to 'std/signal/mod.ts')
-rw-r--r-- | std/signal/mod.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/std/signal/mod.ts b/std/signal/mod.ts new file mode 100644 index 000000000..e368edbd1 --- /dev/null +++ b/std/signal/mod.ts @@ -0,0 +1,28 @@ +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); + + streams.forEach(stream => { + mux.add(stream); + }); + + // Create dispose method for the muxer of signal streams. + const dispose = (): void => { + streams.forEach(stream => { + stream.dispose(); + }); + }; + + return Object.assign(mux, { dispose }); +} |