summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_next_tick.ts
diff options
context:
space:
mode:
authorsnek <snek@deno.com>2024-08-28 19:25:38 -0700
committerGitHub <noreply@github.com>2024-08-29 02:25:38 +0000
commitf7556d8962b2106e82c9f0eb90f406eec0b60dd0 (patch)
treeed8cc065b713ed17750be1170d51725801565bd7 /ext/node/polyfills/_next_tick.ts
parent4f97261a012edda267ff6b74c5b0b6d08a1c12b8 (diff)
fix: reland async context (#25140)
This reverts commit 71ca61e189cca9215982ce4598b7a4da8430c584. Now uses a shared implementation from deno_core.
Diffstat (limited to 'ext/node/polyfills/_next_tick.ts')
-rw-r--r--ext/node/polyfills/_next_tick.ts16
1 files changed, 13 insertions, 3 deletions
diff --git a/ext/node/polyfills/_next_tick.ts b/ext/node/polyfills/_next_tick.ts
index 5915c750e..5ee27728d 100644
--- a/ext/node/polyfills/_next_tick.ts
+++ b/ext/node/polyfills/_next_tick.ts
@@ -10,9 +10,15 @@ import { validateFunction } from "ext:deno_node/internal/validators.mjs";
import { _exiting } from "ext:deno_node/_process/exiting.ts";
import { FixedQueue } from "ext:deno_node/internal/fixed_queue.ts";
+const {
+ getAsyncContext,
+ setAsyncContext,
+} = core;
+
interface Tock {
callback: (...args: Array<unknown>) => void;
args: Array<unknown>;
+ snapshot: unknown;
}
let nextTickEnabled = false;
@@ -23,7 +29,7 @@ export function enableNextTick() {
const queue = new FixedQueue();
export function processTicksAndRejections() {
- let tock;
+ let tock: Tock;
do {
// deno-lint-ignore no-cond-assign
while (tock = queue.shift()) {
@@ -31,9 +37,11 @@ export function processTicksAndRejections() {
// const asyncId = tock[async_id_symbol];
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
+ const oldContext = getAsyncContext();
try {
- const callback = (tock as Tock).callback;
- if ((tock as Tock).args === undefined) {
+ setAsyncContext(tock.snapshot);
+ const callback = tock.callback;
+ if (tock.args === undefined) {
callback();
} else {
const args = (tock as Tock).args;
@@ -58,6 +66,7 @@ export function processTicksAndRejections() {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())
// emitDestroy(asyncId);
+ setAsyncContext(oldContext);
}
// FIXME(bartlomieju): Deno currently doesn't support async hooks
@@ -143,6 +152,7 @@ export function nextTick<T extends Array<unknown>>(
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// [async_id_symbol]: asyncId,
// [trigger_async_id_symbol]: triggerAsyncId,
+ snapshot: getAsyncContext(),
callback,
args: args_,
};