diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 0f6ea222190255c122a5c12de97ae3794133d535..6d06a8cd6103ff473319f6370392a48d2170eafa 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -2146,14 +2146,15 @@ declare namespace Deno { * SignalStream represents the stream of signals, implements both * AsyncIterator and PromiseLike */ - export class SignalStream implements AsyncIterator, PromiseLike { + export class SignalStream + implements AsyncIterableIterator, PromiseLike { constructor(signal: typeof Deno.Signal); then( f: (v: void) => T | Promise, g?: (v: void) => S | Promise ): Promise; next(): Promise>; - [Symbol.asyncIterator](): AsyncIterator; + [Symbol.asyncIterator](): AsyncIterableIterator; dispose(): void; } diff --git a/cli/js/signals.ts b/cli/js/signals.ts index 02d52bc2f1c5aa0471104b50e05750c1a5edf4e5..9f47313d417ca4c4479be23c05158cb9b187c453 100644 --- a/cli/js/signals.ts +++ b/cli/js/signals.ts @@ -96,7 +96,8 @@ export const signals = { /** SignalStream represents the stream of signals, implements both * AsyncIterator and PromiseLike */ -export class SignalStream implements AsyncIterator, PromiseLike { +export class SignalStream + implements AsyncIterableIterator, PromiseLike { private rid: number; /** The promise of polling the signal, * resolves with false when it receives signal, @@ -134,7 +135,7 @@ export class SignalStream implements AsyncIterator, PromiseLike { return { done: await this.pollingPromise, value: undefined }; } - [Symbol.asyncIterator](): AsyncIterator { + [Symbol.asyncIterator](): AsyncIterableIterator { return this; } diff --git a/std/signal/mod.ts b/std/signal/mod.ts new file mode 100644 index 0000000000000000000000000000000000000000..e368edbd123d667573d1ada4cc68f3a076de888f --- /dev/null +++ b/std/signal/mod.ts @@ -0,0 +1,28 @@ +import { MuxAsyncIterator } from "../util/async.ts"; + +export function signal( + ...signos: [number, ...number[]] +): AsyncIterable & { dispose: () => void } { + const mux = new MuxAsyncIterator(); + + 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 }); +} diff --git a/std/signal/test.ts b/std/signal/test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cff9672e5e87ada105db3fced988d14f2f78b689 --- /dev/null +++ b/std/signal/test.ts @@ -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 => { + // 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); + }); +}