@travetto/context 5.0.0-rc.8 → 5.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 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#L6) 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#L13) member, as it is the source of the contextual information.
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#L7) 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#L13) 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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/context",
3
- "version": "5.0.0-rc.8",
3
+ "version": "5.0.0",
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": "^5.0.0-rc.8"
29
+ "@travetto/di": "^5.0.0"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/test": "^5.0.0-rc.8"
32
+ "@travetto/test": "^5.0.0"
33
33
  },
34
34
  "peerDependenciesMeta": {
35
35
  "@travetto/test": {
package/src/decorator.ts CHANGED
@@ -1,17 +1,17 @@
1
+ import { AsyncMethodDescriptor } from '@travetto/runtime';
1
2
  import { AsyncContext } from './service';
2
3
 
3
4
  /**
4
5
  * Allows running a function while providing an async context
5
6
  */
6
- export function WithAsyncContext<T extends { context: AsyncContext }>(data?: Record<string, unknown>) {
7
- return function <U extends unknown[], V = unknown>(
7
+ export function WithAsyncContext(data?: Record<string, unknown>) {
8
+ return function <T extends { context: AsyncContext }>(
8
9
  target: T,
9
10
  prop: string,
10
- descriptor: TypedPropertyDescriptor<(...args: U) => Promise<V>>
11
+ descriptor: AsyncMethodDescriptor<T>
11
12
  ): typeof descriptor {
12
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
13
- const og = descriptor.value! as (this: T, ...args: unknown[]) => Promise<V>;
14
- descriptor.value = function (this: T, ...args: unknown[]): Promise<V> {
13
+ const og = descriptor.value!;
14
+ descriptor.value = function (...args: unknown[]): ReturnType<typeof og> {
15
15
  return this.context.run(og.bind(this, ...args), structuredClone(data ?? {}));
16
16
  };
17
17
 
package/src/service.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
 
3
3
  import { Injectable } from '@travetto/di';
4
- import { AppError } from '@travetto/runtime';
4
+ import { AppError, castTo } from '@travetto/runtime';
5
5
 
6
6
 
7
- type Ctx = Record<string | symbol, unknown>;
7
+ type Ctx<T = unknown> = Record<string | symbol, T>;
8
8
 
9
9
  /**
10
10
  * Async context using `asyncHooks`
@@ -12,7 +12,7 @@ type Ctx = Record<string | symbol, unknown>;
12
12
  @Injectable()
13
13
  export class AsyncContext {
14
14
 
15
- alStorage = new AsyncLocalStorage<{ value: Ctx }>();
15
+ alStorage = new AsyncLocalStorage<{ value?: Ctx }>();
16
16
  active = 0;
17
17
 
18
18
  constructor() {
@@ -20,19 +20,17 @@ export class AsyncContext {
20
20
  this.iterate = this.iterate.bind(this);
21
21
  }
22
22
 
23
- #store(setAs?: Ctx | null): Ctx {
23
+ #store<T = unknown>(setAs?: Ctx<T> | null): Ctx<T> {
24
24
  const val = this.alStorage.getStore();
25
25
  if (!val) {
26
26
  throw new AppError('Context is not initialized', 'general');
27
27
  }
28
28
  if (setAs) {
29
29
  val.value = setAs;
30
- } else {
31
- if (!val.value) {
32
- val.value = {};
33
- }
30
+ } else if (!val.value) {
31
+ val.value = {};
34
32
  }
35
- return val.value;
33
+ return castTo(val.value);
36
34
  }
37
35
 
38
36
  /**
@@ -41,10 +39,9 @@ export class AsyncContext {
41
39
  get<T = unknown>(key: string | symbol): T;
42
40
  get(): Ctx;
43
41
  get<T>(key?: string | symbol): Ctx | T {
44
- const root = this.#store();
42
+ const root = this.#store<T>();
45
43
  if (key) {
46
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
47
- return root[key as string] as T;
44
+ return root[key];
48
45
  } else {
49
46
  return root;
50
47
  }
@@ -75,8 +72,7 @@ export class AsyncContext {
75
72
  try {
76
73
  return await fn();
77
74
  } finally {
78
- // @ts-expect-error
79
- delete this.alStorage.getStore().value;
75
+ delete this.alStorage.getStore()?.value;
80
76
  if ((this.active -= 1) === 0) {
81
77
  this.alStorage.disable();
82
78
  }
@@ -95,8 +91,7 @@ export class AsyncContext {
95
91
  try {
96
92
  return yield* fn();
97
93
  } finally {
98
- // @ts-expect-error
99
- delete this.alStorage.getStore().value;
94
+ delete this.alStorage.getStore()?.value;
100
95
  if ((this.active -= 1) === 0) {
101
96
  this.alStorage.disable();
102
97
  }
@@ -26,7 +26,7 @@ export function WithSuiteContext(data: Record<string, unknown> = {}) {
26
26
  await RootRegistry.init();
27
27
  const ctx = await DependencyRegistry.getInstance(AsyncContext);
28
28
  for (const t of SuiteRegistry.get(target).tests) {
29
- const fn = wrapped(ctx, this[t.methodName] as Function);
29
+ const fn = wrapped(ctx, this[t.methodName]);
30
30
  Object.defineProperty(fn, 'name', { value: t.methodName });
31
31
  this[t.methodName] = fn;
32
32
  }