mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Signed-off-by: Tim Perry <pimterry@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65519 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
72 lines
1.5 KiB
JavaScript
72 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
SafeMap,
|
|
} = primordials;
|
|
|
|
const {
|
|
getContinuationPreservedEmbedderData,
|
|
setContinuationPreservedEmbedderData,
|
|
} = internalBinding('async_context_frame');
|
|
|
|
function activeCurrent() {
|
|
return getContinuationPreservedEmbedderData();
|
|
}
|
|
|
|
function activeSet(frame) {
|
|
setContinuationPreservedEmbedderData(frame);
|
|
}
|
|
|
|
function activeExchange(frame) {
|
|
const prior = getContinuationPreservedEmbedderData();
|
|
setContinuationPreservedEmbedderData(frame);
|
|
return prior;
|
|
}
|
|
|
|
function activeDisable(store) {
|
|
getContinuationPreservedEmbedderData()?.disable(store);
|
|
}
|
|
|
|
class InactiveAsyncContextFrame extends SafeMap {
|
|
static get enabled() {
|
|
enabled_ ??= checkEnabled();
|
|
return enabled_;
|
|
}
|
|
|
|
static current() {}
|
|
static set(frame) {}
|
|
static exchange(frame) {}
|
|
static disable(store) {}
|
|
}
|
|
|
|
class AsyncContextFrame extends InactiveAsyncContextFrame {
|
|
constructor(store, data) {
|
|
super(AsyncContextFrame.current());
|
|
this.set(store, data);
|
|
}
|
|
|
|
disable(store) {
|
|
this.delete(store);
|
|
}
|
|
}
|
|
|
|
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;
|