Skip to content

Commit

Permalink
suggestion from #2331 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Jul 13, 2022
1 parent 6f8f3e7 commit 55a466e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
100 changes: 50 additions & 50 deletions node/_next_tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,56 @@ const queue = new FixedQueue();
// deno-lint-ignore no-explicit-any
let _nextTick: any;

export function processTicksAndRejections() {
let tock;
do {
// deno-lint-ignore no-cond-assign
while (tock = queue.shift()) {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// const asyncId = tock[async_id_symbol];
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);

try {
const callback = (tock as Tock).callback;
if ((tock as Tock).args === undefined) {
callback();
} else {
const args = (tock as Tock).args;
switch (args.length) {
case 1:
callback(args[0]);
break;
case 2:
callback(args[0], args[1]);
break;
case 3:
callback(args[0], args[1], args[2]);
break;
case 4:
callback(args[0], args[1], args[2], args[3]);
break;
default:
callback(...args);
}
}
} finally {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())
// emitDestroy(asyncId);
}

// FIXME(bartlomieju): Deno currently doesn't support async hooks
// emitAfter(asyncId);
}
core.runMicrotasks();
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// } while (!queue.isEmpty() || processPromiseRejections());
} while (!queue.isEmpty());
core.setHasTickScheduled(false);
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// setHasRejectionToWarn(false);
}

if (typeof core.setNextTickCallback !== "undefined") {
function runNextTicks() {
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
Expand All @@ -36,56 +86,6 @@ if (typeof core.setNextTickCallback !== "undefined") {
return true;
}

function processTicksAndRejections() {
let tock;
do {
// deno-lint-ignore no-cond-assign
while (tock = queue.shift()) {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// const asyncId = tock[async_id_symbol];
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);

try {
const callback = (tock as Tock).callback;
if ((tock as Tock).args === undefined) {
callback();
} else {
const args = (tock as Tock).args;
switch (args.length) {
case 1:
callback(args[0]);
break;
case 2:
callback(args[0], args[1]);
break;
case 3:
callback(args[0], args[1], args[2]);
break;
case 4:
callback(args[0], args[1], args[2], args[3]);
break;
default:
callback(...args);
}
}
} finally {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())
// emitDestroy(asyncId);
}

// FIXME(bartlomieju): Deno currently doesn't support async hooks
// emitAfter(asyncId);
}
core.runMicrotasks();
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// } while (!queue.isEmpty() || processPromiseRejections());
} while (!queue.isEmpty());
core.setHasTickScheduled(false);
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// setHasRejectionToWarn(false);
}

core.setNextTickCallback(processTicksAndRejections);
core.setMacrotaskCallback(runNextTicks);

Expand Down
2 changes: 2 additions & 0 deletions node/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
stdout as stdout_,
} from "./_process/streams.mjs";
import { core } from "./_core.ts";
import { processTicksAndRejections } from "./_next_tick.ts";

// TODO(kt3k): Give better types to stdio objects
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -272,6 +273,7 @@ class Process extends EventEmitter {

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

0 comments on commit 55a466e

Please sign in to comment.