@travetto/context 7.1.4 → 8.0.0-alpha.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/package.json +3 -3
- package/src/service.ts +2 -2
- package/src/value.ts +2 -2
- package/support/test/context.ts +24 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/context",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
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": "^
|
|
30
|
+
"@travetto/di": "^8.0.0-alpha.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/test": "^
|
|
33
|
+
"@travetto/test": "^8.0.0-alpha.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/test": {
|
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 { RuntimeError, AsyncQueue, castTo } from '@travetto/runtime';
|
|
5
5
|
|
|
6
6
|
type Ctx<T = unknown> = Record<string | symbol, T>;
|
|
7
7
|
|
|
@@ -21,7 +21,7 @@ export class AsyncContext {
|
|
|
21
21
|
#get<T = unknown>(): Ctx<T> {
|
|
22
22
|
const store = this.storage.getStore();
|
|
23
23
|
if (!store) {
|
|
24
|
-
throw new
|
|
24
|
+
throw new RuntimeError('Context is not initialized');
|
|
25
25
|
}
|
|
26
26
|
return castTo(store);
|
|
27
27
|
}
|
package/src/value.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { RuntimeError, castTo } from '@travetto/runtime';
|
|
4
4
|
|
|
5
5
|
type Payload<T> = Record<string | symbol, T | undefined>;
|
|
6
6
|
type Storage<T = unknown> = AsyncLocalStorage<Payload<T>>;
|
|
@@ -36,7 +36,7 @@ export class AsyncContextValue<T = unknown> {
|
|
|
36
36
|
#store(mode: keyof ReadWriteConfig): Payload<T> | undefined {
|
|
37
37
|
const store = (this.#storage ??= this.#source()).getStore();
|
|
38
38
|
if (!store && this.#failIfUnbound[mode]) {
|
|
39
|
-
throw new
|
|
39
|
+
throw new RuntimeError('Context not initialized');
|
|
40
40
|
}
|
|
41
41
|
return store;
|
|
42
42
|
}
|
package/support/test/context.ts
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
import { DependencyRegistryIndex } from '@travetto/di';
|
|
2
|
-
import type
|
|
2
|
+
import { castKey, castTo, type Class } from '@travetto/runtime';
|
|
3
3
|
import { Registry } from '@travetto/registry';
|
|
4
|
-
import { SuiteRegistryIndex } from '@travetto/test';
|
|
4
|
+
import { SuiteRegistryIndex, type SuitePhaseHandler } from '@travetto/test';
|
|
5
5
|
|
|
6
6
|
import { AsyncContext } from '../../src/service.ts';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
class ContextSuiteHandler<T extends object> implements SuitePhaseHandler<T> {
|
|
9
|
+
target: Class<T>;
|
|
10
|
+
|
|
11
|
+
constructor(target: Class<T>) {
|
|
12
|
+
this.target = target;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async beforeAll(instance: T) {
|
|
16
|
+
await Registry.init();
|
|
17
|
+
const ctx = await DependencyRegistryIndex.getInstance(AsyncContext);
|
|
18
|
+
for (const test of Object.values(SuiteRegistryIndex.getConfig(this.target).tests)) {
|
|
19
|
+
const methodName = castKey<typeof instance>(test.methodName);
|
|
20
|
+
if (methodName in instance && typeof instance[methodName] === 'function') {
|
|
21
|
+
const og = instance[methodName];
|
|
22
|
+
const wrapped = function (this: unknown) { return ctx.run(og.bind(this)); };
|
|
23
|
+
Object.defineProperty(wrapped, 'name', { value: test.methodName });
|
|
24
|
+
instance[methodName] = castTo(wrapped);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
9
29
|
|
|
10
30
|
/**
|
|
11
31
|
* Allows for defining a common suite context
|
|
@@ -13,27 +33,8 @@ const Init = Symbol();
|
|
|
13
33
|
*/
|
|
14
34
|
export function WithSuiteContext() {
|
|
15
35
|
return (target: Class): void => {
|
|
16
|
-
function wrapped(ctx: AsyncContext, og: Function) {
|
|
17
|
-
return function (this: unknown) {
|
|
18
|
-
return ctx.run(og.bind(this));
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
36
|
SuiteRegistryIndex.getForRegister(target).register({
|
|
23
|
-
|
|
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
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
]
|
|
37
|
+
phaseHandlers: [new ContextSuiteHandler(target)]
|
|
37
38
|
});
|
|
38
39
|
};
|
|
39
40
|
}
|