@bian-womp/spark-graph 0.1.19 → 0.1.21

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/lib/cjs/index.cjs CHANGED
@@ -1,5 +1,34 @@
1
1
  'use strict';
2
2
 
3
+ function typed(typeId, value) {
4
+ return { __spark_type: typeId, __spark_value: value };
5
+ }
6
+ function isTypedOutput(v) {
7
+ return (!!v &&
8
+ typeof v === "object" &&
9
+ Object.prototype.hasOwnProperty.call(v, "__spark_type"));
10
+ }
11
+ function getTypedOutputTypeId(v) {
12
+ if (isTypedOutput(v))
13
+ return String(v.__spark_type);
14
+ return undefined;
15
+ }
16
+ function getTypedOutputValue(v) {
17
+ if (isTypedOutput(v))
18
+ return v.__spark_value;
19
+ return v;
20
+ }
21
+ function getInputTypeId(inputs, handle) {
22
+ const v = inputs ? inputs[handle] : undefined;
23
+ if (!v)
24
+ return undefined;
25
+ return typeof v === "string" ? v : v.typeId;
26
+ }
27
+ function isInputPrivate(inputs, handle) {
28
+ const v = inputs ? inputs[handle] : undefined;
29
+ return !!(v && typeof v === "object" && v.private);
30
+ }
31
+
3
32
  class CategoryRegistry {
4
33
  constructor() {
5
34
  this.categories = new Map();
@@ -372,25 +401,6 @@ class Registry {
372
401
  }
373
402
  }
374
403
 
375
- function typed(typeId, value) {
376
- return { __spark_type: typeId, __spark_value: value };
377
- }
378
- function isTypedOutput(v) {
379
- return (!!v &&
380
- typeof v === "object" &&
381
- Object.prototype.hasOwnProperty.call(v, "__spark_type"));
382
- }
383
- function getInputTypeId(inputs, handle) {
384
- const v = inputs ? inputs[handle] : undefined;
385
- if (!v)
386
- return undefined;
387
- return typeof v === "string" ? v : v.typeId;
388
- }
389
- function isInputPrivate(inputs, handle) {
390
- const v = inputs ? inputs[handle] : undefined;
391
- return !!(v && typeof v === "object" && v.private);
392
- }
393
-
394
404
  class GraphRuntime {
395
405
  constructor() {
396
406
  this.nodes = new Map();
@@ -565,12 +575,12 @@ class GraphRuntime {
565
575
  });
566
576
  if (anyAsync) {
567
577
  convertAsync = async (v, signal) => {
568
- if (!isTypedOutput(v))
578
+ const typeId = getTypedOutputTypeId(v);
579
+ if (!typeId)
569
580
  throw new Error(`Typed output required for union source; allowed: ${srcTypes.join("|")}`);
570
- const typeId = String(v.__spark_type);
571
581
  if (!srcTypes.includes(typeId))
572
582
  throw new Error(`Invalid typed output ${typeId}; allowed: ${srcTypes.join("|")}`);
573
- const payload = v.__spark_value;
583
+ const payload = typeId;
574
584
  const res = registry.resolveCoercion(typeId, dstDeclared);
575
585
  if (!res)
576
586
  return payload;
@@ -581,12 +591,12 @@ class GraphRuntime {
581
591
  }
582
592
  else {
583
593
  convert = (v) => {
584
- if (!isTypedOutput(v))
594
+ const typeId = getTypedOutputTypeId(v);
595
+ if (!typeId)
585
596
  throw new Error(`Typed output required for union source; allowed: ${srcTypes.join("|")}`);
586
- const typeId = String(v.__spark_type);
587
597
  if (!srcTypes.includes(typeId))
588
598
  throw new Error(`Invalid typed output ${typeId}; allowed: ${srcTypes.join("|")}`);
589
- const payload = v.__spark_value;
599
+ const payload = typeId;
590
600
  const res = registry.resolveCoercion(typeId, dstDeclared);
591
601
  if (!res)
592
602
  return payload;
@@ -777,9 +787,7 @@ class GraphRuntime {
777
787
  handle: srcHandle,
778
788
  value,
779
789
  io: "output",
780
- runtimeTypeId: isTypedOutput(value)
781
- ? String(value.__spark_type)
782
- : undefined,
790
+ runtimeTypeId: getTypedOutputTypeId(value),
783
791
  });
784
792
  // fan-out along all edges from this output
785
793
  const outEdges = this.edges.filter((e) => e.source.nodeId === srcNodeId && e.source.handle === srcHandle);
@@ -811,7 +819,7 @@ class GraphRuntime {
811
819
  handle: e.target.handle,
812
820
  value: v,
813
821
  io: "input",
814
- runtimeTypeId: isTypedOutput(v) ? String(v.__spark_type) : undefined,
822
+ runtimeTypeId: getTypedOutputTypeId(v),
815
823
  });
816
824
  if (!this.paused && this.allInboundHaveValue(e.target.nodeId))
817
825
  this.scheduleInputsChanged(e.target.nodeId);
@@ -2158,6 +2166,57 @@ function setupBasicGraphRegistry() {
2158
2166
  return { Values: out };
2159
2167
  },
2160
2168
  });
2169
+ // Timer
2170
+ registry.registerNode({
2171
+ id: "base.timer",
2172
+ categoryId: "compute",
2173
+ inputs: {
2174
+ Enabled: "base.bool",
2175
+ IntervalMs: "base.float",
2176
+ Immediate: "base.bool",
2177
+ },
2178
+ outputs: { Now: "base.float", Count: "base.float" },
2179
+ inputDefaults: { Enabled: true, IntervalMs: 1000, Immediate: true },
2180
+ impl: (ins, ctx) => {
2181
+ const enabled = Boolean(ins.Enabled);
2182
+ const intervalMs = Math.max(1, Math.trunc(Number(ins.IntervalMs ?? 1000)));
2183
+ const immediate = Boolean(ins.Immediate);
2184
+ const stop = () => {
2185
+ const id = ctx.state.timerId;
2186
+ if (id !== undefined) {
2187
+ clearInterval(id);
2188
+ ctx.setState({ timerId: undefined });
2189
+ }
2190
+ };
2191
+ if (!enabled) {
2192
+ stop();
2193
+ return;
2194
+ }
2195
+ // restart timer with new settings
2196
+ stop();
2197
+ let count = 0;
2198
+ if (immediate) {
2199
+ ctx.emit("Now", Date.now());
2200
+ ctx.emit("Count", count);
2201
+ count += 1;
2202
+ }
2203
+ const id = setInterval(() => {
2204
+ ctx.emit("Now", Date.now());
2205
+ ctx.emit("Count", count);
2206
+ count += 1;
2207
+ }, intervalMs);
2208
+ ctx.setState({ timerId: id });
2209
+ },
2210
+ lifecycle: {
2211
+ dispose: (ctx) => {
2212
+ const id = ctx.state.timerId;
2213
+ if (id !== undefined) {
2214
+ clearInterval(id);
2215
+ ctx.setState({ timerId: undefined });
2216
+ }
2217
+ },
2218
+ },
2219
+ });
2161
2220
  return registry;
2162
2221
  }
2163
2222
  function registerDelayNode(registry) {
@@ -2436,6 +2495,8 @@ exports.createSimpleGraphRegistry = createSimpleGraphRegistry;
2436
2495
  exports.createValidationGraphDef = createValidationGraphDef;
2437
2496
  exports.createValidationGraphRegistry = createValidationGraphRegistry;
2438
2497
  exports.getInputTypeId = getInputTypeId;
2498
+ exports.getTypedOutputTypeId = getTypedOutputTypeId;
2499
+ exports.getTypedOutputValue = getTypedOutputValue;
2439
2500
  exports.isInputPrivate = isInputPrivate;
2440
2501
  exports.isTypedOutput = isTypedOutput;
2441
2502
  exports.registerDelayNode = registerDelayNode;