diff options
Diffstat (limited to 'std/signal')
-rw-r--r-- | std/signal/README.md | 38 | ||||
-rw-r--r-- | std/signal/mod.ts | 73 | ||||
-rw-r--r-- | std/signal/test.ts | 91 |
3 files changed, 0 insertions, 202 deletions
diff --git a/std/signal/README.md b/std/signal/README.md deleted file mode 100644 index 7a152cdca..000000000 --- a/std/signal/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# signal - -signal is a module used to capture and monitor OS signals. - -# usage - -The following functions are exposed in `mod.ts`: - -## signal - -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. - -```typescript -import { signal } from "https://deno.land/std/signal/mod.ts"; -const sig = signal(Deno.Signal.SIGUSR1, Deno.Signal.SIGINT); -setTimeout(() => {}, 5000); // Prevents exiting immediately. - -for await (const _ of sig) { - // .. -} - -// At some other point in your code when finished listening: -sig.dispose(); -``` - -## onSignal - -Registers a callback function to be called on triggering of a signal event. - -```typescript -import { onSignal } from "https://deno.land/std/signal/mod.ts"; - -const handle = onSignal(Deno.Signal.SIGINT, () => { - // ... - handle.dispose(); // de-register from receiving further events. -}); -``` diff --git a/std/signal/mod.ts b/std/signal/mod.ts deleted file mode 100644 index c28cf5e99..000000000 --- a/std/signal/mod.ts +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { MuxAsyncIterator } from "../async/mux_async_iterator.ts"; - -export type Disposable = { dispose: () => void }; - -/** - * 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: - * - * const sig = signal(Deno.Signal.SIGUSR1, Deno.Signal.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 signos - one or more `Deno.Signal`s to await on - */ -export function signal( - ...signos: [number, ...number[]] -): AsyncIterable<void> & Disposable { - 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 }); -} - -/** - * Registers a callback function to be called on triggering of a signal event. - * - * const handle = onSignal(Deno.Signal.SIGINT, () => { - * console.log('Received SIGINT'); - * handle.dispose(); // de-register from receiving further events - * }); - * - * @param signo One of Deno.Signal (e.g. Deno.Signal.SIGINT) - * @param callback Callback function triggered upon signal event - */ -export function onSignal(signo: number, callback: () => void): Disposable { - const sig = signal(signo); - - // allows `sig` to be returned before blocking on the await - (async (): Promise<void> => { - for await (const _ of sig) { - callback(); - } - })(); - - return sig; -} diff --git a/std/signal/test.ts b/std/signal/test.ts deleted file mode 100644 index d56b7920d..000000000 --- a/std/signal/test.ts +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals, assertThrows } from "../testing/asserts.ts"; -import { delay } from "../async/delay.ts"; -import { onSignal, signal } from "./mod.ts"; - -Deno.test({ - name: "signal() throws when called with empty signals", - ignore: Deno.build.os === "windows", - fn() { - assertThrows( - () => { - // deno-lint-ignore no-explicit-any - (signal as any)(); - }, - Error, - "No signals are given. You need to specify at least one signal to create a signal stream.", - ); - }, -}); - -Deno.test({ - name: "signal() iterates for multiple signals", - ignore: Deno.build.os === "windows", - fn: 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); - }, -}); - -Deno.test({ - name: "onSignal() registers and disposes of event handler", - ignore: Deno.build.os === "windows", - async fn() { - // This prevents the program from exiting. - const t = setInterval(() => {}, 1000); - - let calledCount = 0; - const handle = onSignal(Deno.Signal.SIGINT, () => { - calledCount++; - }); - - await delay(20); - Deno.kill(Deno.pid, Deno.Signal.SIGINT); - await delay(20); - Deno.kill(Deno.pid, Deno.Signal.SIGINT); - await delay(20); - Deno.kill(Deno.pid, Deno.Signal.SIGUSR2); - await delay(20); - handle.dispose(); // stop monitoring SIGINT - await delay(20); - Deno.kill(Deno.pid, Deno.Signal.SIGUSR1); - await delay(20); - Deno.kill(Deno.pid, Deno.Signal.SIGINT); - await delay(20); - assertEquals(calledCount, 2); - - clearTimeout(t); - }, -}); |