@fbp/evaluator 2.1.0 → 2.1.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.
package/esm/evaluate.js CHANGED
@@ -30,8 +30,9 @@ export async function evaluate(graph, options) {
30
30
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
31
31
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
32
32
  * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
33
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
33
34
  */
34
- export function refEdges(nodes, scopePath = [], wiring) {
35
+ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
35
36
  const names = new Set(nodes.map(n => n.name));
36
37
  const scope = joinPath(scopePath);
37
38
  const edges = [];
@@ -85,14 +86,47 @@ export function refEdges(nodes, scopePath = [], wiring) {
85
86
  }
86
87
  if (holder.type !== SUBNET)
87
88
  return;
89
+ const inner = [...holderScope, holder.name];
90
+ const innerContext = holder.context || context;
91
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
92
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
93
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
94
+ const at = JSON.stringify(inner);
95
+ let read = reads.get(at);
96
+ if (!read) {
97
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
98
+ reads.set(at, read);
99
+ }
88
100
  for (const child of holder.nodes ?? []) {
89
- collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
101
+ if (read.has(child.name))
102
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
90
103
  }
91
104
  };
92
105
  for (const node of nodes)
93
106
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
94
107
  return edges;
95
108
  }
109
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
110
+ function pulled(nodes, edges) {
111
+ const upstream = new Map();
112
+ for (const edge of edges) {
113
+ const sources = upstream.get(edge.dst.node);
114
+ if (sources)
115
+ sources.push(edge.src.node);
116
+ else
117
+ upstream.set(edge.dst.node, [edge.src.node]);
118
+ }
119
+ const read = new Set();
120
+ const pending = nodes.filter(n => n.type === GRAPH_OUTPUT).map(n => n.name);
121
+ while (pending.length) {
122
+ const name = pending.pop();
123
+ if (read.has(name))
124
+ continue;
125
+ read.add(name);
126
+ pending.push(...(upstream.get(name) ?? []));
127
+ }
128
+ return read;
129
+ }
96
130
  /** `Wiring` for a scope, answered from its definitions. */
97
131
  export function wiringOf(edges, context, definitions) {
98
132
  return {
package/evaluate.d.ts CHANGED
@@ -19,8 +19,9 @@ export declare function evaluate(graph: Graph, options: EvaluateOptions): Promis
19
19
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
20
20
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
21
21
  * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
22
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
22
23
  */
23
- export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring): Edge[];
24
+ export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring, reads?: Map<string, Set<string>>): Edge[];
24
25
  /** What `refEdges` needs to tell a typed value that a wire overrides from one that is read. */
25
26
  export interface Wiring {
26
27
  /** The edges and context of the scope the nodes live in. */
package/evaluate.js CHANGED
@@ -35,8 +35,9 @@ async function evaluate(graph, options) {
35
35
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
36
36
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
37
37
  * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
38
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
38
39
  */
39
- function refEdges(nodes, scopePath = [], wiring) {
40
+ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
40
41
  const names = new Set(nodes.map(n => n.name));
41
42
  const scope = (0, spec_1.joinPath)(scopePath);
42
43
  const edges = [];
@@ -90,14 +91,47 @@ function refEdges(nodes, scopePath = [], wiring) {
90
91
  }
91
92
  if (holder.type !== spec_1.SUBNET)
92
93
  return;
94
+ const inner = [...holderScope, holder.name];
95
+ const innerContext = holder.context || context;
96
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
97
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
98
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
99
+ const at = JSON.stringify(inner);
100
+ let read = reads.get(at);
101
+ if (!read) {
102
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
103
+ reads.set(at, read);
104
+ }
93
105
  for (const child of holder.nodes ?? []) {
94
- collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
106
+ if (read.has(child.name))
107
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
95
108
  }
96
109
  };
97
110
  for (const node of nodes)
98
111
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
99
112
  return edges;
100
113
  }
114
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
115
+ function pulled(nodes, edges) {
116
+ const upstream = new Map();
117
+ for (const edge of edges) {
118
+ const sources = upstream.get(edge.dst.node);
119
+ if (sources)
120
+ sources.push(edge.src.node);
121
+ else
122
+ upstream.set(edge.dst.node, [edge.src.node]);
123
+ }
124
+ const read = new Set();
125
+ const pending = nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT).map(n => n.name);
126
+ while (pending.length) {
127
+ const name = pending.pop();
128
+ if (read.has(name))
129
+ continue;
130
+ read.add(name);
131
+ pending.push(...(upstream.get(name) ?? []));
132
+ }
133
+ return read;
134
+ }
101
135
  /** `Wiring` for a scope, answered from its definitions. */
102
136
  function wiringOf(edges, context, definitions) {
103
137
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbp/evaluator",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "FBP graph evaluator — node definitions, graph evaluation and validation",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "license": "MIT",
@@ -12,7 +12,7 @@
12
12
  "directory": "dist"
13
13
  },
14
14
  "dependencies": {
15
- "@fbp/spec": "2.1.0"
15
+ "@fbp/spec": "2.1.1"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/jest": "^30.0.0",