Skip to content

Commit

Permalink
fix: reland async context (denoland#25140)
Browse files Browse the repository at this point in the history
This reverts commit 71ca61e.

Now uses a shared implementation from deno_core.
  • Loading branch information
devsnek authored Aug 29, 2024
1 parent 4f97261 commit f7556d8
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 292 deletions.
10 changes: 0 additions & 10 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,6 @@ fn op_node_build_os() -> String {
env!("TARGET").split('-').nth(2).unwrap().to_string()
}

#[op2(fast)]
fn op_node_is_promise_rejected(value: v8::Local<v8::Value>) -> bool {
let Ok(promise) = v8::Local::<v8::Promise>::try_from(value) else {
return false;
};

promise.state() == v8::PromiseState::Rejected
}

#[op2]
#[string]
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
Expand Down Expand Up @@ -347,7 +338,6 @@ deno_core::extension!(deno_node,
ops::os::op_cpus<P>,
ops::os::op_homedir<P>,
op_node_build_os,
op_node_is_promise_rejected,
op_npm_process_state,
ops::require::op_require_init_paths,
ops::require::op_require_node_module_paths<P>,
Expand Down
16 changes: 13 additions & 3 deletions ext/node/polyfills/_next_tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,17 +29,19 @@ 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()) {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// 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;
Expand All @@ -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
Expand Down Expand Up @@ -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_,
};
Expand Down
Loading

0 comments on commit f7556d8

Please sign in to comment.