@travetto/context 6.0.0-rc.1 → 6.0.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 +6 -6
- package/__index__.ts +3 -3
- package/package.json +4 -4
- package/src/decorator.ts +2 -1
- package/src/value.ts +3 -3
- package/support/test/context.ts +1 -1
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#L8) 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
|
|
|
@@ -26,7 +26,7 @@ The decorator will load the context on invocation, and will keep the context act
|
|
|
26
26
|
import { AsyncContext, WithAsyncContext } from '@travetto/context';
|
|
27
27
|
import { Inject } from '@travetto/di';
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const NameSymbol = Symbol();
|
|
30
30
|
|
|
31
31
|
export class ContextAwareService {
|
|
32
32
|
|
|
@@ -35,18 +35,18 @@ export class ContextAwareService {
|
|
|
35
35
|
|
|
36
36
|
@WithAsyncContext()
|
|
37
37
|
async complexOperator(name: string) {
|
|
38
|
-
this.context.set(
|
|
38
|
+
this.context.set(NameSymbol, name);
|
|
39
39
|
await this.additionalOp('extra');
|
|
40
40
|
await this.finalOp();
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
async additionalOp(additional: string) {
|
|
44
|
-
const name = this.context.get(
|
|
45
|
-
this.context.set(
|
|
44
|
+
const name = this.context.get(NameSymbol);
|
|
45
|
+
this.context.set(NameSymbol, `${name} ${additional}`);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
async finalOp() {
|
|
49
|
-
const name = this.context.get(
|
|
49
|
+
const name = this.context.get(NameSymbol);
|
|
50
50
|
// Use name
|
|
51
51
|
return name;
|
|
52
52
|
}
|
package/__index__.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './src/service';
|
|
2
|
-
export * from './src/value';
|
|
3
|
-
export * from './src/decorator';
|
|
1
|
+
export * from './src/service.ts';
|
|
2
|
+
export * from './src/value.ts';
|
|
3
|
+
export * from './src/decorator.ts';
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
|
-
"version": "6.0.0
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Async-aware state management, maintaining context across asynchronous calls.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"async-hooks",
|
|
7
7
|
"decorators",
|
|
8
|
-
"
|
|
8
|
+
"async-local-storage",
|
|
9
9
|
"travetto",
|
|
10
10
|
"typescript"
|
|
11
11
|
],
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"directory": "module/context"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/di": "^6.0.0
|
|
29
|
+
"@travetto/di": "^6.0.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^6.0.0
|
|
32
|
+
"@travetto/test": "^6.0.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependenciesMeta": {
|
|
35
35
|
"@travetto/test": {
|
package/src/decorator.ts
CHANGED
package/src/value.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { AppError, castTo } from '@travetto/runtime';
|
|
2
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
2
|
|
|
3
|
+
import { AppError, castTo } from '@travetto/runtime';
|
|
4
|
+
|
|
4
5
|
type Payload<T> = Record<string | symbol, T | undefined>;
|
|
5
6
|
type Storage<T = unknown> = AsyncLocalStorage<Payload<T>>;
|
|
6
7
|
type Key = string | symbol;
|
|
7
8
|
type StorageSource = Storage | (() => Storage) | { storage: Storage } | { context: { storage: Storage } };
|
|
8
9
|
|
|
9
10
|
type ReadWriteConfig = { read?: boolean, write?: boolean };
|
|
10
|
-
|
|
11
11
|
type ContextConfig = { failIfUnbound?: ReadWriteConfig };
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -60,4 +60,4 @@ export class AsyncContextValue<T = unknown> {
|
|
|
60
60
|
store[this.#key] = value;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
}
|
package/support/test/context.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Class } from '@travetto/runtime';
|
|
|
3
3
|
import { RootRegistry } from '@travetto/registry';
|
|
4
4
|
import { SuiteRegistry } from '@travetto/test';
|
|
5
5
|
|
|
6
|
-
import { AsyncContext } from '../../src/service';
|
|
6
|
+
import { AsyncContext } from '../../src/service.ts';
|
|
7
7
|
|
|
8
8
|
const Init = Symbol();
|
|
9
9
|
|