Skip to content

Commit 7f10ef2

Browse files
committed
Run support.
1 parent 9b6ca4c commit 7f10ef2

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ const app = express();
2222
const port = 3000;
2323

2424
const ContextMiddleware = (req, res, next) => {
25-
const requestResource = new AsyncResource('REQUEST_CONTEXT');
26-
requestResource.runInAsyncScope(() => {
27-
Context.create({
28-
val: true
29-
});
30-
next();
31-
});
25+
Context.run(next, { val: true });
3226
};
3327

3428
app.use('/', ContextMiddleware);

example/middleware.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
const { AsyncResource } = require('async_hooks');
21
const Context = require('../src');
32

43
module.exports = (req, res, next) => {
5-
const asyncResource = new AsyncResource('REQUEST_CONTEXT');
6-
return asyncResource.runInAsyncScope(() => {
7-
Context.create({
8-
val: true
9-
});
10-
next();
11-
});
4+
Context.run(next, { val: true });
125
};

src/hooks/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const getContextRef = (parentContext, triggerAsyncId) => (
1111
);
1212

1313
/**
14-
* Suspends a given function execution over process next tick.
14+
* Suspends a given f unction execution over process next tick.
1515
* @param {Function} fn - The function to trigger upon next tick.
1616
* @param {...any} args - The function arguments to trigger with.
1717
* @return {any}

src/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const asyncHooks = require('async_hooks');
2+
const ExecutionContextResource = require('./lib/ExecutionContextResource')
23
const { isProduction } = require('./lib');
34
const { create: createHooks } = require('./hooks');
45
const { ExecutionContextErrors } = require('./constants');
@@ -32,7 +33,7 @@ const createExecutionContext = () => {
3233
createHooks(executionContextMap)
3334
).enable();
3435

35-
return {
36+
const Context = {
3637

3738
/**
3839
* Creates an execution context for the current asyncId process.
@@ -41,7 +42,7 @@ const createExecutionContext = () => {
4142
* @returns void
4243
*/
4344
create: (initialContext = {}) => {
44-
console.log('Creating execution context : ', executionContextMap);
45+
console.log('Ceating: ', executionContextMap);
4546
const asyncId = asyncHooks.executionAsyncId();
4647

4748
// Creation is allowed once per execution context
@@ -90,8 +91,25 @@ const createExecutionContext = () => {
9091

9192
// Root context
9293
return context;
94+
},
95+
96+
/**
97+
* Runs a given function within "AsyncResource" context, this will ensure the function executed within a uniq execution context.
98+
* @param {Function} fn - The function to run.
99+
* @param {Object} initialContext - The initial context to expose to the function execution
100+
*/
101+
run: (fn, initialContext) => {
102+
const resource = new ExecutionContextResource();
103+
104+
resource.runInAsyncScope(() => {
105+
Context.create(initialContext);
106+
107+
fn();
108+
});
93109
}
94110
};
111+
112+
return Context;
95113
}
96114

97115
global.ExecutionContext = global.ExecutionContext || createExecutionContext();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { AsyncResource } = require('async_hooks');
2+
3+
const ASYNC_RESOURCE_TYPE = 'REQUEST_CONTEXT';
4+
5+
class ExecutionContextResource extends AsyncResource {
6+
constructor() {
7+
super(ASYNC_RESOURCE_TYPE);
8+
}
9+
}
10+
11+
module.exports = ExecutionContextResource;

0 commit comments

Comments
 (0)