@travetto/context 5.1.0 → 6.0.0-rc.1
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/package.json +3 -3
- package/src/value.ts +12 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-rc.1",
|
|
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": "^6.0.0-rc.1"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^
|
|
32
|
+
"@travetto/test": "^6.0.0-rc.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependenciesMeta": {
|
|
35
35
|
"@travetto/test": {
|
package/src/value.ts
CHANGED
|
@@ -6,9 +6,9 @@ type Storage<T = unknown> = AsyncLocalStorage<Payload<T>>;
|
|
|
6
6
|
type Key = string | symbol;
|
|
7
7
|
type StorageSource = Storage | (() => Storage) | { storage: Storage } | { context: { storage: Storage } };
|
|
8
8
|
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
};
|
|
9
|
+
type ReadWriteConfig = { read?: boolean, write?: boolean };
|
|
10
|
+
|
|
11
|
+
type ContextConfig = { failIfUnbound?: ReadWriteConfig };
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Async Context Value
|
|
@@ -18,7 +18,7 @@ export class AsyncContextValue<T = unknown> {
|
|
|
18
18
|
#source: () => Storage<T>;
|
|
19
19
|
#storage?: Storage<T>;
|
|
20
20
|
#key: Key = Symbol();
|
|
21
|
-
#failIfUnbound:
|
|
21
|
+
#failIfUnbound: ReadWriteConfig;
|
|
22
22
|
|
|
23
23
|
constructor(source: StorageSource, config?: ContextConfig) {
|
|
24
24
|
this.#source = castTo(typeof source === 'function' ?
|
|
@@ -30,12 +30,12 @@ export class AsyncContextValue<T = unknown> {
|
|
|
30
30
|
source.context.storage
|
|
31
31
|
))
|
|
32
32
|
);
|
|
33
|
-
this.#failIfUnbound = config?.failIfUnbound
|
|
33
|
+
this.#failIfUnbound = { read: true, write: true, ...config?.failIfUnbound };
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
#store(mode: keyof ReadWriteConfig): Payload<T> | undefined {
|
|
37
37
|
const store = (this.#storage ??= this.#source()).getStore();
|
|
38
|
-
if (!store && this.#failIfUnbound) {
|
|
38
|
+
if (!store && this.#failIfUnbound[mode]) {
|
|
39
39
|
throw new AppError('Context not initialized');
|
|
40
40
|
}
|
|
41
41
|
return store;
|
|
@@ -45,14 +45,17 @@ export class AsyncContextValue<T = unknown> {
|
|
|
45
45
|
* Get value
|
|
46
46
|
*/
|
|
47
47
|
get(): T | undefined {
|
|
48
|
-
|
|
48
|
+
const store = this.#store('read');
|
|
49
|
+
if (store) {
|
|
50
|
+
return store[this.#key];
|
|
51
|
+
}
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
/**
|
|
52
55
|
* Set value
|
|
53
56
|
*/
|
|
54
57
|
set(value: T | undefined): void {
|
|
55
|
-
const store = this.#store;
|
|
58
|
+
const store = this.#store('write');
|
|
56
59
|
if (store) {
|
|
57
60
|
store[this.#key] = value;
|
|
58
61
|
}
|