node/lib/internal/async_context_frame.js
Stephen Belanger 51ae57673d
lib: make ALS default to AsyncContextFrame
PR-URL: https://github.com/nodejs/node/pull/55552
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2024-11-02 15:14:28 +00:00

77 lines
1.5 KiB
JavaScript

'use strict';
const {
ObjectSetPrototypeOf,
} = primordials;
const {
getContinuationPreservedEmbedderData,
setContinuationPreservedEmbedderData,
} = internalBinding('async_context_frame');
let enabled_;
class ActiveAsyncContextFrame extends Map {
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 = require('internal/options')
.getOptionValue('--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
ObjectSetPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame);
}
return enabled;
}
class InactiveAsyncContextFrame extends Map {
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);
}
}
module.exports = AsyncContextFrame;