Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): process.on and process.off for signals #1466

Merged
merged 9 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 26 additions & 11 deletions node/process.ts
Expand Up @@ -17,15 +17,6 @@ const notImplementedEvents = [
"message",
"multipleResolves",
"rejectionHandled",
"SIGBREAK",
"SIGBUS",
"SIGFPE",
"SIGHUP",
"SIGILL",
"SIGINT",
"SIGSEGV",
"SIGTERM",
"SIGWINCH",
"uncaughtException",
"uncaughtExceptionMonitor",
"unhandledRejection",
Expand Down Expand Up @@ -378,15 +369,39 @@ class Process extends EventEmitter {

/** https://nodejs.org/api/process.html#process_process_events */
//deno-lint-ignore ban-types
on(event: typeof notImplementedEvents[number], listener: Function): never;
on(event: "exit", listener: (code: number) => void): this;
on(event: string, listener: (...args: any[]) => void): this;
on(event: typeof notImplementedEvents[number], listener: Function): never;
//deno-lint-ignore no-explicit-any
on(event: string, listener: (...args: any[]) => void): this {
if (notImplementedEvents.includes(event)) {
notImplemented(`process.on("${event}")`);
}

super.on(event, listener);
if (event.startsWith("SIG")) {
Deno.addSignalListener(event as Deno.Signal, listener);
} else {
super.on(event, listener);
}

return this;
}

//deno-lint-ignore ban-types
off(event: "exit", listener: (code: number) => void): this;
off(event: string, listener: (...args: any[]) => void): this;
off(event: typeof notImplementedEvents[number], listener: Function): never;
//deno-lint-ignore no-explicit-any
off(event: string, listener: (...args: any[]) => void): this {
if (notImplementedEvents.includes(event)) {
notImplemented(`process.off("${event}")`);
}

if (event.startsWith("SIG")) {
Deno.removeSignalListener(event as Deno.Signal, listener);
} else {
super.off(event, listener);
}

return this;
}
Expand Down
51 changes: 51 additions & 0 deletions node/process_test.ts
Expand Up @@ -9,6 +9,7 @@ import {
assertThrows,
} from "../testing/asserts.ts";
import { stripColor } from "../fmt/colors.ts";
import { deferred } from "../async/deferred.ts";
import * as path from "../path/mod.ts";
import { delay } from "../async/delay.ts";
import { env } from "./process.ts";
Expand Down Expand Up @@ -152,6 +153,56 @@ Deno.test({
},
});

Deno.test({
name: "process.on signal",
ignore: Deno.build.os == "windows",
async fn() {
const promise = deferred();
let c = 0;
const listener = () => {
c += 1;
};
process.on("SIGINT", listener);
setTimeout(async () => {
// Sends SIGINT 3 times.
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGINT");
}
await delay(20);
Deno.removeSignalListener("SIGINT", listener);
promise.resolve();
});
await promise;
assertEquals(c, 3);
},
});

Deno.test({
name: "process.off signal",
ignore: Deno.build.os == "windows",
async fn() {
const promise = deferred();
let c = 0;
const listener = () => {
c += 1;
process.off("SIGINT", listener);
};
process.on("SIGINT", listener);
setTimeout(async () => {
// Sends SIGINT 3 times.
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGINT");
}
await delay(20);
promise.resolve();
});
await promise;
assertEquals(c, 1);
},
});

Deno.test({
name: "process.argv",
fn() {
Expand Down