@@ -61,6 +61,20 @@ class ExecutionContext {
6161 this . config = config ;
6262 }
6363
64+ /**
65+ * Returns current execution id root context for the current asyncId process.
66+ * @param {Number } asyncId - The current execution context id.
67+ * @return {ExecutionContextNode }
68+ * @private
69+ */
70+ _getRootContext ( asyncId ) {
71+ const context = executionContextMap . get ( asyncId ) ;
72+
73+ if ( context && context . ref ) return executionContextMap . get ( context . ref ) ;
74+
75+ return context ;
76+ }
77+
6478 /**
6579 * Creates an execution context for the current asyncId process.
6680 * This will expose Context get / update at any point after.
@@ -72,28 +86,28 @@ class ExecutionContext {
7286 const config = this . config ;
7387 const asyncId = asyncHooks . executionAsyncId ( ) ;
7488
75- const refContext = executionContextMap . get ( asyncId ) ;
76- if ( refContext ) {
89+ const rootContext = this . _getRootContext ( asyncId ) ;
90+ if ( rootContext ) {
7791
7892 // Execution context creation is allowed once per domain
79- if ( domain === refContext . domain ) return handleError ( ExecutionContextErrors . CONTEXT_ALREADY_DECLARED ) ;
93+ if ( domain === rootContext . domain ) return handleError ( ExecutionContextErrors . CONTEXT_ALREADY_DECLARED ) ;
8094
8195 // Setting up domain initial context
82- initialContext = { ...this . get ( ) , ...initialContext } ;
96+ initialContext = { ...rootContext . context , ...initialContext } ;
8397
8498 // Disconnecting current async id from stored parent chain
85- onChildProcessDestroy ( executionContextMap , asyncId , refContext . ref ) ;
99+ onChildProcessDestroy ( executionContextMap , asyncId , rootContext . asyncId ) ;
86100 }
87101
88102 // Creating root context node
89- const rootContext = createRootContext ( {
103+ const root = createRootContext ( {
90104 asyncId,
91105 initialContext,
92106 config,
93107 domain
94108 } ) ;
95109
96- executionContextMap . set ( asyncId , rootContext ) ;
110+ executionContextMap . set ( asyncId , root ) ;
97111 }
98112
99113 /**
@@ -106,14 +120,10 @@ class ExecutionContext {
106120
107121 if ( ! executionContextMap . has ( asyncId ) ) return handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
108122
109- const contextData = executionContextMap . get ( asyncId ) ;
110-
111123 // Update target is always the root context, ref updates will need to be channeled
112- const targetContextData = contextData . ref
113- ? executionContextMap . get ( contextData . ref )
114- : contextData ;
124+ const rootContext = this . _getRootContext ( asyncId ) ;
115125
116- targetContextData . context = { ...targetContextData . context , ...update } ;
126+ rootContext . context = { ...rootContext . context , ...update } ;
117127 }
118128
119129 /**
@@ -122,17 +132,10 @@ class ExecutionContext {
122132 */
123133 get ( ) {
124134 const asyncId = asyncHooks . executionAsyncId ( ) ;
125- if ( ! executionContextMap . has ( asyncId ) ) return handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
126-
127- const { context = { } , ref } = executionContextMap . get ( asyncId ) ;
128- if ( ref ) {
129135
130- // Ref will be used to point out on the root context
131- return executionContextMap . get ( ref ) . context ;
132- }
136+ if ( ! executionContextMap . has ( asyncId ) ) return handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
133137
134- // Root context
135- return context ;
138+ return this . _getRootContext ( asyncId ) . context ;
136139 }
137140
138141 /**
0 commit comments