@travetto/context 6.0.1 → 7.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/decorator.ts +2 -1
- package/support/test/context.ts +17 -16
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ yarn add @travetto/context
|
|
|
15
15
|
|
|
16
16
|
This module provides a wrapper around node's [async_hooks](https://nodejs.org/api/async_hooks.html) to maintain context across async calls. This is generally used for retaining contextual user information at various levels of async flow.
|
|
17
17
|
|
|
18
|
-
The most common way of utilizing the context, is via the [WithAsyncContext](https://github.com/travetto/travetto/tree/main/module/context/src/decorator.ts#
|
|
18
|
+
The most common way of utilizing the context, is via the [@WithAsyncContext](https://github.com/travetto/travetto/tree/main/module/context/src/decorator.ts#L9) decorator. The decorator requires the class it's being used in, to have a [AsyncContext](https://github.com/travetto/travetto/tree/main/module/context/src/service.ts#L12) member, as it is the source of the contextual information.
|
|
19
19
|
|
|
20
20
|
The decorator will load the context on invocation, and will keep the context active during the entire asynchronous call chain.
|
|
21
21
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.0",
|
|
4
4
|
"description": "Async-aware state management, maintaining context across asynchronous calls.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"async-hooks",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"directory": "module/context"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/di": "^
|
|
29
|
+
"@travetto/di": "^7.0.0-rc.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^
|
|
32
|
+
"@travetto/test": "^7.0.0-rc.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependenciesMeta": {
|
|
35
35
|
"@travetto/test": {
|
package/src/decorator.ts
CHANGED
|
@@ -4,11 +4,12 @@ import { AsyncContext } from './service.ts';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Allows running a function while providing an async context
|
|
7
|
+
* @kind decorator
|
|
7
8
|
*/
|
|
8
9
|
export function WithAsyncContext() {
|
|
9
10
|
return function <T extends { context: AsyncContext }>(
|
|
10
11
|
target: T,
|
|
11
|
-
|
|
12
|
+
property: string,
|
|
12
13
|
descriptor: AsyncMethodDescriptor<T>
|
|
13
14
|
): typeof descriptor {
|
|
14
15
|
const og = descriptor.value!;
|
package/support/test/context.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DependencyRegistryIndex } from '@travetto/di';
|
|
2
2
|
import { Class } from '@travetto/runtime';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { Registry } from '@travetto/registry';
|
|
4
|
+
import { SuiteRegistryIndex } from '@travetto/test';
|
|
5
5
|
|
|
6
6
|
import { AsyncContext } from '../../src/service.ts';
|
|
7
7
|
|
|
@@ -19,20 +19,21 @@ export function WithSuiteContext() {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this[Init]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
SuiteRegistryIndex.getForRegister(target).register({
|
|
23
|
+
beforeEach: [
|
|
24
|
+
async function (this: { [Init]?: boolean } & Record<string, Function>) {
|
|
25
|
+
if (!this[Init]) {
|
|
26
|
+
this[Init] = true;
|
|
27
|
+
await Registry.init();
|
|
28
|
+
const ctx = await DependencyRegistryIndex.getInstance(AsyncContext);
|
|
29
|
+
for (const [k, t] of Object.entries(SuiteRegistryIndex.getConfig(target).tests)) {
|
|
30
|
+
const fn = wrapped(ctx, this[t.methodName]);
|
|
31
|
+
Object.defineProperty(fn, 'name', { value: t.methodName });
|
|
32
|
+
this[t.methodName] = fn;
|
|
33
|
+
}
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
36
|
+
]
|
|
37
|
+
});
|
|
37
38
|
};
|
|
38
39
|
}
|