summaryrefslogtreecommitdiff
path: root/std/signal/mod.ts
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-02-02 19:05:46 +0800
committerGitHub <noreply@github.com>2021-02-02 12:05:46 +0100
commit6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch)
treefd94c013a19fcb38954844085821ec1601c20e18 /std/signal/mod.ts
parenta2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff)
chore: remove std directory (#9361)
This removes the std folder from the tree. Various parts of the tests are pretty tightly dependent on std (47 direct imports and 75 indirect imports, not counting the cli tests that use them as fixtures) so I've added std as a submodule for now.
Diffstat (limited to 'std/signal/mod.ts')
-rw-r--r--std/signal/mod.ts73
1 files changed, 0 insertions, 73 deletions
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;
-}