feat: async context (#24402)

We are switching to ContinuationPreservedEmbedderData. This allows
adding async context tracking to the various async operations that deno
provides.

Fixes: https://github.com/denoland/deno/issues/7010
Fixes: https://github.com/denoland/deno/issues/22886
Fixes: https://github.com/denoland/deno/issues/24368
This commit is contained in:
snek 2024-08-02 08:14:35 -07:00 committed by GitHub
parent b82a2f114c
commit 3a1a1cc030
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 208 additions and 294 deletions

View file

@ -1,5 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
import process from "node:process";
import { setImmediate } from "node:timers";
import { assert, assertEquals } from "@std/assert";
Deno.test(async function foo() {
@ -92,7 +94,7 @@ Deno.test(async function enterWith() {
});
assertEquals(await deferred.promise, { x: 2 });
assertEquals(await deferred1.promise, { x: 1 });
assertEquals(await deferred1.promise, null);
});
Deno.test(async function snapshot() {
@ -135,3 +137,26 @@ Deno.test(function emitDestroyStub() {
const resource = new AsyncResource("foo");
assert(typeof resource.emitDestroy === "function");
});
Deno.test(async function worksWithAsyncAPIs() {
const store = new AsyncLocalStorage();
const test = () => assertEquals(store.getStore(), "data");
await store.run("data", async () => {
test();
queueMicrotask(() => test());
process.nextTick(() => test());
setImmediate(() => test());
setTimeout(() => test(), 0);
const intervalId = setInterval(() => {
test();
clearInterval(intervalId);
}, 0);
store.run("data2", () => {
assertEquals(store.getStore(), "data2");
});
await new Promise((r) => setTimeout(r, 50));
test();
});
});