forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add std/signal/mod.ts (denoland#3913)
- Loading branch information
Showing
4 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { test, assertEquals, assertThrows } from "../testing/mod.ts"; | ||
import { delay } from "../util/async.ts"; | ||
import { signal } from "./mod.ts"; | ||
|
||
if (Deno.build.os !== "win") { | ||
test("signal() throws when called with empty signals", (): void => { | ||
assertThrows( | ||
() => { | ||
// @ts-ignore | ||
signal(); | ||
}, | ||
Error, | ||
"No signals are given. You need to specify at least one signal to create a signal stream." | ||
); | ||
}); | ||
|
||
test("signal() iterates for multiple signals", async (): Promise<void> => { | ||
// This prevents the program from exiting. | ||
const t = setInterval(() => {}, 1000); | ||
|
||
let c = 0; | ||
const sig = signal( | ||
Deno.Signal.SIGUSR1, | ||
Deno.Signal.SIGUSR2, | ||
Deno.Signal.SIGINT | ||
); | ||
|
||
setTimeout(async () => { | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGINT); | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2); | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1); | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2); | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1); | ||
await delay(20); | ||
Deno.kill(Deno.pid, Deno.Signal.SIGINT); | ||
await delay(20); | ||
sig.dispose(); | ||
}); | ||
|
||
for await (const _ of sig) { | ||
c += 1; | ||
} | ||
|
||
assertEquals(c, 6); | ||
|
||
clearTimeout(t); | ||
}); | ||
} |