You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-21Lines changed: 56 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
# node-execution-context
2
-
A straightforward library that provides a persistent process-level context wrapper using node "async_hooks" feature.
2
+
A straightforward library that provides a persistent process-level context wrapper using node "async_hooks" feature.
3
+
This library will try to use by default [`AsyncLocalStorage`](https://nodejs.org/api/async_hooks.html#async_hooks_class_asynclocalstorage) implementation based if current node version supports it, otherwise it will fallback to raw [`async_hooks`](https://nodejs.org/api/async_hooks.html) implementation for lower versions which mimics this behaviour.
3
4
4
5
## Installation
5
6
@@ -21,7 +22,7 @@ const app = express();
21
22
constport=3000;
22
23
23
24
constContextMiddleware= (req, res, next) => {
24
-
Context.run(next, { val:true });
25
+
Context.run(next, { reference:Math.random() });
25
26
};
26
27
27
28
app.use('/', ContextMiddleware);
@@ -44,7 +45,7 @@ export class UserController {
44
45
asynccreate (req) {
45
46
const { user } =req.body;
46
47
47
-
// This will return the reference number set by out ContextMiddleware
48
+
// This will return the reference number set by out ContextMiddleware (generated by Math.random())
48
49
const { reference } =Context.get();
49
50
50
51
logger.info('Created user for reference: ', reference);
Creates for the current async resource an execution context entry identified with his asyncId.
62
-
Any future processes that will be added to the async execution chain will be exposed to this context.
62
+
Runs given callback that will be exposed to the given context.
63
+
The context will be exposed to all callbacks and promise chains triggered from the given `fn`.
63
64
64
-
> When passing custom domain to this method, the trigger point and all of it's sub processes will be exposed to a standalone context and won't effect / be effected by root context.
65
+
### get()
65
66
66
-
### update(update: object)
67
+
Gets the current asynchronous execution context.
67
68
68
-
Updates the current execution context with a given update obect.
69
+
> The `get` result is returned by `reference` meaning if you wish any immutability applied, it will have to be manually applied.
69
70
70
-
### get()
71
+
> This API may throw CONTEXT_DOES_NOT_EXIST error if accessed without initializing the context properly.
72
+
73
+
### set(context: unknown)
74
+
75
+
Sets the current asynchronous execution context to given value.
76
+
77
+
> This API may throw CONTEXT_DOES_NOT_EXIST error if accessed without initializing the context properly.
78
+
79
+
### create(context?: unknown)
80
+
81
+
Creates a given context for the current asynchronous execution.
82
+
It is recommended to use the `run` method. This method should be used in special cases in which the `run` method cannot be used directly.
83
+
84
+
> Note that if this method will be called not within a AsyncResource context, it will effect current execution context and should be used with caution.
85
+
86
+
#### Example
87
+
88
+
```js
89
+
constContext=require('node-execution-context');
90
+
91
+
// context-events.js
92
+
constcontext= { id:Math.random() };
71
93
72
-
Returns the current execution context identified with the current asyncId.
94
+
emitter.on('contextual-event', () => {
95
+
Context.create(context);
96
+
});
73
97
74
-
### run(fn: Function, initialContext: object)
98
+
// service.js
99
+
emitter.on('contextual-event', () => {
100
+
Context.get(); // Returns { id: random }
101
+
});
75
102
76
-
Runs a given function under a dedicated AsyncResource, exposing given initial context to the process and it's child processes.
0 commit comments