Skip to content

Commit

Permalink
use Deno.core.eventLoopHasMoreWork()
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and cjihrig committed Jun 28, 2022
1 parent 974693b commit 3b66278
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions node/_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ if (Deno?.core) {
encode(chunk: string): Uint8Array {
return new TextEncoder().encode(chunk);
},
eventLoopHasMoreWork(): boolean {
return false;
},
};
}
9 changes: 7 additions & 2 deletions node/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
stdin as stdin_,
stdout as stdout_,
} from "./_process/streams.mjs";
import { core } from "./_core.ts";

// TODO(kt3k): Give better types to stdio objects
// deno-lint-ignore no-explicit-any
const stderr = stderr_ as any;
Expand Down Expand Up @@ -81,7 +83,7 @@ export const exit = (code?: number | string) => {
if (!process._exiting) {
process._exiting = true;
// FIXME(bartlomieju): this is wrong, we won't be using syscall to exit
// and thus the `unload` event will be emitted to properly trigget "emit"
// and thus the `unload` event will not be emitted to properly trigger "emit"
// event on `process`.
process.emit("exit", process.exitCode || 0);
}
Expand Down Expand Up @@ -260,8 +262,11 @@ class Process extends EventEmitter {
constructor() {
super();

globalThis.addEventListener("beforeunload", () => {
globalThis.addEventListener("beforeunload", (e) => {
super.emit("beforeExit", process.exitCode || 0);
if (core.eventLoopHasMoreWork()) {
e.preventDefault();
}
});

globalThis.addEventListener("unload", () => {
Expand Down
10 changes: 5 additions & 5 deletions process.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code: ', code);
process.on("beforeExit", (code) => {
console.log("Process beforeExit event with code: ", code);
});

process.on('exit', (code) => {
console.log('Process exit event with code: ', code);
process.on("exit", (code) => {
console.log("Process exit event with code: ", code);
});

console.log('This message is displayed first.');
console.log("This message is displayed first.");

0 comments on commit 3b66278

Please sign in to comment.