@travetto/context 8.0.0-alpha.2 → 8.0.0-alpha.21
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 +2 -4
- package/__index__.ts +1 -1
- package/package.json +3 -3
- package/src/decorator.ts +1 -1
- package/src/service.ts +2 -3
- package/src/value.ts +6 -11
- package/support/test/context.ts +6 -4
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#L9) decorator.
|
|
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
|
|
|
@@ -29,7 +29,6 @@ import { Inject } from '@travetto/di';
|
|
|
29
29
|
const NameSymbol = Symbol();
|
|
30
30
|
|
|
31
31
|
export class ContextAwareService {
|
|
32
|
-
|
|
33
32
|
@Inject()
|
|
34
33
|
context: AsyncContext;
|
|
35
34
|
|
|
@@ -54,7 +53,7 @@ export class ContextAwareService {
|
|
|
54
53
|
```
|
|
55
54
|
|
|
56
55
|
## AsyncContextValue
|
|
57
|
-
Within the framework that is a need to access context values, in a type safe fashion.
|
|
56
|
+
Within the framework that is a need to access context values, in a type safe fashion. Additionally, we have the requirement to keep the data accesses isolated from other operations. To this end, [AsyncContextValue](https://github.com/travetto/travetto/tree/main/module/context/src/value.ts#L16) was created to support this use case. This class represents the ability to define a simple read/write contract for a given context field. It also provides some supplemental functionality, e.g., the ability to suppress errors if a context is not initialized.
|
|
58
57
|
|
|
59
58
|
**Code: Source for AsyncContextValue**
|
|
60
59
|
```typescript
|
|
@@ -77,7 +76,6 @@ import { type AsyncContext, AsyncContextValue, WithAsyncContext } from '@travett
|
|
|
77
76
|
import { Inject } from '@travetto/di';
|
|
78
77
|
|
|
79
78
|
export class ContextValueService {
|
|
80
|
-
|
|
81
79
|
@Inject()
|
|
82
80
|
context: AsyncContext;
|
|
83
81
|
|
package/__index__.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Async-aware state management, maintaining context across asynchronous calls.",
|
|
6
6
|
"keywords": [
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"directory": "module/context"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/di": "^8.0.0-alpha.
|
|
30
|
+
"@travetto/di": "^8.0.0-alpha.21"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
33
|
+
"@travetto/test": "^8.0.0-alpha.22"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/test": {
|
package/src/decorator.ts
CHANGED
package/src/service.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
3
|
import { Injectable } from '@travetto/di';
|
|
4
|
-
import {
|
|
4
|
+
import { AsyncQueue, castTo, RuntimeError } from '@travetto/runtime';
|
|
5
5
|
|
|
6
6
|
type Ctx<T = unknown> = Record<string | symbol, T>;
|
|
7
7
|
|
|
@@ -10,7 +10,6 @@ type Ctx<T = unknown> = Record<string | symbol, T>;
|
|
|
10
10
|
*/
|
|
11
11
|
@Injectable()
|
|
12
12
|
export class AsyncContext {
|
|
13
|
-
|
|
14
13
|
storage = new AsyncLocalStorage<Ctx>();
|
|
15
14
|
|
|
16
15
|
constructor() {
|
|
@@ -79,4 +78,4 @@ export class AsyncContext {
|
|
|
79
78
|
});
|
|
80
79
|
return out;
|
|
81
80
|
}
|
|
82
|
-
}
|
|
81
|
+
}
|
package/src/value.ts
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
1
|
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { castTo, RuntimeError } from '@travetto/runtime';
|
|
4
4
|
|
|
5
5
|
type Payload<T> = Record<string | symbol, T | undefined>;
|
|
6
6
|
type Storage<T = unknown> = AsyncLocalStorage<Payload<T>>;
|
|
7
7
|
type Key = string | symbol;
|
|
8
8
|
type StorageSource = Storage | (() => Storage) | { storage: Storage } | { context: { storage: Storage } };
|
|
9
9
|
|
|
10
|
-
type ReadWriteConfig = { read?: boolean
|
|
10
|
+
type ReadWriteConfig = { read?: boolean; write?: boolean };
|
|
11
11
|
type ContextConfig = { failIfUnbound?: ReadWriteConfig };
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Async Context Value
|
|
15
15
|
*/
|
|
16
16
|
export class AsyncContextValue<T = unknown> {
|
|
17
|
-
|
|
18
17
|
#source: () => Storage<T>;
|
|
19
18
|
#storage?: Storage<T>;
|
|
20
19
|
#key: Key = Symbol();
|
|
21
20
|
#failIfUnbound: ReadWriteConfig;
|
|
22
21
|
|
|
23
22
|
constructor(source: StorageSource, config?: ContextConfig) {
|
|
24
|
-
this.#source = castTo(
|
|
25
|
-
source
|
|
26
|
-
|
|
27
|
-
source :
|
|
28
|
-
('storage' in source ?
|
|
29
|
-
source.storage :
|
|
30
|
-
source.context.storage
|
|
31
|
-
))
|
|
23
|
+
this.#source = castTo(
|
|
24
|
+
typeof source === 'function'
|
|
25
|
+
? source
|
|
26
|
+
: (): Storage => ('getStore' in source ? source : 'storage' in source ? source.storage : source.context.storage)
|
|
32
27
|
);
|
|
33
28
|
this.#failIfUnbound = { read: true, write: true, ...config?.failIfUnbound };
|
|
34
29
|
}
|
package/support/test/context.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DependencyRegistryIndex } from '@travetto/di';
|
|
2
|
-
import { castKey, castTo, type Class } from '@travetto/runtime';
|
|
3
2
|
import { Registry } from '@travetto/registry';
|
|
4
|
-
import {
|
|
3
|
+
import { type Class, castKey, castTo } from '@travetto/runtime';
|
|
4
|
+
import { type SuitePhaseHandler, SuiteRegistryIndex } from '@travetto/test';
|
|
5
5
|
|
|
6
6
|
import { AsyncContext } from '../../src/service.ts';
|
|
7
7
|
|
|
@@ -19,7 +19,9 @@ class ContextSuiteHandler<T extends object> implements SuitePhaseHandler<T> {
|
|
|
19
19
|
const methodName = castKey<typeof instance>(test.methodName);
|
|
20
20
|
if (methodName in instance && typeof instance[methodName] === 'function') {
|
|
21
21
|
const og = instance[methodName];
|
|
22
|
-
const wrapped = function (this: unknown) {
|
|
22
|
+
const wrapped = function (this: unknown) {
|
|
23
|
+
return ctx.run(og.bind(this));
|
|
24
|
+
};
|
|
23
25
|
Object.defineProperty(wrapped, 'name', { value: test.methodName });
|
|
24
26
|
instance[methodName] = castTo(wrapped);
|
|
25
27
|
}
|
|
@@ -37,4 +39,4 @@ export function WithSuiteContext() {
|
|
|
37
39
|
phaseHandlers: [new ContextSuiteHandler(target)]
|
|
38
40
|
});
|
|
39
41
|
};
|
|
40
|
-
}
|
|
42
|
+
}
|