@@ -3,17 +3,6 @@ const { isProduction } = require('./lib');
33const { create : createHooks } = require ( './hooks' ) ;
44const { ExecutionContextErrors } = require ( './constants' ) ;
55
6- /**
7- * The global service context execution map
8- * @type ExecutionContextMap
9- */
10- const executionContextMap = new Map ( ) ;
11-
12- // Sets node async hooks setup
13- asyncHooks . createHook (
14- createHooks ( executionContextMap )
15- ) . enable ( ) ;
16-
176/**
187 * Handles execution context error, throws when none production
198 * @param code
@@ -28,65 +17,82 @@ const handleError = (code) => {
2817
2918/**
3019 * The Execution Context API
20+ * @return {ExecutionContextAPI }
3121 */
32- const Context = {
33-
34- /**
35- * Creates an execution context for the current asyncId process.
36- * This will expose Context get / update at any point after.
37- * @param {Object } initialContext - The initial context to be used
38- * @returns void
39- */
40- create : ( initialContext = { } ) => {
41- const asyncId = asyncHooks . executionAsyncId ( ) ;
42-
43- // Creation is allowed once per execution context
44- if ( executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_ALREADY_DECLARED ) ;
45-
46- executionContextMap . set ( asyncId , {
47- context : { ...initialContext , executionId : asyncId } ,
48- children : [ ]
49- } ) ;
50- } ,
51-
52- /**
53- * Updates the current async process context
54- * @param {Object } update - The update to apply on the current process context
55- * @returns void
56- */
57- update : ( update = { } ) => {
58- const asyncId = asyncHooks . executionAsyncId ( ) ;
59-
60- if ( ! executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
61-
62- const contextData = executionContextMap . get ( asyncId ) ;
63-
64- // Update target is always the root context, ref updates will need to be channeled
65- const targetContextData = contextData . ref
66- ? executionContextMap . get ( contextData . ref )
67- : contextData ;
68-
69- targetContextData . context = { ...targetContextData . context , ...update } ;
70- } ,
22+ const createExecutionContext = ( ) => {
7123
7224 /**
73- * Gets the current async process execution context
74- * @returns { Object }
25+ * The global service context execution map
26+ * @type ExecutionContextMap
7527 */
76- get : ( ) => {
77- const asyncId = asyncHooks . executionAsyncId ( ) ;
78- if ( ! executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
79-
80- const { context = { } , ref } = executionContextMap . get ( asyncId ) ;
81- if ( ref ) {
82-
83- // Ref will be used to point out on the root context
84- return executionContextMap . get ( ref ) . context ;
28+ const executionContextMap = new Map ( ) ;
29+
30+ // Sets node async hooks setup
31+ asyncHooks . createHook (
32+ createHooks ( executionContextMap )
33+ ) . enable ( ) ;
34+
35+ return {
36+
37+ /**
38+ * Creates an execution context for the current asyncId process.
39+ * This will expose Context get / update at any point after.
40+ * @param {Object } initialContext - The initial context to be used
41+ * @returns void
42+ */
43+ create : ( initialContext = { } ) => {
44+ const asyncId = asyncHooks . executionAsyncId ( ) ;
45+
46+ // Creation is allowed once per execution context
47+ if ( executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_ALREADY_DECLARED ) ;
48+
49+ executionContextMap . set ( asyncId , {
50+ context : { ...initialContext , executionId : asyncId } ,
51+ children : [ ]
52+ } ) ;
53+ } ,
54+
55+ /**
56+ * Updates the current async process context.
57+ * @param {Object } update - The update to apply on the current process context.
58+ * @returns void
59+ */
60+ update : ( update = { } ) => {
61+ const asyncId = asyncHooks . executionAsyncId ( ) ;
62+
63+ if ( ! executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
64+
65+ const contextData = executionContextMap . get ( asyncId ) ;
66+
67+ // Update target is always the root context, ref updates will need to be channeled
68+ const targetContextData = contextData . ref
69+ ? executionContextMap . get ( contextData . ref )
70+ : contextData ;
71+
72+ targetContextData . context = { ...targetContextData . context , ...update } ;
73+ } ,
74+
75+ /**
76+ * Gets the current async process execution context.
77+ * @returns {Object }
78+ */
79+ get : ( ) => {
80+ const asyncId = asyncHooks . executionAsyncId ( ) ;
81+ if ( ! executionContextMap . has ( asyncId ) ) handleError ( ExecutionContextErrors . CONTEXT_DOES_NOT_EXISTS ) ;
82+
83+ const { context = { } , ref } = executionContextMap . get ( asyncId ) ;
84+ if ( ref ) {
85+
86+ // Ref will be used to point out on the root context
87+ return executionContextMap . get ( ref ) . context ;
88+ }
89+
90+ // Root context
91+ return context ;
8592 }
93+ } ;
94+ }
8695
87- // Root context
88- return context ;
89- }
90- } ;
96+ global . ExecutionContext = global . ExecutionContext || createExecutionContext ( ) ;
9197
92- module . exports = Context ;
98+ module . exports = global . ExecutionContext ;
0 commit comments