mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
feat: add std/signal/mod.ts (#3913)
This commit is contained in:
parent
d9c84eb91e
commit
5a8ba3b114
4 changed files with 87 additions and 4 deletions
5
cli/js/lib.deno.ns.d.ts
vendored
5
cli/js/lib.deno.ns.d.ts
vendored
|
@ -2146,14 +2146,15 @@ declare namespace Deno {
|
|||
* SignalStream represents the stream of signals, implements both
|
||||
* AsyncIterator and PromiseLike
|
||||
*/
|
||||
export class SignalStream implements AsyncIterator<void>, PromiseLike<void> {
|
||||
export class SignalStream
|
||||
implements AsyncIterableIterator<void>, PromiseLike<void> {
|
||||
constructor(signal: typeof Deno.Signal);
|
||||
then<T, S>(
|
||||
f: (v: void) => T | Promise<T>,
|
||||
g?: (v: void) => S | Promise<S>
|
||||
): Promise<T | S>;
|
||||
next(): Promise<IteratorResult<void>>;
|
||||
[Symbol.asyncIterator](): AsyncIterator<void>;
|
||||
[Symbol.asyncIterator](): AsyncIterableIterator<void>;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
|
|
|
@ -96,7 +96,8 @@ export const signals = {
|
|||
|
||||
/** SignalStream represents the stream of signals, implements both
|
||||
* AsyncIterator and PromiseLike */
|
||||
export class SignalStream implements AsyncIterator<void>, PromiseLike<void> {
|
||||
export class SignalStream
|
||||
implements AsyncIterableIterator<void>, PromiseLike<void> {
|
||||
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<void>, PromiseLike<void> {
|
|||
return { done: await this.pollingPromise, value: undefined };
|
||||
}
|
||||
|
||||
[Symbol.asyncIterator](): AsyncIterator<void> {
|
||||
[Symbol.asyncIterator](): AsyncIterableIterator<void> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
28
std/signal/mod.ts
Normal file
28
std/signal/mod.ts
Normal file
|
@ -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 });
|
||||
}
|
53
std/signal/test.ts
Normal file
53
std/signal/test.ts
Normal file
|
@ -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);
|
||||
});
|
||||
}
|
Loading…
Add table
Reference in a new issue