@cadenza.io/core 1.7.0 → 1.7.2

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/dist/index.mjs CHANGED
@@ -27,9 +27,6 @@ var SignalBroker = class _SignalBroker {
27
27
  if (signalName.includes(" ")) {
28
28
  throw new Error("Signal name must not contain spaces");
29
29
  }
30
- if (signalName.includes("/")) {
31
- throw new Error("Signal name must not contain slashes");
32
- }
33
30
  if (signalName.includes("\\")) {
34
31
  throw new Error("Signal name must not contain backslashes");
35
32
  }
@@ -49,7 +46,7 @@ var SignalBroker = class _SignalBroker {
49
46
  this.metaRunner = metaRunner;
50
47
  }
51
48
  init() {
52
- Cadenza.createMetaTask(
49
+ Cadenza.createDebounceMetaTask(
53
50
  "Execute and clear queued signals",
54
51
  () => {
55
52
  for (const [id, signals] of this.emitStacks.entries()) {
@@ -61,8 +58,10 @@ var SignalBroker = class _SignalBroker {
61
58
  }
62
59
  return true;
63
60
  },
64
- "Executes queued signals and clears the stack"
65
- ).doOn("meta.clear_signal_queue_requested");
61
+ "Executes queued signals and clears the stack",
62
+ 500,
63
+ { maxWait: 1e4 }
64
+ ).doOn("meta.process_signal_queue_requested");
66
65
  this.getSignalsTask = Cadenza.createMetaTask("Get signals", (ctx) => {
67
66
  return {
68
67
  __signals: Array.from(this.signalObservers.keys()),
@@ -101,7 +100,6 @@ var SignalBroker = class _SignalBroker {
101
100
  * @param context The payload.
102
101
  * @edge Fire-and-forget; guards against loops per execId (from context.__graphExecId).
103
102
  * @edge For distribution, SignalTask can prefix and proxy remote.
104
- * @throws Error on detected loop.
105
103
  */
106
104
  emit(signal, context = {}) {
107
105
  const execId = context.__routineExecId || "global";
@@ -113,6 +111,7 @@ var SignalBroker = class _SignalBroker {
113
111
  executed = this.execute(signal, context);
114
112
  } finally {
115
113
  if (executed) stack.delete(signal);
114
+ if (stack.size === 0) this.emitStacks.delete(execId);
116
115
  }
117
116
  }
118
117
  execute(signal, context) {
@@ -123,6 +122,12 @@ var SignalBroker = class _SignalBroker {
123
122
  const parent = parts.slice(0, i).join(".");
124
123
  executed = executed || this.executeListener(parent + ".*", context);
125
124
  }
125
+ if (this.debug) {
126
+ console.log(
127
+ `Emitted signal ${signal} with context ${JSON.stringify(context)}`,
128
+ executed ? "\u2705" : "\u274C"
129
+ );
130
+ }
126
131
  return executed;
127
132
  }
128
133
  executeListener(signal, context) {
@@ -130,11 +135,6 @@ var SignalBroker = class _SignalBroker {
130
135
  const runner = signal.startsWith("meta") ? this.metaRunner : this.runner;
131
136
  if (obs && obs.tasks.size && runner) {
132
137
  obs.fn(runner, Array.from(obs.tasks), context);
133
- if (this.debug) {
134
- console.log(
135
- `Emitted signal ${signal} with context ${JSON.stringify(context)}`
136
- );
137
- }
138
138
  return true;
139
139
  }
140
140
  return false;
@@ -149,6 +149,7 @@ var SignalBroker = class _SignalBroker {
149
149
  this.emit("meta.signal_broker.added", { __signalName: signal });
150
150
  }
151
151
  }
152
+ // TODO schedule signals
152
153
  /**
153
154
  * Lists all observed signals.
154
155
  * @returns Array of signals.
@@ -602,7 +603,7 @@ var SignalEmitter = class {
602
603
 
603
604
  // src/graph/execution/GraphNode.ts
604
605
  var GraphNode = class _GraphNode extends SignalEmitter {
605
- constructor(task, context, routineExecId, prevNodes = []) {
606
+ constructor(task, context, routineExecId, prevNodes = [], debug = false) {
606
607
  super(task.isMeta);
607
608
  this.divided = false;
608
609
  this.splitGroupId = "";
@@ -616,12 +617,17 @@ var GraphNode = class _GraphNode extends SignalEmitter {
616
617
  this.failed = false;
617
618
  this.errored = false;
618
619
  this.destroyed = false;
620
+ this.debug = false;
619
621
  this.task = task;
620
622
  this.context = context;
621
623
  this.previousNodes = prevNodes;
622
624
  this.id = uuid3();
623
625
  this.routineExecId = routineExecId;
624
626
  this.splitGroupId = routineExecId;
627
+ this.debug = debug;
628
+ }
629
+ setDebug(value) {
630
+ this.debug = value;
625
631
  }
626
632
  isUnique() {
627
633
  return this.task.isUnique;
@@ -693,6 +699,9 @@ var GraphNode = class _GraphNode extends SignalEmitter {
693
699
  if (this.previousNodes.length === 0) {
694
700
  this.emit("meta.node.started_routine_execution", memento);
695
701
  }
702
+ if (this.debug) {
703
+ this.log();
704
+ }
696
705
  this.emit("meta.node.started", memento);
697
706
  return this.executionStart;
698
707
  }
@@ -718,7 +727,6 @@ var GraphNode = class _GraphNode extends SignalEmitter {
718
727
  }
719
728
  execute() {
720
729
  if (!this.divided && !this.processing) {
721
- this.start();
722
730
  this.processing = true;
723
731
  const inputValidation = this.task.validateInput(
724
732
  this.context.getContext()
@@ -889,7 +897,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
889
897
  return this;
890
898
  }
891
899
  clone() {
892
- return new _GraphNode(this.task, this.context, this.routineExecId, [this]);
900
+ return new _GraphNode(
901
+ this.task,
902
+ this.context,
903
+ this.routineExecId,
904
+ [this],
905
+ this.debug
906
+ );
893
907
  }
894
908
  consume(node) {
895
909
  this.context = this.context.combine(node.context);
@@ -985,7 +999,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
985
999
  };
986
1000
  }
987
1001
  log() {
988
- console.log(this.task.name, this.context.getContext(), this.executionTime);
1002
+ console.log(this.task.name, this.context.getContext(), this.routineExecId);
989
1003
  }
990
1004
  };
991
1005
 
@@ -1856,13 +1870,10 @@ var GraphRunner = class extends SignalEmitter {
1856
1870
  __scheduled: Date.now()
1857
1871
  };
1858
1872
  this.emit("meta.runner.added_tasks", data);
1859
- if (this.debug) {
1860
- console.log(
1861
- `Running routine ${routineName} with context ${JSON.stringify(ctx.getContext())}`
1862
- );
1863
- }
1864
1873
  allTasks.forEach(
1865
- (task) => this.currentRun.addNode(new GraphNode(task, ctx, routineExecId))
1874
+ (task) => this.currentRun.addNode(
1875
+ new GraphNode(task, ctx, routineExecId, [], this.debug)
1876
+ )
1866
1877
  );
1867
1878
  }
1868
1879
  /**
@@ -1893,9 +1904,6 @@ var GraphRunner = class extends SignalEmitter {
1893
1904
  return this.reset();
1894
1905
  }
1895
1906
  reset() {
1896
- if (this.debug) {
1897
- this.currentRun.log();
1898
- }
1899
1907
  this.isRunning = false;
1900
1908
  const lastRun = this.currentRun;
1901
1909
  if (!this.debug) {
@@ -1932,7 +1940,7 @@ var GraphRunner = class extends SignalEmitter {
1932
1940
 
1933
1941
  // src/graph/definition/DebounceTask.ts
1934
1942
  var DebounceTask = class extends Task {
1935
- constructor(name, task, description = "", debounceTime = 1e3, leading = false, trailing = true, concurrency = 0, timeout = 0, register = true, isUnique = false, isMeta = false, inputSchema = void 0, validateInputSchema = false, outputSchema = void 0, validateOutputSchema = false) {
1943
+ constructor(name, task, description = "", debounceTime = 1e3, leading = false, trailing = true, maxWait = 0, concurrency = 0, timeout = 0, register = true, isUnique = false, isMeta = false, inputSchema = void 0, validateInputSchema = false, outputSchema = void 0, validateOutputSchema = false) {
1936
1944
  super(
1937
1945
  name,
1938
1946
  task,
@@ -1949,15 +1957,19 @@ var DebounceTask = class extends Task {
1949
1957
  validateOutputSchema
1950
1958
  );
1951
1959
  this.timer = null;
1960
+ this.maxTimer = null;
1961
+ this.hasLaterCall = false;
1952
1962
  this.lastResolve = null;
1953
1963
  this.lastReject = null;
1954
1964
  this.lastContext = null;
1955
1965
  this.lastTimeout = null;
1966
+ this.lastProgressCallback = null;
1956
1967
  this.debounceTime = debounceTime;
1957
1968
  this.leading = leading;
1958
1969
  this.trailing = trailing;
1970
+ this.maxWait = maxWait;
1959
1971
  }
1960
- executeFunction(progressCallback) {
1972
+ executeFunction() {
1961
1973
  if (this.lastTimeout) {
1962
1974
  clearTimeout(this.lastTimeout);
1963
1975
  }
@@ -1965,7 +1977,7 @@ var DebounceTask = class extends Task {
1965
1977
  try {
1966
1978
  result = this.taskFunction(
1967
1979
  this.lastContext.getClonedContext(),
1968
- progressCallback
1980
+ this.lastProgressCallback
1969
1981
  );
1970
1982
  } catch (error) {
1971
1983
  if (this.lastResolve) {
@@ -1983,21 +1995,46 @@ var DebounceTask = class extends Task {
1983
1995
  }
1984
1996
  debouncedTrigger(resolve, reject, context, timeout, progressCallback) {
1985
1997
  const callNow = this.leading && this.timer === null;
1998
+ const isNewBurst = this.timer === null;
1986
1999
  if (this.timer !== null) {
1987
2000
  clearTimeout(this.timer);
2001
+ this.timer = null;
1988
2002
  }
1989
2003
  this.lastResolve = resolve;
1990
2004
  this.lastReject = reject;
1991
2005
  this.lastContext = context;
1992
2006
  this.lastTimeout = timeout;
2007
+ this.lastProgressCallback = progressCallback;
2008
+ if (!callNow) {
2009
+ this.hasLaterCall = true;
2010
+ }
1993
2011
  this.timer = setTimeout(() => {
1994
2012
  this.timer = null;
1995
- if (this.trailing) {
1996
- this.executeFunction(progressCallback);
2013
+ if (this.trailing && this.hasLaterCall) {
2014
+ this.executeFunction();
2015
+ this.hasLaterCall = false;
2016
+ }
2017
+ if (this.maxTimer) {
2018
+ clearTimeout(this.maxTimer);
2019
+ this.maxTimer = null;
1997
2020
  }
1998
2021
  }, this.debounceTime);
1999
2022
  if (callNow) {
2000
- this.executeFunction(progressCallback);
2023
+ this.executeFunction();
2024
+ this.hasLaterCall = false;
2025
+ }
2026
+ if (this.maxWait > 0 && isNewBurst) {
2027
+ this.maxTimer = setTimeout(() => {
2028
+ this.maxTimer = null;
2029
+ if (this.trailing && this.hasLaterCall) {
2030
+ if (this.timer) {
2031
+ clearTimeout(this.timer);
2032
+ this.timer = null;
2033
+ }
2034
+ this.executeFunction();
2035
+ this.hasLaterCall = false;
2036
+ }
2037
+ }, this.maxWait);
2001
2038
  }
2002
2039
  }
2003
2040
  execute(context, progressCallback) {
@@ -2131,8 +2168,15 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
2131
2168
  this.nodes = [];
2132
2169
  this.executionTime = 0;
2133
2170
  this.executionStart = 0;
2171
+ this.debug = false;
2134
2172
  this.index = index;
2135
2173
  }
2174
+ setDebug(value) {
2175
+ this.debug = value;
2176
+ for (const node of this.nodes) {
2177
+ node.setDebug(value);
2178
+ }
2179
+ }
2136
2180
  get hasPreceding() {
2137
2181
  return !!this.previous && this.previous instanceof _GraphLayer;
2138
2182
  }
@@ -2262,6 +2306,10 @@ var GraphBuilder = class {
2262
2306
  constructor() {
2263
2307
  this.topLayerIndex = 0;
2264
2308
  this.layers = [];
2309
+ this.debug = false;
2310
+ }
2311
+ setDebug(value) {
2312
+ this.debug = value;
2265
2313
  }
2266
2314
  getResult() {
2267
2315
  return this.graph;
@@ -2307,7 +2355,9 @@ var GraphBuilder = class {
2307
2355
  }
2308
2356
  }
2309
2357
  createLayer(index) {
2310
- return new SyncGraphLayer(index);
2358
+ const layer = new SyncGraphLayer(index);
2359
+ layer.setDebug(this.debug);
2360
+ return layer;
2311
2361
  }
2312
2362
  getLayer(layerIndex) {
2313
2363
  return this.layers[layerIndex - this.topLayerIndex];
@@ -2519,7 +2569,9 @@ var GraphAsyncQueueBuilder = class extends GraphBuilder {
2519
2569
  }
2520
2570
  }
2521
2571
  createLayer(index) {
2522
- return new AsyncGraphLayer(index);
2572
+ const layer = new AsyncGraphLayer(index);
2573
+ layer.setDebug(this.debug);
2574
+ return layer;
2523
2575
  }
2524
2576
  };
2525
2577
 
@@ -2563,6 +2615,10 @@ var Cadenza = class {
2563
2615
  this.runner = new GraphRunner();
2564
2616
  this.metaRunner = new GraphRunner(true);
2565
2617
  this.broker.bootstrap(this.runner, this.metaRunner);
2618
+ if (this.mode === "debug" || this.mode === "dev") {
2619
+ this.broker.setDebug(true);
2620
+ this.runner.setDebug(true);
2621
+ }
2566
2622
  this.registry = GraphRegistry.instance;
2567
2623
  this.broker.init();
2568
2624
  this.runner.init();
@@ -2576,8 +2632,10 @@ var Cadenza = class {
2576
2632
  }
2577
2633
  static setMode(mode) {
2578
2634
  this.mode = mode;
2635
+ this.bootstrap();
2579
2636
  if (mode === "debug" || mode === "dev") {
2580
2637
  this.broker.setDebug(true);
2638
+ this.runner.setDebug(true);
2581
2639
  }
2582
2640
  }
2583
2641
  /**
@@ -2771,6 +2829,7 @@ var Cadenza = class {
2771
2829
  register: true,
2772
2830
  leading: false,
2773
2831
  trailing: true,
2832
+ maxWait: 0,
2774
2833
  isUnique: false,
2775
2834
  isMeta: false,
2776
2835
  inputSchema: void 0,
@@ -2787,6 +2846,7 @@ var Cadenza = class {
2787
2846
  debounceTime,
2788
2847
  options.leading,
2789
2848
  options.trailing,
2849
+ options.maxWait,
2790
2850
  options.concurrency,
2791
2851
  options.timeout,
2792
2852
  options.register,
@@ -2813,6 +2873,7 @@ var Cadenza = class {
2813
2873
  register: true,
2814
2874
  leading: false,
2815
2875
  trailing: true,
2876
+ maxWait: 0,
2816
2877
  isUnique: false,
2817
2878
  isMeta: false,
2818
2879
  inputSchema: void 0,