summaryrefslogtreecommitdiff
path: root/runtime/js/01_async_context.js
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/js/01_async_context.js')
-rw-r--r--runtime/js/01_async_context.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/runtime/js/01_async_context.js b/runtime/js/01_async_context.js
new file mode 100644
index 000000000..9c0236fbe
--- /dev/null
+++ b/runtime/js/01_async_context.js
@@ -0,0 +1,45 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+
+import { primordials } from "ext:core/mod.js";
+import { op_get_extras_binding_object } from "ext:core/ops";
+
+const {
+ SafeWeakMap,
+} = primordials;
+
+const {
+ getContinuationPreservedEmbedderData,
+ setContinuationPreservedEmbedderData,
+} = op_get_extras_binding_object();
+
+let counter = 0;
+
+export const getAsyncContext = getContinuationPreservedEmbedderData;
+export const setAsyncContext = setContinuationPreservedEmbedderData;
+
+export class AsyncVariable {
+ #id = counter++;
+ #data = new SafeWeakMap();
+
+ enter(value) {
+ const previousContextMapping = getAsyncContext();
+ const entry = { id: this.#id };
+ const asyncContextMapping = {
+ __proto__: null,
+ ...previousContextMapping,
+ [this.#id]: entry,
+ };
+ this.#data.set(entry, value);
+ setAsyncContext(asyncContextMapping);
+ return previousContextMapping;
+ }
+
+ get() {
+ const current = getAsyncContext();
+ const entry = current?.[this.#id];
+ if (entry) {
+ return this.#data.get(entry);
+ }
+ return undefined;
+ }
+}