@twin.org/context 0.0.3-next.2 → 0.0.3-next.4

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.
@@ -1,4 +1,4 @@
1
- import { BaseError, GeneralError } from "@twin.org/core";
1
+ import { BaseError, GeneralError, Is, SharedStore } from "@twin.org/core";
2
2
  /**
3
3
  * Class to maintain context ids and execute an async method.
4
4
  */
@@ -7,10 +7,6 @@ export class ContextIdStore {
7
7
  * Runtime name for the class.
8
8
  */
9
9
  static CLASS_NAME = "ContextIdStore";
10
- /**
11
- * The async local storage for the context ids.
12
- */
13
- static _storage;
14
10
  /**
15
11
  * Execute the method wrapped in the context.
16
12
  * @param contextIds The context IDs.
@@ -18,32 +14,37 @@ export class ContextIdStore {
18
14
  * @returns Nothing.
19
15
  */
20
16
  static async run(contextIds, asyncMethod) {
21
- return (await ContextIdStore.createStorage()).run(contextIds, asyncMethod);
17
+ const storage = await ContextIdStore.getStorage();
18
+ return storage.run(contextIds, asyncMethod);
22
19
  }
23
20
  /**
24
21
  * Get the context IDs.
25
22
  * @returns The context IDs.
26
23
  */
27
24
  static async getContextIds() {
28
- return (await ContextIdStore.createStorage()).getStore();
25
+ const storage = await ContextIdStore.getStorage();
26
+ return storage.getStore();
29
27
  }
30
28
  /**
31
- * Create the storage if it doesn't exist.
29
+ * Get the storage and create it if it doesn't exist.
32
30
  * @returns The storage.
33
31
  */
34
- static async createStorage() {
35
- if (!ContextIdStore._storage) {
32
+ static async getStorage() {
33
+ let asyncHooksStore = SharedStore.get("asyncHooks");
34
+ if (Is.empty(asyncHooksStore?.contextIds)) {
36
35
  try {
37
36
  const hooks = await import("node:async_hooks");
38
- ContextIdStore._storage = new hooks.AsyncLocalStorage({
37
+ asyncHooksStore = asyncHooksStore ?? {};
38
+ asyncHooksStore.contextIds = new hooks.AsyncLocalStorage({
39
39
  name: "AsyncContextIdsStorage"
40
40
  });
41
41
  }
42
42
  catch (err) {
43
43
  throw new GeneralError(ContextIdStore.CLASS_NAME, "asyncHooksNotAvailable", undefined, BaseError.fromError(err));
44
44
  }
45
+ SharedStore.set("asyncHooks", asyncHooksStore);
45
46
  }
46
- return ContextIdStore._storage;
47
+ return asyncHooksStore.contextIds;
47
48
  }
48
49
  }
49
50
  //# sourceMappingURL=contextIdStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"contextIdStore.js","sourceRoot":"","sources":["../../../src/utils/contextIdStore.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAIzD;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;OAEG;IACK,MAAM,CAAC,QAAQ,CAAiC;IAExD;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAc,UAAuB,EAAE,WAAoB;QACjF,OAAO,CAAC,MAAM,cAAc,CAAC,aAAa,EAAE,CAAC,CAAC,GAAG,CAAI,UAAU,EAAE,WAAW,CAAC,CAAC;IAC/E,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa;QAChC,OAAO,CAAC,MAAM,cAAc,CAAC,aAAa,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;IAC1D,CAAC;IAED;;;OAGG;IACK,MAAM,CAAC,KAAK,CAAC,aAAa;QACjC,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC;gBACJ,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAC/C,cAAc,CAAC,QAAQ,GAAG,IAAI,KAAK,CAAC,iBAAiB,CAAc;oBAClE,IAAI,EAAE,wBAAwB;iBAC9B,CAAC,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,wBAAwB,EACxB,SAAS,EACT,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CACxB,CAAC;YACH,CAAC;QACF,CAAC;QACD,OAAO,cAAc,CAAC,QAAQ,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { BaseError, GeneralError } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to maintain context ids and execute an async method.\n */\nexport class ContextIdStore {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdStore>();\n\n\t/**\n\t * The async local storage for the context ids.\n\t */\n\tprivate static _storage: AsyncLocalStorage<IContextIds>;\n\n\t/**\n\t * Execute the method wrapped in the context.\n\t * @param contextIds The context IDs.\n\t * @param asyncMethod The async method to run.\n\t * @returns Nothing.\n\t */\n\tpublic static async run<T = unknown>(contextIds: IContextIds, asyncMethod: () => T): Promise<T> {\n\t\treturn (await ContextIdStore.createStorage()).run<T>(contextIds, asyncMethod);\n\t}\n\n\t/**\n\t * Get the context IDs.\n\t * @returns The context IDs.\n\t */\n\tpublic static async getContextIds(): Promise<IContextIds | undefined> {\n\t\treturn (await ContextIdStore.createStorage()).getStore();\n\t}\n\n\t/**\n\t * Create the storage if it doesn't exist.\n\t * @returns The storage.\n\t */\n\tprivate static async createStorage(): Promise<AsyncLocalStorage<IContextIds>> {\n\t\tif (!ContextIdStore._storage) {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\tContextIdStore._storage = new hooks.AsyncLocalStorage<IContextIds>({\n\t\t\t\t\tname: \"AsyncContextIdsStorage\"\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tContextIdStore.CLASS_NAME,\n\t\t\t\t\t\"asyncHooksNotAvailable\",\n\t\t\t\t\tundefined,\n\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn ContextIdStore._storage;\n\t}\n}\n"]}
1
+ {"version":3,"file":"contextIdStore.js","sourceRoot":"","sources":["../../../src/utils/contextIdStore.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAI1E;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAc,UAAuB,EAAE,WAAoB;QACjF,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,UAAU,EAAE,CAAC;QAClD,OAAO,OAAO,CAAC,GAAG,CAAI,UAAU,EAAE,WAAW,CAAC,CAAC;IAChD,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa;QAChC,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,UAAU,EAAE,CAAC;QAClD,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,UAAU;QAC7B,IAAI,eAAe,GAAG,WAAW,CAAC,GAAG,CACpC,YAAY,CACZ,CAAC;QAEF,IAAI,EAAE,CAAC,KAAK,CAAC,eAAe,EAAE,UAAU,CAAC,EAAE,CAAC;YAC3C,IAAI,CAAC;gBACJ,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAC/C,eAAe,GAAG,eAAe,IAAI,EAAE,CAAC;gBACxC,eAAe,CAAC,UAAU,GAAG,IAAI,KAAK,CAAC,iBAAiB,CAAc;oBACrE,IAAI,EAAE,wBAAwB;iBAC9B,CAAC,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,wBAAwB,EACxB,SAAS,EACT,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CACxB,CAAC;YACH,CAAC;YACD,WAAW,CAAC,GAAG,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC;QAChD,CAAC;QACD,OAAO,eAAe,CAAC,UAAU,CAAC;IACnC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { BaseError, GeneralError, Is, SharedStore } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to maintain context ids and execute an async method.\n */\nexport class ContextIdStore {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdStore>();\n\n\t/**\n\t * Execute the method wrapped in the context.\n\t * @param contextIds The context IDs.\n\t * @param asyncMethod The async method to run.\n\t * @returns Nothing.\n\t */\n\tpublic static async run<T = unknown>(contextIds: IContextIds, asyncMethod: () => T): Promise<T> {\n\t\tconst storage = await ContextIdStore.getStorage();\n\t\treturn storage.run<T>(contextIds, asyncMethod);\n\t}\n\n\t/**\n\t * Get the context IDs.\n\t * @returns The context IDs.\n\t */\n\tpublic static async getContextIds(): Promise<IContextIds | undefined> {\n\t\tconst storage = await ContextIdStore.getStorage();\n\t\treturn storage.getStore();\n\t}\n\n\t/**\n\t * Get the storage and create it if it doesn't exist.\n\t * @returns The storage.\n\t */\n\tpublic static async getStorage(): Promise<AsyncLocalStorage<IContextIds>> {\n\t\tlet asyncHooksStore = SharedStore.get<{ contextIds?: AsyncLocalStorage<IContextIds> }>(\n\t\t\t\"asyncHooks\"\n\t\t);\n\n\t\tif (Is.empty(asyncHooksStore?.contextIds)) {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\tasyncHooksStore = asyncHooksStore ?? {};\n\t\t\t\tasyncHooksStore.contextIds = new hooks.AsyncLocalStorage<IContextIds>({\n\t\t\t\t\tname: \"AsyncContextIdsStorage\"\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tContextIdStore.CLASS_NAME,\n\t\t\t\t\t\"asyncHooksNotAvailable\",\n\t\t\t\t\tundefined,\n\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t);\n\t\t\t}\n\t\t\tSharedStore.set(\"asyncHooks\", asyncHooksStore);\n\t\t}\n\t\treturn asyncHooksStore.contextIds;\n\t}\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import type { AsyncLocalStorage } from "node:async_hooks";
1
2
  import type { IContextIds } from "../models/IContextIds.js";
2
3
  /**
3
4
  * Class to maintain context ids and execute an async method.
@@ -7,10 +8,6 @@ export declare class ContextIdStore {
7
8
  * Runtime name for the class.
8
9
  */
9
10
  static readonly CLASS_NAME: string;
10
- /**
11
- * The async local storage for the context ids.
12
- */
13
- private static _storage;
14
11
  /**
15
12
  * Execute the method wrapped in the context.
16
13
  * @param contextIds The context IDs.
@@ -24,8 +21,8 @@ export declare class ContextIdStore {
24
21
  */
25
22
  static getContextIds(): Promise<IContextIds | undefined>;
26
23
  /**
27
- * Create the storage if it doesn't exist.
24
+ * Get the storage and create it if it doesn't exist.
28
25
  * @returns The storage.
29
26
  */
30
- private static createStorage;
27
+ static getStorage(): Promise<AsyncLocalStorage<IContextIds>>;
31
28
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.0.3-next.4](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.3...context-v0.0.3-next.4) (2025-11-13)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * use singleton pattern for context storage ([c69f358](https://github.com/twinfoundation/framework/commit/c69f358e45361b45d4e46f19846cd5b8c99b0ccd))
9
+ * use singleton pattern for context storage ([5cc706a](https://github.com/twinfoundation/framework/commit/5cc706a2bbfc601fa3d00f3efd8b764052e9f91d))
10
+
11
+
12
+ ### Dependencies
13
+
14
+ * The following workspace dependencies were updated
15
+ * dependencies
16
+ * @twin.org/core bumped from 0.0.3-next.3 to 0.0.3-next.4
17
+ * @twin.org/nameof bumped from 0.0.3-next.3 to 0.0.3-next.4
18
+ * devDependencies
19
+ * @twin.org/nameof-transformer bumped from 0.0.3-next.3 to 0.0.3-next.4
20
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.3 to 0.0.3-next.4
21
+ * @twin.org/validate-locales bumped from 0.0.3-next.3 to 0.0.3-next.4
22
+
23
+ ## [0.0.3-next.3](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.2...context-v0.0.3-next.3) (2025-11-12)
24
+
25
+
26
+ ### Miscellaneous Chores
27
+
28
+ * **context:** Synchronize repo versions
29
+
30
+
31
+ ### Dependencies
32
+
33
+ * The following workspace dependencies were updated
34
+ * dependencies
35
+ * @twin.org/core bumped from 0.0.3-next.2 to 0.0.3-next.3
36
+ * @twin.org/nameof bumped from 0.0.3-next.2 to 0.0.3-next.3
37
+ * devDependencies
38
+ * @twin.org/nameof-transformer bumped from 0.0.3-next.2 to 0.0.3-next.3
39
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.2 to 0.0.3-next.3
40
+ * @twin.org/validate-locales bumped from 0.0.3-next.2 to 0.0.3-next.3
41
+
3
42
  ## [0.0.3-next.2](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.1...context-v0.0.3-next.2) (2025-11-12)
4
43
 
5
44
 
@@ -67,3 +67,17 @@ Get the context IDs.
67
67
  `Promise`\<[`IContextIds`](../interfaces/IContextIds.md) \| `undefined`\>
68
68
 
69
69
  The context IDs.
70
+
71
+ ***
72
+
73
+ ### getStorage()
74
+
75
+ > `static` **getStorage**(): `Promise`\<`AsyncLocalStorage`\<[`IContextIds`](../interfaces/IContextIds.md)\>\>
76
+
77
+ Get the storage and create it if it doesn't exist.
78
+
79
+ #### Returns
80
+
81
+ `Promise`\<`AsyncLocalStorage`\<[`IContextIds`](../interfaces/IContextIds.md)\>\>
82
+
83
+ The storage.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/context",
3
- "version": "0.0.3-next.2",
3
+ "version": "0.0.3-next.4",
4
4
  "description": "Helper methods/classes for context handling",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.0.3-next.2",
18
- "@twin.org/nameof": "0.0.3-next.2"
17
+ "@twin.org/core": "0.0.3-next.4",
18
+ "@twin.org/nameof": "0.0.3-next.4"
19
19
  },
20
20
  "main": "./dist/es/index.js",
21
21
  "types": "./dist/types/index.d.ts",