Skip to content

Commit f3f275e

Browse files
committed
getRootContext getter.
1 parent 5615630 commit f3f275e

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# CHANGELOG
22

3+
## 2.0.2 (October 27, 2020)
4+
5+
### Bug Fixes
6+
7+
- Domains root context fetching, extracted to private `getRootContext`.
8+
9+
## 2.0.1 (September 22, 2020)
10+
11+
### Improvements
12+
13+
- Update changelog.
14+
315
## 2.0.0 (September 19, 2020)
416

517
### New features

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-execution-context",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Provides execution context wrapper for node JS, can be used to create execution wrapper for handling requests and more",
55
"author": "Oded Goldglas <odedglas@gmail.com>",
66
"license": "ISC",

src/ExecutionContext/index.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)