forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathasync_context_frame.js
76 lines (60 loc) · 1.52 KB
/
async_context_frame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'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;