1
1
'use strict' ;
2
2
3
+ const {
4
+ ObjectSetPrototypeOf,
5
+ } = primordials ;
6
+
3
7
const {
4
8
getContinuationPreservedEmbedderData,
5
9
setContinuationPreservedEmbedderData,
6
10
} = internalBinding ( 'async_context_frame' ) ;
7
11
8
12
let enabled_ ;
9
13
10
- class AsyncContextFrame extends Map {
11
- constructor ( store , data ) {
12
- super ( AsyncContextFrame . current ( ) ) ;
13
- this . set ( store , data ) ;
14
- }
15
-
14
+ class ActiveAsyncContextFrame {
16
15
static get enabled ( ) {
17
- enabled_ ??= require ( 'internal/options' )
18
- . getOptionValue ( '--experimental-async-context-frame' ) ;
19
- return enabled_ ;
16
+ return true ;
20
17
}
21
18
22
19
static current ( ) {
23
- if ( this . enabled ) {
24
- return getContinuationPreservedEmbedderData ( ) ;
25
- }
20
+ return getContinuationPreservedEmbedderData ( ) ;
26
21
}
27
22
28
23
static set ( frame ) {
29
- if ( this . enabled ) {
30
- setContinuationPreservedEmbedderData ( frame ) ;
31
- }
24
+ setContinuationPreservedEmbedderData ( frame ) ;
32
25
}
33
26
34
27
static exchange ( frame ) {
@@ -41,6 +34,37 @@ class AsyncContextFrame extends Map {
41
34
const frame = this . current ( ) ;
42
35
frame ?. disable ( store ) ;
43
36
}
37
+ }
38
+
39
+ function checkEnabled ( ) {
40
+ const enabled = require ( 'internal/options' )
41
+ . getOptionValue ( '--experimental-async-context-frame' ) ;
42
+
43
+ // If enabled, swap to active prototype so we don't need to check status
44
+ // on every interaction with the async context frame.
45
+ if ( enabled ) {
46
+ // eslint-disable-next-line no-use-before-define
47
+ ObjectSetPrototypeOf ( AsyncContextFrame , ActiveAsyncContextFrame ) ;
48
+ }
49
+
50
+ return enabled ;
51
+ }
52
+
53
+ class AsyncContextFrame extends Map {
54
+ constructor ( store , data ) {
55
+ super ( AsyncContextFrame . current ( ) ) ;
56
+ this . set ( store , data ) ;
57
+ }
58
+
59
+ static get enabled ( ) {
60
+ enabled_ ??= checkEnabled ( ) ;
61
+ return enabled_ ;
62
+ }
63
+
64
+ static current ( ) { }
65
+ static set ( frame ) { }
66
+ static exchange ( frame ) { }
67
+ static disable ( store ) { }
44
68
45
69
disable ( store ) {
46
70
this . delete ( store ) ;
0 commit comments