nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
73 lines (57 loc) • 1.6 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/async_context_frame.js
import {
getContinuationPreservedEmbedderData,
setContinuationPreservedEmbedderData,
} from "nstdlib/stub/binding/async_context_frame";
import * as __hoisted_internal_options__ from "nstdlib/lib/internal/options";
let enabled_;
class ActiveAsyncContextFrame {
static get enabled() {
return true;
}
static current() {
return getContinuationPreservedEmbedderData();
}
static set(frame) {
setContinuationPreservedEmbedderData(frame);
}
static exchange(frame) {
const prior = this.current();
this.set(frame);
return prior;
}
static disable(store) {
const frame = this.current();
frame?.disable(store);
}
}
function checkEnabled() {
const enabled = __hoisted_internal_options__.getOptionValue(
"--experimental-async-context-frame",
);
// If enabled, swap to active prototype so we don't need to check status
// on every interaction with the async context frame.
if (enabled) {
// eslint-disable-next-line no-use-before-define
Object.setPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame);
}
return enabled;
}
class AsyncContextFrame extends Map {
constructor(store, data) {
super(AsyncContextFrame.current());
this.set(store, data);
}
static get enabled() {
enabled_ ??= checkEnabled();
return enabled_;
}
static current() {}
static set(frame) {}
static exchange(frame) {}
static disable(store) {}
disable(store) {
this.delete(store);
}
}
export default AsyncContextFrame;