@twin.org/modules 0.0.1-next.64 → 0.0.1-next.65

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.
@@ -91,44 +91,48 @@ class ModuleHelper {
91
91
  */
92
92
  static async execModuleMethodThread(module, method, args) {
93
93
  return new Promise((resolve, reject) => {
94
- const worker = new node_worker_threads.Worker(`
95
- (async () => {
96
- const { workerData, parentPort } = await import('worker_threads');
94
+ const worker = new node_worker_threads.Worker(`(async () => {
95
+ try {
96
+ const { workerData, parentPort } = await import('node:worker_threads');
97
97
 
98
- function rejectError(type, innerError) {
99
- parentPort.postMessage({ errorType: type, innerError });
100
- }
98
+ function rejectError(type, innerError) {
99
+ parentPort.postMessage({ errorType: type, innerError });
100
+ }
101
101
 
102
- function resolveResult(result) {
103
- Promise.resolve(result).then(res => parentPort.postMessage({ result: res }));
104
- }
102
+ async function executeMethod(method) {
103
+ try {
104
+ const result = await method(...(args ?? []));
105
105
 
106
- const { module, method, args } = workerData;
106
+ parentPort.postMessage({ result });
107
+ } catch (err) {
108
+ rejectError('resultError', err);
109
+ }
110
+ }
107
111
 
108
- import(module)
109
- .then(moduleInstance => {
110
- const methodParts = method.split(".");
111
- const moduleEntry = moduleInstance[methodParts[0]];
112
+ const { module, method, args } = workerData;
112
113
 
113
- if (moduleEntry === undefined) {
114
- rejectError("entryNotFound");
115
- } else if (methodParts.length === 2) {
116
- const moduleMethod = moduleEntry[methodParts[1]];
117
- if (typeof moduleMethod === "function") {
118
- resolveResult(moduleMethod(...(args ?? [])));
119
- } else {
120
- rejectError("notFunction");
121
- }
122
- } else if (typeof moduleEntry === "function") {
123
- resolveResult(moduleEntry(...(args ?? [])));
124
- } else {
125
- rejectError("notFunction");
126
- }
127
- })
128
- .catch(err => {
129
- rejectError("moduleNotFound", err);
130
- });
131
- })();
114
+ const moduleInstance = await import(module);
115
+ const methodParts = method.split('.');
116
+ const moduleEntry = moduleInstance[methodParts[0]];
117
+
118
+ if (moduleEntry === undefined) {
119
+ rejectError('entryNotFound');
120
+ } else if (methodParts.length === 2) {
121
+ const moduleMethod = moduleEntry[methodParts[1]];
122
+ if (typeof moduleMethod === 'function') {
123
+ await executeMethod(moduleMethod, args);
124
+ } else {
125
+ rejectError('notFunction');
126
+ }
127
+ } else if (typeof moduleEntry === 'function') {
128
+ await executeMethod(moduleEntry, args);
129
+ } else {
130
+ rejectError('notFunction');
131
+ }
132
+ } catch (err) {
133
+ rejectError('moduleNotFound', err);
134
+ }
135
+ })();
132
136
  `, { eval: true, workerData: { module, method, args: args ?? [] } });
133
137
  worker.on("message", msg => {
134
138
  if (core.Is.stringValue(msg.errorType)) {
@@ -89,44 +89,48 @@ class ModuleHelper {
89
89
  */
90
90
  static async execModuleMethodThread(module, method, args) {
91
91
  return new Promise((resolve, reject) => {
92
- const worker = new Worker(`
93
- (async () => {
94
- const { workerData, parentPort } = await import('worker_threads');
92
+ const worker = new Worker(`(async () => {
93
+ try {
94
+ const { workerData, parentPort } = await import('node:worker_threads');
95
95
 
96
- function rejectError(type, innerError) {
97
- parentPort.postMessage({ errorType: type, innerError });
98
- }
96
+ function rejectError(type, innerError) {
97
+ parentPort.postMessage({ errorType: type, innerError });
98
+ }
99
99
 
100
- function resolveResult(result) {
101
- Promise.resolve(result).then(res => parentPort.postMessage({ result: res }));
102
- }
100
+ async function executeMethod(method) {
101
+ try {
102
+ const result = await method(...(args ?? []));
103
103
 
104
- const { module, method, args } = workerData;
104
+ parentPort.postMessage({ result });
105
+ } catch (err) {
106
+ rejectError('resultError', err);
107
+ }
108
+ }
105
109
 
106
- import(module)
107
- .then(moduleInstance => {
108
- const methodParts = method.split(".");
109
- const moduleEntry = moduleInstance[methodParts[0]];
110
+ const { module, method, args } = workerData;
110
111
 
111
- if (moduleEntry === undefined) {
112
- rejectError("entryNotFound");
113
- } else if (methodParts.length === 2) {
114
- const moduleMethod = moduleEntry[methodParts[1]];
115
- if (typeof moduleMethod === "function") {
116
- resolveResult(moduleMethod(...(args ?? [])));
117
- } else {
118
- rejectError("notFunction");
119
- }
120
- } else if (typeof moduleEntry === "function") {
121
- resolveResult(moduleEntry(...(args ?? [])));
122
- } else {
123
- rejectError("notFunction");
124
- }
125
- })
126
- .catch(err => {
127
- rejectError("moduleNotFound", err);
128
- });
129
- })();
112
+ const moduleInstance = await import(module);
113
+ const methodParts = method.split('.');
114
+ const moduleEntry = moduleInstance[methodParts[0]];
115
+
116
+ if (moduleEntry === undefined) {
117
+ rejectError('entryNotFound');
118
+ } else if (methodParts.length === 2) {
119
+ const moduleMethod = moduleEntry[methodParts[1]];
120
+ if (typeof moduleMethod === 'function') {
121
+ await executeMethod(moduleMethod, args);
122
+ } else {
123
+ rejectError('notFunction');
124
+ }
125
+ } else if (typeof moduleEntry === 'function') {
126
+ await executeMethod(moduleEntry, args);
127
+ } else {
128
+ rejectError('notFunction');
129
+ }
130
+ } catch (err) {
131
+ rejectError('moduleNotFound', err);
132
+ }
133
+ })();
130
134
  `, { eval: true, workerData: { module, method, args: args ?? [] } });
131
135
  worker.on("message", msg => {
132
136
  if (Is.stringValue(msg.errorType)) {
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/modules - Changelog
2
2
 
3
+ ## [0.0.1-next.65](https://github.com/twinfoundation/framework/compare/modules-v0.0.1-next.64...modules-v0.0.1-next.65) (2025-06-19)
4
+
5
+
6
+ ### Features
7
+
8
+ * improve async pattern and error handling ([aaa1f68](https://github.com/twinfoundation/framework/commit/aaa1f6879d60adf04b78b0c1bbbec50f2873f020))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/core bumped from 0.0.1-next.64 to 0.0.1-next.65
16
+
3
17
  ## [0.0.1-next.64](https://github.com/twinfoundation/framework/compare/modules-v0.0.1-next.63...modules-v0.0.1-next.64) (2025-06-19)
4
18
 
5
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/modules",
3
- "version": "0.0.1-next.64",
3
+ "version": "0.0.1-next.65",
4
4
  "description": "Helper classes for loading and executing from modules",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,7 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.0.1-next.64",
17
+ "@twin.org/core": "0.0.1-next.65",
18
18
  "@twin.org/nameof": "next"
19
19
  },
20
20
  "main": "./dist/cjs/index.cjs",