@bian-womp/spark-graph 0.1.19 → 0.1.20

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
@@ -2158,6 +2158,57 @@ function setupBasicGraphRegistry() {
2158
2158
  return { Values: out };
2159
2159
  },
2160
2160
  });
2161
+ // Timer
2162
+ registry.registerNode({
2163
+ id: "base.timer",
2164
+ categoryId: "compute",
2165
+ inputs: {
2166
+ Enabled: "base.bool",
2167
+ IntervalMs: "base.float",
2168
+ Immediate: "base.bool",
2169
+ },
2170
+ outputs: { Now: "base.float", Count: "base.float" },
2171
+ inputDefaults: { Enabled: true, IntervalMs: 1000, Immediate: true },
2172
+ impl: (ins, ctx) => {
2173
+ const enabled = Boolean(ins.Enabled);
2174
+ const intervalMs = Math.max(1, Math.trunc(Number(ins.IntervalMs ?? 1000)));
2175
+ const immediate = Boolean(ins.Immediate);
2176
+ const stop = () => {
2177
+ const id = ctx.state.timerId;
2178
+ if (id !== undefined) {
2179
+ clearInterval(id);
2180
+ ctx.setState({ timerId: undefined });
2181
+ }
2182
+ };
2183
+ if (!enabled) {
2184
+ stop();
2185
+ return;
2186
+ }
2187
+ // restart timer with new settings
2188
+ stop();
2189
+ let count = 0;
2190
+ if (immediate) {
2191
+ ctx.emit("Now", Date.now());
2192
+ ctx.emit("Count", count);
2193
+ count += 1;
2194
+ }
2195
+ const id = setInterval(() => {
2196
+ ctx.emit("Now", Date.now());
2197
+ ctx.emit("Count", count);
2198
+ count += 1;
2199
+ }, intervalMs);
2200
+ ctx.setState({ timerId: id });
2201
+ },
2202
+ lifecycle: {
2203
+ dispose: (ctx) => {
2204
+ const id = ctx.state.timerId;
2205
+ if (id !== undefined) {
2206
+ clearInterval(id);
2207
+ ctx.setState({ timerId: undefined });
2208
+ }
2209
+ },
2210
+ },
2211
+ });
2161
2212
  return registry;
2162
2213
  }
2163
2214
  function registerDelayNode(registry) {