@n3rdw1z4rd/core 0.21.0 → 0.22.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.
@@ -3,7 +3,7 @@ import { NodeDef, NodeRegistry, NodeSpec, Param, ParamSpec, Port, PortSpec, RunC
3
3
  import { ConnectCheck, GRAPH_FORMAT, GRAPH_VERSION, GraphDoc, GraphGroup, GraphLink, GraphNode, PortRef, SubgraphDef, addNode, canConnect, connect, createGraph, disconnect, getNode, graphId, linkInto, linksInto, normalizeGraph, removeNode } from "./doc.js";
4
4
  import { EvaluateOptions, EvaluateResult, FrameInfo, GraphEvaluator, NodeResult, OUTPUT_LISTS } from "./evaluate.js";
5
5
  import { BUILTIN_NODES, createNodeRegistry } from "./nodes.js";
6
- import { BehaviorValue, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript } from "./script.js";
6
+ import { BEHAVIOR_HOOKS, BehaviorValue, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript } from "./script.js";
7
7
  import { BUILTIN_SOURCES } from "./sources.js";
8
8
  import { collapseToSubgraph, expandSubgraph, freeSubgraphType, registerSubgraphs, subgraphSpec } from "./subgraph.js";
9
- export { BUILTIN_NODES, BUILTIN_SOURCES, BehaviorValue, ConnectCheck, EvaluateOptions, EvaluateResult, FrameInfo, GRAPH_FORMAT, GRAPH_VERSION, GraphDoc, GraphEvaluator, GraphGroup, GraphLink, GraphNode, NodeDef, NodeRegistry, NodeResult, NodeSpec, OUTPUT_LISTS, Param, ParamSpec, ParsedType, Port, PortRef, PortSpec, PortTypeDef, RunContext, RunFn, SCRIPT_NODE_TEMPLATE, SubgraphDef, TypeRegistry, WidgetSpec, addNode, behaviorRun, canConnect, collapseToSubgraph, compileNodeScript, connect, createGraph, createNodeRegistry, defineNode, disconnect, expandSubgraph, freeSubgraphType, getNode, graphId, linkInto, linksInto, normalizeGraph, parseType, portTypes, registerSubgraphs, removeNode, subgraphSpec };
9
+ export { BEHAVIOR_HOOKS, BUILTIN_NODES, BUILTIN_SOURCES, BehaviorValue, ConnectCheck, EvaluateOptions, EvaluateResult, FrameInfo, GRAPH_FORMAT, GRAPH_VERSION, GraphDoc, GraphEvaluator, GraphGroup, GraphLink, GraphNode, NodeDef, NodeRegistry, NodeResult, NodeSpec, OUTPUT_LISTS, Param, ParamSpec, ParsedType, Port, PortRef, PortSpec, PortTypeDef, RunContext, RunFn, SCRIPT_NODE_TEMPLATE, SubgraphDef, TypeRegistry, WidgetSpec, addNode, behaviorRun, canConnect, collapseToSubgraph, compileNodeScript, connect, createGraph, createNodeRegistry, defineNode, disconnect, expandSubgraph, freeSubgraphType, getNode, graphId, linkInto, linksInto, normalizeGraph, parseType, portTypes, registerSubgraphs, removeNode, subgraphSpec };
@@ -4,6 +4,6 @@ import { GraphEvaluator, OUTPUT_LISTS } from "./evaluate.js";
4
4
  import { NodeRegistry, defineNode } from "./node.js";
5
5
  import { BUILTIN_SOURCES } from "./sources.js";
6
6
  import { BUILTIN_NODES, createNodeRegistry } from "./nodes.js";
7
- import { SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript } from "./script.js";
7
+ import { BEHAVIOR_HOOKS, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript } from "./script.js";
8
8
  import { collapseToSubgraph, expandSubgraph, freeSubgraphType, registerSubgraphs, subgraphSpec } from "./subgraph.js";
9
- export { BUILTIN_NODES, BUILTIN_SOURCES, GRAPH_FORMAT, GRAPH_VERSION, GraphEvaluator, NodeRegistry, OUTPUT_LISTS, SCRIPT_NODE_TEMPLATE, TypeRegistry, addNode, behaviorRun, canConnect, collapseToSubgraph, compileNodeScript, connect, createGraph, createNodeRegistry, defineNode, disconnect, expandSubgraph, freeSubgraphType, getNode, graphId, linkInto, linksInto, normalizeGraph, parseType, portTypes, registerSubgraphs, removeNode, subgraphSpec };
9
+ export { BEHAVIOR_HOOKS, BUILTIN_NODES, BUILTIN_SOURCES, GRAPH_FORMAT, GRAPH_VERSION, GraphEvaluator, NodeRegistry, OUTPUT_LISTS, SCRIPT_NODE_TEMPLATE, TypeRegistry, addNode, behaviorRun, canConnect, collapseToSubgraph, compileNodeScript, connect, createGraph, createNodeRegistry, defineNode, disconnect, expandSubgraph, freeSubgraphType, getNode, graphId, linkInto, linksInto, normalizeGraph, parseType, portTypes, registerSubgraphs, removeNode, subgraphSpec };
@@ -19,6 +19,14 @@ import { NodeDef, RunContext } from "./node.js";
19
19
  *
20
20
  * @module
21
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
+ export declare const BEHAVIOR_HOOKS: readonly ["setup", "step", "enter", "exit"];
22
30
  /**
23
31
  * Compiles a script node's source into a node type named `type`.
24
32
  *
@@ -26,8 +34,8 @@ import { NodeDef, RunContext } from "./node.js";
26
34
  */
27
35
  export declare function compileNodeScript(type: string, source: string): Promise<NodeDef>;
28
36
  /**
29
- * A behaviour: what a behaviour node evaluates to. The host runs `source`'s `step` (and
30
- * `setup`) against whatever it's attached to, with `params`.
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`.
31
39
  *
32
40
  * @category Graph
33
41
  */
@@ -39,12 +47,12 @@ export interface BehaviorValue {
39
47
  node: string;
40
48
  /** its inputs and params at evaluation time */
41
49
  params: Record<string, unknown>;
42
- /** the module with step / setup */
50
+ /** the module with its hooks (setup / step / enter / exit) */
43
51
  source: string;
44
52
  }
45
53
  /**
46
54
  * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
47
- * on the `behavior` output. Behaviour modules (exporting `step`, no `run`) get it automatically.
55
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
48
56
  *
49
57
  * @category Graph
50
58
  */
@@ -20,6 +20,19 @@ import { defineNode } from "./node.js";
20
20
  * @module
21
21
  */
22
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
+ ];
35
+ /**
23
36
  * Compiles a script node's source into a node type named `type`.
24
37
  *
25
38
  * @category Graph
@@ -30,12 +43,12 @@ async function compileNodeScript(type, source) {
30
43
  `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
31
44
  );
32
45
  const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
33
- if (typeof m.run !== "function" && (typeof m.step === "function" || typeof m.setup === "function")) {
46
+ if (typeof m.run !== "function" && BEHAVIOR_HOOKS.some((h) => typeof m[h] === "function")) {
34
47
  m.run = behaviorRun;
35
48
  m.outputs ??= { behavior: "behavior" };
36
49
  m.category ??= "behavior";
37
50
  }
38
- if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or step(self, world, dt, params) for a behaviour)`);
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)`);
39
52
  return defineNode({
40
53
  type,
41
54
  title: m.title,
@@ -52,7 +65,7 @@ async function compileNodeScript(type, source) {
52
65
  }
53
66
  /**
54
67
  * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
55
- * on the `behavior` output. Behaviour modules (exporting `step`, no `run`) get it automatically.
68
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
56
69
  *
57
70
  * @category Graph
58
71
  */
@@ -95,4 +108,4 @@ export function run({ a, b }, { scale }, ctx) {
95
108
  }
96
109
  `;
97
110
  //#endregion
98
- export { SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript };
111
+ export { BEHAVIOR_HOOKS, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript };
package/dist/graph.cjs CHANGED
@@ -1324,6 +1324,19 @@ function createNodeRegistry() {
1324
1324
  * @module
1325
1325
  */
1326
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
+ ];
1339
+ /**
1327
1340
  * Compiles a script node's source into a node type named `type`.
1328
1341
  *
1329
1342
  * @category Graph
@@ -1334,12 +1347,12 @@ async function compileNodeScript(type, source) {
1334
1347
  `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
1335
1348
  );
1336
1349
  const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
1337
- if (typeof m.run !== "function" && (typeof m.step === "function" || typeof m.setup === "function")) {
1350
+ if (typeof m.run !== "function" && BEHAVIOR_HOOKS.some((h) => typeof m[h] === "function")) {
1338
1351
  m.run = behaviorRun;
1339
1352
  m.outputs ??= { behavior: "behavior" };
1340
1353
  m.category ??= "behavior";
1341
1354
  }
1342
- if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or step(self, world, dt, params) for a behaviour)`);
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)`);
1343
1356
  return defineNode({
1344
1357
  type,
1345
1358
  title: m.title,
@@ -1356,7 +1369,7 @@ async function compileNodeScript(type, source) {
1356
1369
  }
1357
1370
  /**
1358
1371
  * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
1359
- * on the `behavior` output. Behaviour modules (exporting `step`, no `run`) get it automatically.
1372
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
1360
1373
  *
1361
1374
  * @category Graph
1362
1375
  */
@@ -1676,6 +1689,7 @@ function expandSubgraph(doc, nodeId, defs = doc.subgraphs) {
1676
1689
  return [...ids.values()];
1677
1690
  }
1678
1691
  //#endregion
1692
+ exports.BEHAVIOR_HOOKS = BEHAVIOR_HOOKS;
1679
1693
  exports.BUILTIN_NODES = BUILTIN_NODES;
1680
1694
  exports.BUILTIN_SOURCES = BUILTIN_SOURCES;
1681
1695
  exports.GRAPH_FORMAT = GRAPH_FORMAT;
package/dist/graph.d.cts CHANGED
@@ -486,6 +486,14 @@ export declare function createNodeRegistry(): NodeRegistry;
486
486
  *
487
487
  * @module
488
488
  */
489
+ /**
490
+ * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
491
+ * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
492
+ * starts / stops touching (or overlapping) the one the behaviour is on.
493
+ *
494
+ * @category Graph
495
+ */
496
+ export declare const BEHAVIOR_HOOKS: readonly ["setup", "step", "enter", "exit"];
489
497
  /**
490
498
  * Compiles a script node's source into a node type named `type`.
491
499
  *
@@ -493,8 +501,8 @@ export declare function createNodeRegistry(): NodeRegistry;
493
501
  */
494
502
  export declare function compileNodeScript(type: string, source: string): Promise<NodeDef>;
495
503
  /**
496
- * A behaviour: what a behaviour node evaluates to. The host runs `source`'s `step` (and
497
- * `setup`) against whatever it's attached to, with `params`.
504
+ * A behaviour: what a behaviour node evaluates to. The host runs `source`'s hooks
505
+ * ({@link BEHAVIOR_HOOKS}) against whatever it's attached to, with `params`.
498
506
  *
499
507
  * @category Graph
500
508
  */
@@ -506,12 +514,12 @@ export interface BehaviorValue {
506
514
  node: string;
507
515
  /** its inputs and params at evaluation time */
508
516
  params: Record<string, unknown>;
509
- /** the module with step / setup */
517
+ /** the module with its hooks (setup / step / enter / exit) */
510
518
  source: string;
511
519
  }
512
520
  /**
513
521
  * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
514
- * on the `behavior` output. Behaviour modules (exporting `step`, no `run`) get it automatically.
522
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
515
523
  *
516
524
  * @category Graph
517
525
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n3rdw1z4rd/core",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "core library",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",