Skip to content
Open
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
66 changes: 30 additions & 36 deletions lib/internal/async_context_frame.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ObjectSetPrototypeOf,
SafeMap,
} = primordials;

Expand All @@ -10,45 +9,22 @@ const {
setContinuationPreservedEmbedderData,
} = internalBinding('async_context_frame');

let enabled_;

class ActiveAsyncContextFrame extends SafeMap {
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 activeCurrent() {
return getContinuationPreservedEmbedderData();
}

function checkEnabled() {
const enabled = require('internal/options')
.getOptionValue('--async-context-frame');
function activeSet(frame) {
setContinuationPreservedEmbedderData(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
ObjectSetPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame);
}
function activeExchange(frame) {
const prior = getContinuationPreservedEmbedderData();
setContinuationPreservedEmbedderData(frame);
return prior;
}

return enabled;
function activeDisable(store) {
getContinuationPreservedEmbedderData()?.disable(store);
}

class InactiveAsyncContextFrame extends SafeMap {
Expand All @@ -74,4 +50,22 @@ class AsyncContextFrame extends InactiveAsyncContextFrame {
}
}

let enabled_;

function checkEnabled() {
const enabled = require('internal/options')
.getOptionValue('--async-context-frame');

// If enabled, install the active implementations directly. We use props
// rather than a prototype replacement to preserve V8 optimizations.
if (enabled) {
AsyncContextFrame.current = activeCurrent;
AsyncContextFrame.set = activeSet;
AsyncContextFrame.exchange = activeExchange;
AsyncContextFrame.disable = activeDisable;
}

return enabled;
}

module.exports = AsyncContextFrame;
Loading