@n3rdw1z4rd/core 0.22.0 → 0.23.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.
Files changed (142) hide show
  1. package/README.md +59 -4
  2. package/dist/astar.d.ts +1 -3
  3. package/dist/astar.js +1 -2
  4. package/dist/canvas-renderer.d.ts +1 -3
  5. package/dist/canvas-renderer.js +1 -2
  6. package/dist/clock.d.ts +1 -3
  7. package/dist/clock.js +1 -2
  8. package/dist/color-palette.d.ts +1 -3
  9. package/dist/color-palette.js +1 -2
  10. package/dist/color.d.ts +1 -3
  11. package/dist/color.js +1 -2
  12. package/dist/disposable.d.ts +1 -3
  13. package/dist/disposable.js +1 -2
  14. package/dist/dungeon-generator.d.ts +2 -4
  15. package/dist/dungeon-generator.js +3 -4
  16. package/dist/ecs.d.ts +1 -3
  17. package/dist/ecs.js +1 -2
  18. package/dist/emitter.d.ts +1 -3
  19. package/dist/emitter.js +1 -2
  20. package/dist/event-queue.d.ts +1 -3
  21. package/dist/event-queue.js +1 -2
  22. package/dist/graph/doc.d.ts +2 -4
  23. package/dist/graph/doc.js +1 -2
  24. package/dist/graph/evaluate.d.ts +1 -3
  25. package/dist/graph/evaluate.js +1 -2
  26. package/dist/graph/index.d.ts +4 -4
  27. package/dist/graph/index.js +4 -4
  28. package/dist/graph/node.d.ts +7 -9
  29. package/dist/graph/node.js +1 -2
  30. package/dist/graph/nodes.d.ts +1 -3
  31. package/dist/graph/nodes.js +1 -2
  32. package/dist/graph/script.d.ts +37 -40
  33. package/dist/graph/script.js +14 -60
  34. package/dist/graph/sources.d.ts +1 -3
  35. package/dist/graph/sources.js +1 -2
  36. package/dist/graph/subgraph.d.ts +2 -4
  37. package/dist/graph/subgraph.js +1 -2
  38. package/dist/graph/types.d.ts +3 -5
  39. package/dist/graph/types.js +2 -3
  40. package/dist/graph.cjs +22 -77
  41. package/dist/graph.d.cts +44 -61
  42. package/dist/index.cjs +27 -52
  43. package/dist/index.d.cts +1 -63
  44. package/dist/index.d.ts +10 -10
  45. package/dist/index.js +14 -14
  46. package/dist/input/_input.d.ts +1 -3
  47. package/dist/input/_input.js +1 -2
  48. package/dist/input/action-map.d.ts +1 -3
  49. package/dist/input/action-map.js +1 -2
  50. package/dist/input/bindings.d.ts +1 -3
  51. package/dist/input/bindings.js +1 -2
  52. package/dist/input/gestures.d.ts +1 -3
  53. package/dist/input/gestures.js +1 -2
  54. package/dist/input/index.d.ts +1 -1
  55. package/dist/input/index.js +1 -1
  56. package/dist/input/input-action-map.d.ts +3 -5
  57. package/dist/input/input-action-map.js +3 -4
  58. package/dist/input/input.d.ts +2 -4
  59. package/dist/input/input.js +2 -3
  60. package/dist/log.d.ts +1 -3
  61. package/dist/log.js +1 -2
  62. package/dist/map2d.d.ts +1 -3
  63. package/dist/map2d.js +1 -2
  64. package/dist/{map3d-CHDJiq5T.d.cts → map3d-DRHhbrFn.d.cts} +0 -8
  65. package/dist/map3d.d.ts +1 -3
  66. package/dist/map3d.js +1 -2
  67. package/dist/math.d.ts +1 -3
  68. package/dist/math.js +1 -2
  69. package/dist/misc.d.ts +1 -3
  70. package/dist/misc.js +1 -2
  71. package/dist/observable.d.ts +1 -3
  72. package/dist/observable.js +1 -2
  73. package/dist/particle-renderer.d.ts +1 -3
  74. package/dist/particle-renderer.js +1 -2
  75. package/dist/particle-system-2d.d.ts +1 -3
  76. package/dist/particle-system-2d.js +3 -4
  77. package/dist/random.d.ts +1 -3
  78. package/dist/random.js +1 -2
  79. package/dist/rectangle.d.ts +1 -3
  80. package/dist/rectangle.js +1 -2
  81. package/dist/{rng-Dhqq7owV.cjs → rng-edL6atS3.cjs} +3 -6
  82. package/dist/rng.d.ts +1 -3
  83. package/dist/rng.js +2 -3
  84. package/dist/signal.d.ts +1 -3
  85. package/dist/signal.js +1 -2
  86. package/dist/spatial-partition-2d.d.ts +1 -3
  87. package/dist/spatial-partition-2d.js +1 -2
  88. package/dist/{spatial-partition-3d-CmSCL87Z.cjs → spatial-partition-3d-D1uyJVhM.cjs} +7 -13
  89. package/dist/spatial-partition-3d.d.ts +1 -3
  90. package/dist/spatial-partition-3d.js +1 -2
  91. package/dist/three/adaptive-grid.d.ts +1 -3
  92. package/dist/three/adaptive-grid.js +1 -2
  93. package/dist/three/asteroid-mesh.d.ts +1 -3
  94. package/dist/three/asteroid-mesh.js +1 -2
  95. package/dist/three/camera-rig.d.ts +1 -3
  96. package/dist/three/camera-rig.js +1 -2
  97. package/dist/three/cube-sphere-geometry.d.ts +1 -3
  98. package/dist/three/cube-sphere-geometry.js +1 -2
  99. package/dist/three/ecs.d.ts +1 -3
  100. package/dist/three/ecs.js +1 -2
  101. package/dist/three/editor-camera.d.ts +1 -3
  102. package/dist/three/editor-camera.js +1 -2
  103. package/dist/three/index.d.ts +4 -4
  104. package/dist/three/index.js +1 -1
  105. package/dist/three/particle-system-3d.d.ts +1 -3
  106. package/dist/three/particle-system-3d.js +3 -4
  107. package/dist/three/picking.d.ts +2 -4
  108. package/dist/three/picking.js +1 -2
  109. package/dist/three/renderer.d.ts +1 -3
  110. package/dist/three/renderer.js +1 -2
  111. package/dist/three/schema.d.ts +0 -2
  112. package/dist/three/schema.js +1 -2
  113. package/dist/three/starfield.d.ts +1 -3
  114. package/dist/three/starfield.js +1 -2
  115. package/dist/three/texture-atlas.d.ts +1 -3
  116. package/dist/three/texture-atlas.js +1 -2
  117. package/dist/three/texture-utils.d.ts +1 -3
  118. package/dist/three/texture-utils.js +1 -2
  119. package/dist/three/voxel-geometry.d.ts +2 -4
  120. package/dist/three/voxel-geometry.js +2 -3
  121. package/dist/three/voxel-mesh.d.ts +1 -3
  122. package/dist/three/voxel-mesh.js +1 -2
  123. package/dist/three/world.d.ts +0 -2
  124. package/dist/three/world.js +1 -2
  125. package/dist/three.cjs +18 -34
  126. package/dist/three.d.cts +1 -33
  127. package/dist/tilemap.d.ts +1 -3
  128. package/dist/tilemap.js +1 -2
  129. package/dist/types.d.ts +1 -3
  130. package/dist/vector.d.ts +1 -3
  131. package/dist/vector.js +1 -2
  132. package/dist/vite.cjs +1 -2
  133. package/dist/vite.d.cts +1 -3
  134. package/dist/vite.d.ts +1 -3
  135. package/dist/vite.js +1 -2
  136. package/dist/webgl-renderer.d.ts +1 -3
  137. package/dist/webgl-renderer.js +1 -2
  138. package/dist/webgl.d.ts +1 -3
  139. package/dist/webgl.js +1 -2
  140. package/dist/worker-interface.d.ts +1 -3
  141. package/dist/worker-interface.js +1 -2
  142. package/package.json +6 -6
@@ -1,15 +1,14 @@
1
- import { NodeDef, RunContext } from "./node.js";
2
- //#region src/graph/script.d.ts
1
+ import { NodeDef, NodeSpec } from "./node.js";
3
2
  /**
4
3
  * Script nodes: a node type written as a plain ES module, compiled at runtime.
5
4
  *
6
5
  * ```js
7
- * export const title = 'Ball'; // optional: title, category, description, output, animate
8
- * export const inputs = { at: 'vec2', radius: { type: 'number', default: 8 } };
9
- * export const params = { color: { type: 'color', default: '#e8554e' } };
10
- * export const outputs = { entity: 'entity' };
11
- * export function run({ at, radius }, { color }, ctx) {
12
- * return { entity: { shape: 'ball', x: at.x, y: at.y, r: radius, color } };
6
+ * export const title = 'Lerp'; // optional: title, category, description, output, animate
7
+ * export const inputs = { a: 'number', b: { type: 'number', default: 1 } };
8
+ * export const params = { t: { type: 'number', default: 0.5, min: 0, max: 1, step: 0.05 } };
9
+ * export const outputs = { value: 'number' };
10
+ * export function run({ a, b }, { t }, ctx) {
11
+ * return { value: a + (b - a) * t };
13
12
  * }
14
13
  * ```
15
14
  *
@@ -20,45 +19,43 @@ import { NodeDef, RunContext } from "./node.js";
20
19
  * @module
21
20
  */
22
21
  /**
23
- * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
24
- * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
25
- * starts / stops touching (or overlapping) the one the behaviour is on.
22
+ * Another shape of script module a host understands, besides `run` nodes: a module that exports
23
+ * no `run` but any of `hooks` is compiled into a node with this `run` (and these outputs and
24
+ * category, unless the module declares its own). What the hooks mean is up to the host — e.g. a
25
+ * per-frame `update` it calls later: `run` typically packs the node's params and `ctx.source`
26
+ * into a value the host picks up from the output.
26
27
  *
27
- * @category Graph
28
- */
29
- export declare const BEHAVIOR_HOOKS: readonly ["setup", "step", "enter", "exit"];
30
- /**
31
- * Compiles a script node's source into a node type named `type`.
32
- *
33
- * @category Graph
34
- */
35
- export declare function compileNodeScript(type: string, source: string): Promise<NodeDef>;
36
- /**
37
- * A behaviour: what a behaviour node evaluates to. The host runs `source`'s hooks
38
- * ({@link BEHAVIOR_HOOKS}) against whatever it's attached to, with `params`.
28
+ * ```ts
29
+ * const agent: ModuleKind = {
30
+ * hooks: ['update'],
31
+ * outputs: { agent: 'agent' },
32
+ * run: (inputs, params, ctx) => ({ agent: { params: { ...inputs, ...params }, source: ctx.source } }),
33
+ * };
34
+ * const def = await compileNodeScript('agents/wander', source, { kinds: [agent] });
35
+ * ```
39
36
  *
40
37
  * @category Graph
41
38
  */
42
- export interface BehaviorValue {
43
- kind: 'behavior';
44
- /** the behaviour node's type */
45
- type: string;
46
- /** the node instance (for error reports) */
47
- node: string;
48
- /** its inputs and params at evaluation time */
49
- params: Record<string, unknown>;
50
- /** the module with its hooks (setup / step / enter / exit) */
51
- source: string;
39
+ export interface ModuleKind {
40
+ /** exporting any of these (and no `run`) makes a module this kind */
41
+ hooks: readonly string[];
42
+ /** the node's run */
43
+ run: NodeSpec['run'];
44
+ /** outputs, unless the module exports its own */
45
+ outputs?: NodeSpec['outputs'];
46
+ /** category, unless the module exports its own */
47
+ category?: string;
48
+ }
49
+ /** @category Graph */
50
+ export interface ScriptOptions {
51
+ /** the module kinds this host understands besides `run` nodes (first match wins) */
52
+ kinds?: readonly ModuleKind[];
52
53
  }
53
54
  /**
54
- * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
55
- * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
55
+ * Compiles a script node's source into a node type named `type`.
56
56
  *
57
57
  * @category Graph
58
58
  */
59
- export declare function behaviorRun(inputs: Record<string, unknown>, params: Record<string, unknown>, ctx: RunContext): {
60
- behavior: BehaviorValue;
61
- };
59
+ export declare function compileNodeScript(type: string, source: string, options?: ScriptOptions): Promise<NodeDef>;
62
60
  /** A starting point for a new script node. @category Graph */
63
- export declare const SCRIPT_NODE_TEMPLATE = "// A node is a function with a typed interface.\n// inputs: pins wired from other nodes (an unconnected one uses its widget value / default)\n// params: widgets on the node\n// outputs: what run() returns\n// A \"type[]\" pin takes (or gives) a whole list; a list wired into a plain pin runs\n// run() once per item and collects the results into lists.\n\nexport const inputs = {\n a: { type: 'number', default: 1 },\n b: { type: 'number', default: 2 },\n};\n\nexport const params = {\n scale: { type: 'number', default: 1, min: 0, max: 10, step: 0.1 },\n};\n\nexport const outputs = {\n result: 'number',\n};\n\nexport function run({ a, b }, { scale }, ctx) {\n return { result: (a + b) * scale };\n}\n";
64
- //#endregion
61
+ export declare const SCRIPT_NODE_TEMPLATE = "// A node is a function with a typed interface.\n// inputs: pins wired from other nodes (an unconnected one uses its widget value / default)\n// params: widgets on the node\n// outputs: what run() returns\n// A \"type[]\" pin takes (or gives) a whole list; a list wired into a plain pin runs\n// run() once per item and collects the results into lists.\n\nexport const inputs = {\n a: { type: 'number', default: 1 },\n b: { type: 'number', default: 2 },\n};\n\nexport const params = {\n scale: { type: 'number', default: 1, min: 0, max: 10, step: 0.1 },\n};\n\nexport const outputs = {\n result: 'number',\n};\n\nexport function run({ a, b }, { scale }, ctx) {\n return { result: (a + b) * scale };\n}\n";
@@ -1,54 +1,28 @@
1
1
  import { defineNode } from "./node.js";
2
- //#region src/graph/script.ts
3
- /**
4
- * Script nodes: a node type written as a plain ES module, compiled at runtime.
5
- *
6
- * ```js
7
- * export const title = 'Ball'; // optional: title, category, description, output, animate
8
- * export const inputs = { at: 'vec2', radius: { type: 'number', default: 8 } };
9
- * export const params = { color: { type: 'color', default: '#e8554e' } };
10
- * export const outputs = { entity: 'entity' };
11
- * export function run({ at, radius }, { color }, ctx) {
12
- * return { entity: { shape: 'ball', x: at.x, y: at.y, r: radius, color } };
13
- * }
14
- * ```
15
- *
16
- * The module is imported from a `data:` URL, which works in browsers, workers and Node.
17
- * It runs with the page's (or worker's) privileges — treat shared scripts as untrusted and
18
- * evaluate them in a worker.
19
- *
20
- * @module
21
- */
22
- /**
23
- * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
24
- * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
25
- * starts / stops touching (or overlapping) the one the behaviour is on.
26
- *
27
- * @category Graph
28
- */
29
- const BEHAVIOR_HOOKS = [
30
- "setup",
31
- "step",
32
- "enter",
33
- "exit"
34
- ];
2
+ // src/graph/script.ts
35
3
  /**
36
4
  * Compiles a script node's source into a node type named `type`.
37
5
  *
38
6
  * @category Graph
39
7
  */
40
- async function compileNodeScript(type, source) {
8
+ async function compileNodeScript(type, source, options = {}) {
41
9
  const mod = await import(
42
10
  /* @vite-ignore */
43
11
  `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
44
12
  );
45
13
  const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
46
- if (typeof m.run !== "function" && BEHAVIOR_HOOKS.some((h) => typeof m[h] === "function")) {
47
- m.run = behaviorRun;
48
- m.outputs ??= { behavior: "behavior" };
49
- m.category ??= "behavior";
14
+ if (typeof m.run !== "function") {
15
+ const kind = options.kinds?.find((k) => k.hooks.some((h) => typeof m[h] === "function"));
16
+ if (kind) {
17
+ m.run = kind.run;
18
+ m.outputs ??= kind.outputs;
19
+ m.category ??= kind.category;
20
+ }
21
+ }
22
+ if (typeof m.run !== "function") {
23
+ const hooks = options.kinds?.flatMap((k) => k.hooks) ?? [];
24
+ throw new Error(`script node "${type}": export a run(inputs, params, ctx) function${hooks.length ? ` (or ${hooks.join(" / ")})` : ""}`);
50
25
  }
51
- if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or setup / step / enter / exit for a behaviour)`);
52
26
  return defineNode({
53
27
  type,
54
28
  title: m.title,
@@ -63,25 +37,6 @@ async function compileNodeScript(type, source) {
63
37
  source
64
38
  });
65
39
  }
66
- /**
67
- * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
68
- * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
69
- *
70
- * @category Graph
71
- */
72
- function behaviorRun(inputs, params, ctx) {
73
- if (!ctx.source) throw new Error(`behaviour node ${ctx.type} has no source module`);
74
- return { behavior: {
75
- kind: "behavior",
76
- type: ctx.type,
77
- node: ctx.nodeId,
78
- params: {
79
- ...inputs,
80
- ...params
81
- },
82
- source: ctx.source
83
- } };
84
- }
85
40
  /** A starting point for a new script node. @category Graph */
86
41
  const SCRIPT_NODE_TEMPLATE = `// A node is a function with a typed interface.
87
42
  // inputs: pins wired from other nodes (an unconnected one uses its widget value / default)
@@ -107,5 +62,4 @@ export function run({ a, b }, { scale }, ctx) {
107
62
  return { result: (a + b) * scale };
108
63
  }
109
64
  `;
110
- //#endregion
111
- export { BEHAVIOR_HOOKS, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript };
65
+ export { SCRIPT_NODE_TEMPLATE, compileNodeScript };
@@ -1,4 +1,3 @@
1
- //#region src/graph/sources.d.ts
2
1
  /**
3
2
  * The built-in nodes as script modules: the same interface and behaviour as the TypeScript
4
3
  * definitions in `nodes.ts`, written self-contained so the editor can show a built-in's code
@@ -7,5 +6,4 @@
7
6
  * @module
8
7
  */
9
8
  /** @category Graph */
10
- export declare const BUILTIN_SOURCES: Record<string, string>;
11
- //#endregion
9
+ export declare const BUILTIN_SOURCES: Record<string, string>;
@@ -1,4 +1,4 @@
1
- //#region src/graph/sources.ts
1
+ // src/graph/sources.ts
2
2
  /**
3
3
  * The built-in nodes as script modules: the same interface and behaviour as the TypeScript
4
4
  * definitions in `nodes.ts`, written self-contained so the editor can show a built-in's code
@@ -259,5 +259,4 @@ export function run({ list, every, offset }) {
259
259
  }
260
260
  `
261
261
  };
262
- //#endregion
263
262
  export { BUILTIN_SOURCES };
@@ -1,6 +1,5 @@
1
- import { NodeRegistry, NodeSpec } from "./node.js";
2
1
  import { GraphDoc, GraphNode, SubgraphDef } from "./doc.js";
3
- //#region src/graph/subgraph.d.ts
2
+ import { NodeRegistry, NodeSpec } from "./node.js";
4
3
  /**
5
4
  * Subgraphs: a selection of nodes collapsed into one node type. The inner graph is stored in
6
5
  * `doc.subgraphs[type]` with its interface — every wire that crossed the selection's edge
@@ -32,5 +31,4 @@ export declare function collapseToSubgraph(doc: GraphDoc, registry: NodeRegistry
32
31
  * `defs`: where the definitions are (default: `doc.subgraphs`; pass the top-level document's
33
32
  * when `doc` is itself a subgraph's inner graph).
34
33
  */
35
- export declare function expandSubgraph(doc: GraphDoc, nodeId: string, defs?: Record<string, SubgraphDef> | undefined): string[];
36
- //#endregion
34
+ export declare function expandSubgraph(doc: GraphDoc, nodeId: string, defs?: Record<string, SubgraphDef> | undefined): string[];
@@ -1,6 +1,6 @@
1
1
  import { getNode, graphId } from "./doc.js";
2
2
  import { GraphEvaluator, OUTPUT_LISTS } from "./evaluate.js";
3
- //#region src/graph/subgraph.ts
3
+ // src/graph/subgraph.ts
4
4
  /**
5
5
  * Subgraphs: a selection of nodes collapsed into one node type. The inner graph is stored in
6
6
  * `doc.subgraphs[type]` with its interface — every wire that crossed the selection's edge
@@ -276,5 +276,4 @@ function expandSubgraph(doc, nodeId, defs = doc.subgraphs) {
276
276
  doc.nodes = doc.nodes.filter((n) => n.id !== nodeId);
277
277
  return [...ids.values()];
278
278
  }
279
- //#endregion
280
279
  export { collapseToSubgraph, expandSubgraph, freeSubgraphType, registerSubgraphs, subgraphSpec };
@@ -1,6 +1,5 @@
1
- //#region src/graph/types.d.ts
2
1
  /**
3
- * Pin types. A type is a name (`number`, `vec2`, `entity`…) with a color for the editor;
2
+ * Pin types. A type is a name (`number`, `vec2`, your own…) with a color for the editor;
4
3
  * a trailing `[]` in a port declaration (`vec2[]`) makes the port a **list port** — it takes
5
4
  * or produces a whole list instead of one value at a time (see {@link GraphEvaluator}).
6
5
  *
@@ -48,9 +47,8 @@ export declare class TypeRegistry {
48
47
  all(): PortTypeDef[];
49
48
  }
50
49
  /**
51
- * The default pin types (colors match the apparatus palette).
50
+ * The default pin types. Add your own with `portTypes.define(name, color)`.
52
51
  *
53
52
  * @category Graph
54
53
  */
55
- export declare const portTypes: TypeRegistry;
56
- //#endregion
54
+ export declare const portTypes: TypeRegistry;
@@ -1,4 +1,4 @@
1
- //#region src/graph/types.ts
1
+ // src/graph/types.ts
2
2
  /** @category Graph */
3
3
  function parseType(type) {
4
4
  const t = type.trim();
@@ -52,7 +52,7 @@ var TypeRegistry = class {
52
52
  }
53
53
  };
54
54
  /**
55
- * The default pin types (colors match the apparatus palette).
55
+ * The default pin types. Add your own with `portTypes.define(name, color)`.
56
56
  *
57
57
  * @category Graph
58
58
  */
@@ -63,5 +63,4 @@ portTypes.define("string", "#e86fb0");
63
63
  portTypes.define("color", "#f2a541");
64
64
  portTypes.define("vec2", "#4ea8de");
65
65
  portTypes.define("vec3", "#9b6dde");
66
- //#endregion
67
66
  export { TypeRegistry, parseType, portTypes };
package/dist/graph.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_rng = require("./rng-Dhqq7owV.cjs");
3
- //#region src/graph/types.ts
2
+ const require_rng = require("./rng-edL6atS3.cjs");
3
+ // src/graph/types.ts
4
4
  /** @category Graph */
5
5
  function parseType(type) {
6
6
  const t = type.trim();
@@ -54,7 +54,7 @@ var TypeRegistry = class {
54
54
  }
55
55
  };
56
56
  /**
57
- * The default pin types (colors match the apparatus palette).
57
+ * The default pin types. Add your own with `portTypes.define(name, color)`.
58
58
  *
59
59
  * @category Graph
60
60
  */
@@ -65,8 +65,7 @@ portTypes.define("string", "#e86fb0");
65
65
  portTypes.define("color", "#f2a541");
66
66
  portTypes.define("vec2", "#4ea8de");
67
67
  portTypes.define("vec3", "#9b6dde");
68
- //#endregion
69
- //#region src/graph/doc.ts
68
+ // src/graph/doc.ts
70
69
  /**
71
70
  * The graph document: plain JSON, saved and shared as-is.
72
71
  *
@@ -236,8 +235,7 @@ function reaches(doc, start, target) {
236
235
  function clone(v) {
237
236
  return v !== null && typeof v === "object" ? structuredClone(v) : v;
238
237
  }
239
- //#endregion
240
- //#region src/graph/evaluate.ts
238
+ // src/graph/evaluate.ts
241
239
  /**
242
240
  * Evaluates a graph: runs the nodes the outputs depend on, in dependency order, passing
243
241
  * values along the links.
@@ -553,8 +551,7 @@ function safeKey(parts) {
553
551
  }
554
552
  }
555
553
  const now = () => typeof performance !== "undefined" ? performance.now() : Date.now();
556
- //#endregion
557
- //#region src/graph/node.ts
554
+ // src/graph/node.ts
558
555
  let revisionCounter = 0;
559
556
  function port(name, spec) {
560
557
  const s = typeof spec === "string" ? { type: spec } : spec;
@@ -645,8 +642,7 @@ var NodeRegistry = class {
645
642
  return () => this.listeners.delete(listener);
646
643
  }
647
644
  };
648
- //#endregion
649
- //#region src/graph/sources.ts
645
+ // src/graph/sources.ts
650
646
  /**
651
647
  * The built-in nodes as script modules: the same interface and behaviour as the TypeScript
652
648
  * definitions in `nodes.ts`, written self-contained so the editor can show a built-in's code
@@ -907,8 +903,7 @@ export function run({ list, every, offset }) {
907
903
  }
908
904
  `
909
905
  };
910
- //#endregion
911
- //#region src/graph/nodes.ts
906
+ // src/graph/nodes.ts
912
907
  const v2 = (x = 0, y = 0) => ({
913
908
  x,
914
909
  y
@@ -1302,57 +1297,30 @@ function createNodeRegistry() {
1302
1297
  });
1303
1298
  return registry;
1304
1299
  }
1305
- //#endregion
1306
- //#region src/graph/script.ts
1307
- /**
1308
- * Script nodes: a node type written as a plain ES module, compiled at runtime.
1309
- *
1310
- * ```js
1311
- * export const title = 'Ball'; // optional: title, category, description, output, animate
1312
- * export const inputs = { at: 'vec2', radius: { type: 'number', default: 8 } };
1313
- * export const params = { color: { type: 'color', default: '#e8554e' } };
1314
- * export const outputs = { entity: 'entity' };
1315
- * export function run({ at, radius }, { color }, ctx) {
1316
- * return { entity: { shape: 'ball', x: at.x, y: at.y, r: radius, color } };
1317
- * }
1318
- * ```
1319
- *
1320
- * The module is imported from a `data:` URL, which works in browsers, workers and Node.
1321
- * It runs with the page's (or worker's) privileges — treat shared scripts as untrusted and
1322
- * evaluate them in a worker.
1323
- *
1324
- * @module
1325
- */
1326
- /**
1327
- * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
1328
- * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
1329
- * starts / stops touching (or overlapping) the one the behaviour is on.
1330
- *
1331
- * @category Graph
1332
- */
1333
- const BEHAVIOR_HOOKS = [
1334
- "setup",
1335
- "step",
1336
- "enter",
1337
- "exit"
1338
- ];
1300
+ // src/graph/script.ts
1339
1301
  /**
1340
1302
  * Compiles a script node's source into a node type named `type`.
1341
1303
  *
1342
1304
  * @category Graph
1343
1305
  */
1344
- async function compileNodeScript(type, source) {
1306
+ async function compileNodeScript(type, source, options = {}) {
1345
1307
  const mod = await import(
1346
1308
  /* @vite-ignore */
1347
1309
  `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
1348
1310
  );
1349
1311
  const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
1350
- if (typeof m.run !== "function" && BEHAVIOR_HOOKS.some((h) => typeof m[h] === "function")) {
1351
- m.run = behaviorRun;
1352
- m.outputs ??= { behavior: "behavior" };
1353
- m.category ??= "behavior";
1312
+ if (typeof m.run !== "function") {
1313
+ const kind = options.kinds?.find((k) => k.hooks.some((h) => typeof m[h] === "function"));
1314
+ if (kind) {
1315
+ m.run = kind.run;
1316
+ m.outputs ??= kind.outputs;
1317
+ m.category ??= kind.category;
1318
+ }
1319
+ }
1320
+ if (typeof m.run !== "function") {
1321
+ const hooks = options.kinds?.flatMap((k) => k.hooks) ?? [];
1322
+ throw new Error(`script node "${type}": export a run(inputs, params, ctx) function${hooks.length ? ` (or ${hooks.join(" / ")})` : ""}`);
1354
1323
  }
1355
- if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or setup / step / enter / exit for a behaviour)`);
1356
1324
  return defineNode({
1357
1325
  type,
1358
1326
  title: m.title,
@@ -1367,25 +1335,6 @@ async function compileNodeScript(type, source) {
1367
1335
  source
1368
1336
  });
1369
1337
  }
1370
- /**
1371
- * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
1372
- * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
1373
- *
1374
- * @category Graph
1375
- */
1376
- function behaviorRun(inputs, params, ctx) {
1377
- if (!ctx.source) throw new Error(`behaviour node ${ctx.type} has no source module`);
1378
- return { behavior: {
1379
- kind: "behavior",
1380
- type: ctx.type,
1381
- node: ctx.nodeId,
1382
- params: {
1383
- ...inputs,
1384
- ...params
1385
- },
1386
- source: ctx.source
1387
- } };
1388
- }
1389
1338
  /** A starting point for a new script node. @category Graph */
1390
1339
  const SCRIPT_NODE_TEMPLATE = `// A node is a function with a typed interface.
1391
1340
  // inputs: pins wired from other nodes (an unconnected one uses its widget value / default)
@@ -1411,8 +1360,7 @@ export function run({ a, b }, { scale }, ctx) {
1411
1360
  return { result: (a + b) * scale };
1412
1361
  }
1413
1362
  `;
1414
- //#endregion
1415
- //#region src/graph/subgraph.ts
1363
+ // src/graph/subgraph.ts
1416
1364
  /**
1417
1365
  * Subgraphs: a selection of nodes collapsed into one node type. The inner graph is stored in
1418
1366
  * `doc.subgraphs[type]` with its interface — every wire that crossed the selection's edge
@@ -1688,8 +1636,6 @@ function expandSubgraph(doc, nodeId, defs = doc.subgraphs) {
1688
1636
  doc.nodes = doc.nodes.filter((n) => n.id !== nodeId);
1689
1637
  return [...ids.values()];
1690
1638
  }
1691
- //#endregion
1692
- exports.BEHAVIOR_HOOKS = BEHAVIOR_HOOKS;
1693
1639
  exports.BUILTIN_NODES = BUILTIN_NODES;
1694
1640
  exports.BUILTIN_SOURCES = BUILTIN_SOURCES;
1695
1641
  exports.GRAPH_FORMAT = GRAPH_FORMAT;
@@ -1700,7 +1646,6 @@ exports.OUTPUT_LISTS = OUTPUT_LISTS;
1700
1646
  exports.SCRIPT_NODE_TEMPLATE = SCRIPT_NODE_TEMPLATE;
1701
1647
  exports.TypeRegistry = TypeRegistry;
1702
1648
  exports.addNode = addNode;
1703
- exports.behaviorRun = behaviorRun;
1704
1649
  exports.canConnect = canConnect;
1705
1650
  exports.collapseToSubgraph = collapseToSubgraph;
1706
1651
  exports.compileNodeScript = compileNodeScript;