@travetto/context 2.1.4 → 2.1.5
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/service.ts +6 -29
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
3
|
"displayName": "Async Context",
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.1.5",
|
|
5
5
|
"description": "Async-aware state management, maintaining context across asynchronous calls.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"async-hooks",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"directory": "module/context"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/di": "^2.1.
|
|
30
|
+
"@travetto/di": "^2.1.5"
|
|
31
31
|
},
|
|
32
32
|
"optionalPeerDependencies": {
|
|
33
|
-
"@travetto/rest": "^2.1.
|
|
33
|
+
"@travetto/rest": "^2.1.5"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
package/src/service.ts
CHANGED
|
@@ -14,7 +14,6 @@ type Ctx = Record<string | symbol, unknown>;
|
|
|
14
14
|
export class AsyncContext {
|
|
15
15
|
|
|
16
16
|
alStorage = new AsyncLocalStorage<{ value: Ctx }>();
|
|
17
|
-
active = 0;
|
|
18
17
|
|
|
19
18
|
constructor() {
|
|
20
19
|
this.run = this.run.bind(this);
|
|
@@ -63,43 +62,21 @@ export class AsyncContext {
|
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
|
|
65
|
+
#context(ctx: Ctx = {}): { value: Ctx } {
|
|
66
|
+
return { value: this.alStorage.getStore() ? { ...this.#store(), ...ctx } : ctx };
|
|
67
|
+
}
|
|
68
|
+
|
|
66
69
|
/**
|
|
67
70
|
* Run an async function and ensure the context is available during execution
|
|
68
71
|
*/
|
|
69
72
|
async run<T = unknown>(fn: () => Promise<T>, init: Ctx = {}): Promise<T> {
|
|
70
|
-
|
|
71
|
-
init = { ...this.#store(), ...init };
|
|
72
|
-
}
|
|
73
|
-
this.active += 1;
|
|
74
|
-
this.alStorage.enterWith({ value: init });
|
|
75
|
-
try {
|
|
76
|
-
return await fn();
|
|
77
|
-
} finally {
|
|
78
|
-
// @ts-expect-error
|
|
79
|
-
delete this.alStorage.getStore().value;
|
|
80
|
-
if ((this.active -= 1) === 0) {
|
|
81
|
-
this.alStorage.disable();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
73
|
+
return await this.alStorage.run(this.#context(init), fn);
|
|
84
74
|
}
|
|
85
75
|
|
|
86
76
|
/**
|
|
87
77
|
* Run an async function and ensure the context is available during execution
|
|
88
78
|
*/
|
|
89
79
|
async * iterate<T>(fn: () => AsyncGenerator<T>, init: Ctx = {}): AsyncGenerator<T> {
|
|
90
|
-
|
|
91
|
-
init = { ...this.#store(), ...init };
|
|
92
|
-
}
|
|
93
|
-
this.active += 1;
|
|
94
|
-
this.alStorage.enterWith({ value: init });
|
|
95
|
-
try {
|
|
96
|
-
return yield* fn();
|
|
97
|
-
} finally {
|
|
98
|
-
// @ts-expect-error
|
|
99
|
-
delete this.alStorage.getStore().value;
|
|
100
|
-
if ((this.active -= 1) === 0) {
|
|
101
|
-
this.alStorage.disable();
|
|
102
|
-
}
|
|
103
|
-
}
|
|
80
|
+
return yield* this.alStorage.run(this.#context(init), fn);
|
|
104
81
|
}
|
|
105
82
|
}
|