11'use strict' ;
22
3+ const {
4+ ObjectSetPrototypeOf,
5+ } = primordials ;
6+
37const {
48 getContinuationPreservedEmbedderData,
59 setContinuationPreservedEmbedderData,
610} = internalBinding ( 'async_context_frame' ) ;
711
812let enabled_ ;
913
10- class AsyncContextFrame extends Map {
11- constructor ( store , data ) {
12- super ( AsyncContextFrame . current ( ) ) ;
13- this . set ( store , data ) ;
14- }
15-
14+ class ActiveAsyncContextFrame {
1615 static get enabled ( ) {
17- enabled_ ??= require ( 'internal/options' )
18- . getOptionValue ( '--experimental-async-context-frame' ) ;
19- return enabled_ ;
16+ return true ;
2017 }
2118
2219 static current ( ) {
23- if ( this . enabled ) {
24- return getContinuationPreservedEmbedderData ( ) ;
25- }
20+ return getContinuationPreservedEmbedderData ( ) ;
2621 }
2722
2823 static set ( frame ) {
29- if ( this . enabled ) {
30- setContinuationPreservedEmbedderData ( frame ) ;
31- }
24+ setContinuationPreservedEmbedderData ( frame ) ;
3225 }
3326
3427 static exchange ( frame ) {
@@ -41,6 +34,37 @@ class AsyncContextFrame extends Map {
4134 const frame = this . current ( ) ;
4235 frame ?. disable ( store ) ;
4336 }
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 ) { }
4468
4569 disable ( store ) {
4670 this . delete ( store ) ;
0 commit comments