@bian-womp/spark-graph 0.2.2 → 0.2.4

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
@@ -1767,6 +1767,30 @@ const ComputeCategory = {
1767
1767
  ctx.emit(h, v);
1768
1768
  }
1769
1769
  },
1770
+ onExternalEvent(event, ctx) {
1771
+ try {
1772
+ const e = event;
1773
+ // Preferred: call a function on ctx.state keyed by e.action
1774
+ const action = e?.action;
1775
+ if (action && typeof ctx.state?.[action] === "function") {
1776
+ const fn = ctx.state[action];
1777
+ // Normalize args: prefer explicit args array, else wrap single value;
1778
+ let args = [];
1779
+ if (Array.isArray(e?.args))
1780
+ args = [...e.args];
1781
+ else if (e?.args !== undefined)
1782
+ args = [e.args];
1783
+ else if (e?.params !== undefined)
1784
+ args = [e.params];
1785
+ void fn(...args);
1786
+ return;
1787
+ }
1788
+ console.warn("Unsupported external event", event);
1789
+ }
1790
+ catch {
1791
+ console.warn("Error handling external event", event);
1792
+ }
1793
+ },
1770
1794
  }),
1771
1795
  policy: { mode: "push", asyncConcurrency: "switch" },
1772
1796
  };
@@ -2118,39 +2142,29 @@ function setupBasicGraphRegistry() {
2118
2142
  });
2119
2143
  // Number
2120
2144
  registry.registerNode({
2121
- id: "base.number",
2145
+ id: "base.input.number",
2122
2146
  categoryId: "compute",
2123
2147
  inputs: { Value: "base.float" },
2124
2148
  outputs: { Result: "base.float" },
2125
2149
  impl: (ins) => ({ Result: Number(ins.Value) }),
2126
2150
  });
2127
2151
  registry.registerNode({
2128
- id: "base.string",
2152
+ id: "base.input.string",
2129
2153
  categoryId: "compute",
2130
2154
  inputs: { Value: "base.string" },
2131
2155
  outputs: { Result: "base.string" },
2132
2156
  impl: (ins) => ({ Result: String(ins.Value) }),
2133
2157
  });
2134
2158
  registry.registerNode({
2135
- id: "base.bool",
2159
+ id: "base.input.bool",
2136
2160
  categoryId: "compute",
2137
2161
  inputs: { Value: "base.bool" },
2138
2162
  outputs: { Result: "base.bool" },
2139
2163
  impl: (ins) => ({ Result: Boolean(ins.Value) }),
2140
2164
  });
2141
- // Integer
2142
- registry.registerNode({
2143
- id: "base.int",
2144
- categoryId: "compute",
2145
- inputs: { Value: "base.float" },
2146
- outputs: { Result: "base.float" },
2147
- impl: (ins) => ({
2148
- Result: Math.trunc(Number(ins.Value)),
2149
- }),
2150
- });
2151
2165
  // JSON parser node: base.stringToObject
2152
2166
  registry.registerNode({
2153
- id: "base.stringToObject",
2167
+ id: "base.string.toObject",
2154
2168
  categoryId: "compute",
2155
2169
  inputs: { Text: "base.string" },
2156
2170
  outputs: { Object: "base.object" },
@@ -2166,7 +2180,7 @@ function setupBasicGraphRegistry() {
2166
2180
  },
2167
2181
  });
2168
2182
  registry.registerNode({
2169
- id: "base.objectToString",
2183
+ id: "base.object.toString",
2170
2184
  categoryId: "compute",
2171
2185
  inputs: { Object: "base.object" },
2172
2186
  outputs: { Text: "base.string" },
@@ -2256,7 +2270,7 @@ function setupBasicGraphRegistry() {
2256
2270
  impl: (ins) => {
2257
2271
  const a = asArray(ins.A ?? []);
2258
2272
  const b = asArray(ins.B ?? []);
2259
- const op = Number(ins.Operation ?? 0) | 0;
2273
+ const op = Number(ins.Operation ?? BaseMathOperation.Add);
2260
2274
  const unaryByOp = {
2261
2275
  [BaseMathOperation.Round]: (x) => Math.round(x),
2262
2276
  [BaseMathOperation.Floor]: (x) => Math.floor(x),
@@ -2317,7 +2331,7 @@ function setupBasicGraphRegistry() {
2317
2331
  outputs: { Result: "base.bool[]" },
2318
2332
  impl: (ins) => {
2319
2333
  const [a, b] = broadcast(ins.A, ins.B);
2320
- const op = Number(ins.Operation ?? BaseCompareOperation.Equal) | 0;
2334
+ const op = Number(ins.Operation ?? BaseCompareOperation.Equal);
2321
2335
  const compareByOp = {
2322
2336
  [BaseCompareOperation.LessThan]: (x, y) => x < y,
2323
2337
  [BaseCompareOperation.LessThanOrEqual]: (x, y) => x <= y,
@@ -2342,7 +2356,7 @@ function setupBasicGraphRegistry() {
2342
2356
  outputs: { Result: "base.bool[]" },
2343
2357
  inputDefaults: { Operation: BaseLogicOperation.Not },
2344
2358
  impl: (ins) => {
2345
- const op = Number(ins.Operation ?? BaseLogicOperation.Not) | 0;
2359
+ const op = Number(ins.Operation ?? BaseLogicOperation.Not);
2346
2360
  if (op === BaseLogicOperation.Not) {
2347
2361
  const a = asBoolArray(ins.A ?? []);
2348
2362
  return { Result: a.map((x) => !x) };
@@ -2966,8 +2980,8 @@ function createAsyncGraphRegistry() {
2966
2980
  function createProgressGraphDef() {
2967
2981
  const def = {
2968
2982
  nodes: [
2969
- { nodeId: "steps", typeId: "base.number" },
2970
- { nodeId: "delay", typeId: "base.number" },
2983
+ { nodeId: "steps", typeId: "base.input.number" },
2984
+ { nodeId: "delay", typeId: "base.input.number" },
2971
2985
  { nodeId: "work", typeId: "async.progress" },
2972
2986
  ],
2973
2987
  edges: [
@@ -2999,10 +3013,10 @@ function createValidationGraphDef() {
2999
3013
  // - Multi inbound to same input
3000
3014
  const def = {
3001
3015
  nodes: [
3002
- { nodeId: "nA", typeId: "base.number" },
3003
- { nodeId: "nB", typeId: "base.number" },
3016
+ { nodeId: "nA", typeId: "base.input.number" },
3017
+ { nodeId: "nB", typeId: "base.input.number" },
3004
3018
  { nodeId: "nC", typeId: "base.math" },
3005
- { nodeId: "s1", typeId: "base.objectToString" },
3019
+ { nodeId: "s1", typeId: "base.object.toString" },
3006
3020
  { nodeId: "cmp", typeId: "base.compare" },
3007
3021
  // Global validation issue: unknown node type (no nodeId/edgeId in data)
3008
3022
  { nodeId: "bad", typeId: "unknownType" },