Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: async context #24402

Merged
merged 1 commit into from
Aug 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,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 @@ -350,7 +341,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
15 changes: 12 additions & 3 deletions ext/node/polyfills/_next_tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// deno-lint-ignore-file prefer-primordials

import { core } from "ext:core/mod.js";
import {
getAsyncContext,
setAsyncContext,
} from "ext:runtime/01_async_context.js";

import { validateFunction } from "ext:deno_node/internal/validators.mjs";
import { _exiting } from "ext:deno_node/_process/exiting.ts";
Expand All @@ -13,6 +17,7 @@ import { FixedQueue } from "ext:deno_node/internal/fixed_queue.ts";
interface Tock {
callback: (...args: Array<unknown>) => void;
args: Array<unknown>;
snapshot: unknown;
}

let nextTickEnabled = false;
Expand All @@ -23,17 +28,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 +65,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 +151,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