@highstate/backend 0.2.0 → 0.2.1

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.
@@ -0,0 +1,43 @@
1
+ import { workerData, parentPort } from 'node:worker_threads';
2
+ import { getUnitRegistrations } from '@highstate/contract';
3
+ import { l as loadLibrary } from '../../shared-B55c8XjT.mjs';
4
+ import 'jiti';
5
+
6
+ const library = await loadLibrary(workerData.modulePaths);
7
+ const instances = workerData.instances;
8
+ const instanceIds = workerData.instanceIds;
9
+ const instanceOutputs = {};
10
+ function evaluateInstance(instanceId, instance) {
11
+ let outputs = instanceOutputs[instanceId];
12
+ if (!outputs) {
13
+ outputs = instanceOutputs[instanceId] = {};
14
+ }
15
+ outputs[instance.name] = _evaluateInstance(instance);
16
+ return outputs;
17
+ }
18
+ function _evaluateInstance(instance) {
19
+ const inputs = {};
20
+ for (const [inputName, input] of Object.entries(instance.inputs)) {
21
+ if (!Array.isArray(input)) {
22
+ const outputs = evaluateInstance(input.instanceId, instances[input.instanceId]);
23
+ inputs[inputName] = outputs[input.output];
24
+ continue;
25
+ }
26
+ inputs[inputName] = input.map((input2) => {
27
+ const evaluated = evaluateInstance(input2.instanceId, instances[input2.instanceId]);
28
+ return evaluated[input2.output];
29
+ });
30
+ }
31
+ return library.components[instance.type]({
32
+ name: instance.name,
33
+ args: instance.args,
34
+ inputs
35
+ });
36
+ }
37
+ for (const [instanceId, instance] of Object.entries(instances)) {
38
+ if (instanceIds && !instanceIds.includes(instanceId)) {
39
+ continue;
40
+ }
41
+ evaluateInstance(instanceId, instance);
42
+ }
43
+ parentPort.postMessage(getUnitRegistrations());
@@ -0,0 +1,11 @@
1
+ import { workerData, parentPort } from 'node:worker_threads';
2
+ import { mapValues } from 'remeda';
3
+ import { l as loadLibrary } from '../../shared-B55c8XjT.mjs';
4
+ import '@highstate/contract';
5
+ import 'jiti';
6
+
7
+ const library = await loadLibrary(workerData.modulePaths);
8
+ parentPort.postMessage({
9
+ components: mapValues(library.components, (component) => component.model),
10
+ entities: library.entities
11
+ });
@@ -0,0 +1,36 @@
1
+ import { isComponent, isEntity } from '@highstate/contract';
2
+ import { createJiti } from 'jiti';
3
+
4
+ async function loadLibrary(modulePaths) {
5
+ const jiti = createJiti(import.meta.filename);
6
+ const modules = {};
7
+ for (const modulePath of modulePaths) {
8
+ try {
9
+ modules[modulePath] = await jiti.import(modulePath);
10
+ } catch (error) {
11
+ console.warn(`Failed to load module: ${modulePath}`, error);
12
+ }
13
+ }
14
+ const components = {};
15
+ const entities = {};
16
+ _loadLibrary(modules, components, entities);
17
+ return { components, entities };
18
+ }
19
+ function _loadLibrary(value, components, entities) {
20
+ if (isComponent(value)) {
21
+ components[value.model.type] = value;
22
+ return;
23
+ }
24
+ if (isEntity(value)) {
25
+ entities[value.type] = value;
26
+ return;
27
+ }
28
+ if (typeof value !== "object" || value === null) {
29
+ return;
30
+ }
31
+ for (const key in value) {
32
+ _loadLibrary(value[key], components, entities);
33
+ }
34
+ }
35
+
36
+ export { loadLibrary as l };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/backend",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "build": "pkgroll --clean-dist"
27
27
  },
28
28
  "dependencies": {
29
- "@highstate/contract": "^0.2.0",
29
+ "@highstate/contract": "^0.2.1",
30
30
  "@trpc/server": "^11.0.0-rc.660",
31
31
  "@types/node": "^22.10.1",
32
32
  "better-lock": "^3.2.0",
@@ -54,5 +54,5 @@
54
54
  "pkgroll": "^2.5.1",
55
55
  "rollup": "^4.28.1"
56
56
  },
57
- "gitHead": "41f789ef9d6da623923812bba9783c216605495d"
57
+ "gitHead": "205bae57e7b9e8e8ebe979859df3f18fdb8cfe81"
58
58
  }