@cadenza.io/core 1.7.0 → 1.7.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/dist/index.d.mts CHANGED
@@ -155,7 +155,9 @@ declare abstract class GraphLayer extends ExecutionChain implements Graph {
155
155
  protected nodes: GraphNode[];
156
156
  private executionTime;
157
157
  private executionStart;
158
+ protected debug: boolean;
158
159
  constructor(index: number);
160
+ setDebug(value: boolean): void;
159
161
  abstract execute(context?: GraphContext): unknown;
160
162
  get hasPreceding(): boolean;
161
163
  getNumberOfNodes(): number;
@@ -199,7 +201,9 @@ declare class GraphNode extends SignalEmitter implements Graph {
199
201
  private failed;
200
202
  private errored;
201
203
  destroyed: boolean;
202
- constructor(task: Task, context: GraphContext, routineExecId: string, prevNodes?: GraphNode[]);
204
+ protected debug: boolean;
205
+ constructor(task: Task, context: GraphContext, routineExecId: string, prevNodes?: GraphNode[], debug?: boolean);
206
+ setDebug(value: boolean): void;
203
207
  isUnique(): boolean;
204
208
  isMeta(): boolean;
205
209
  isProcessed(): boolean;
@@ -491,6 +495,8 @@ declare abstract class GraphBuilder {
491
495
  graph: GraphLayer | undefined;
492
496
  topLayerIndex: number;
493
497
  layers: GraphLayer[];
498
+ debug: boolean;
499
+ setDebug(value: boolean): void;
494
500
  getResult(): GraphLayer;
495
501
  compose(): void;
496
502
  addNode(node: GraphNode): void;
@@ -644,7 +650,6 @@ declare class SignalBroker {
644
650
  * @param context The payload.
645
651
  * @edge Fire-and-forget; guards against loops per execId (from context.__graphExecId).
646
652
  * @edge For distribution, SignalTask can prefix and proxy remote.
647
- * @throws Error on detected loop.
648
653
  */
649
654
  emit(signal: string, context?: AnyObject): void;
650
655
  execute(signal: string, context: AnyObject): boolean;
@@ -687,17 +692,22 @@ declare class GraphRegistry {
687
692
  interface DebounceOptions {
688
693
  leading?: boolean;
689
694
  trailing?: boolean;
695
+ maxWait?: number;
690
696
  }
691
697
  declare class DebounceTask extends Task {
692
698
  private readonly debounceTime;
693
699
  private leading;
694
700
  private trailing;
701
+ private maxWait;
695
702
  private timer;
703
+ private maxTimer;
704
+ private hasLaterCall;
696
705
  private lastResolve;
697
706
  private lastReject;
698
707
  private lastContext;
699
708
  private lastTimeout;
700
- constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
709
+ private lastProgressCallback;
710
+ constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, maxWait?: number, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
701
711
  private executeFunction;
702
712
  private debouncedTrigger;
703
713
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
package/dist/index.d.ts CHANGED
@@ -155,7 +155,9 @@ declare abstract class GraphLayer extends ExecutionChain implements Graph {
155
155
  protected nodes: GraphNode[];
156
156
  private executionTime;
157
157
  private executionStart;
158
+ protected debug: boolean;
158
159
  constructor(index: number);
160
+ setDebug(value: boolean): void;
159
161
  abstract execute(context?: GraphContext): unknown;
160
162
  get hasPreceding(): boolean;
161
163
  getNumberOfNodes(): number;
@@ -199,7 +201,9 @@ declare class GraphNode extends SignalEmitter implements Graph {
199
201
  private failed;
200
202
  private errored;
201
203
  destroyed: boolean;
202
- constructor(task: Task, context: GraphContext, routineExecId: string, prevNodes?: GraphNode[]);
204
+ protected debug: boolean;
205
+ constructor(task: Task, context: GraphContext, routineExecId: string, prevNodes?: GraphNode[], debug?: boolean);
206
+ setDebug(value: boolean): void;
203
207
  isUnique(): boolean;
204
208
  isMeta(): boolean;
205
209
  isProcessed(): boolean;
@@ -491,6 +495,8 @@ declare abstract class GraphBuilder {
491
495
  graph: GraphLayer | undefined;
492
496
  topLayerIndex: number;
493
497
  layers: GraphLayer[];
498
+ debug: boolean;
499
+ setDebug(value: boolean): void;
494
500
  getResult(): GraphLayer;
495
501
  compose(): void;
496
502
  addNode(node: GraphNode): void;
@@ -644,7 +650,6 @@ declare class SignalBroker {
644
650
  * @param context The payload.
645
651
  * @edge Fire-and-forget; guards against loops per execId (from context.__graphExecId).
646
652
  * @edge For distribution, SignalTask can prefix and proxy remote.
647
- * @throws Error on detected loop.
648
653
  */
649
654
  emit(signal: string, context?: AnyObject): void;
650
655
  execute(signal: string, context: AnyObject): boolean;
@@ -687,17 +692,22 @@ declare class GraphRegistry {
687
692
  interface DebounceOptions {
688
693
  leading?: boolean;
689
694
  trailing?: boolean;
695
+ maxWait?: number;
690
696
  }
691
697
  declare class DebounceTask extends Task {
692
698
  private readonly debounceTime;
693
699
  private leading;
694
700
  private trailing;
701
+ private maxWait;
695
702
  private timer;
703
+ private maxTimer;
704
+ private hasLaterCall;
696
705
  private lastResolve;
697
706
  private lastReject;
698
707
  private lastContext;
699
708
  private lastTimeout;
700
- constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
709
+ private lastProgressCallback;
710
+ constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, maxWait?: number, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
701
711
  private executeFunction;
702
712
  private debouncedTrigger;
703
713
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
package/dist/index.js CHANGED
@@ -63,9 +63,6 @@ var SignalBroker = class _SignalBroker {
63
63
  if (signalName.includes(" ")) {
64
64
  throw new Error("Signal name must not contain spaces");
65
65
  }
66
- if (signalName.includes("/")) {
67
- throw new Error("Signal name must not contain slashes");
68
- }
69
66
  if (signalName.includes("\\")) {
70
67
  throw new Error("Signal name must not contain backslashes");
71
68
  }
@@ -85,7 +82,7 @@ var SignalBroker = class _SignalBroker {
85
82
  this.metaRunner = metaRunner;
86
83
  }
87
84
  init() {
88
- Cadenza.createMetaTask(
85
+ Cadenza.createDebounceMetaTask(
89
86
  "Execute and clear queued signals",
90
87
  () => {
91
88
  for (const [id, signals] of this.emitStacks.entries()) {
@@ -97,8 +94,10 @@ var SignalBroker = class _SignalBroker {
97
94
  }
98
95
  return true;
99
96
  },
100
- "Executes queued signals and clears the stack"
101
- ).doOn("meta.clear_signal_queue_requested");
97
+ "Executes queued signals and clears the stack",
98
+ 500,
99
+ { maxWait: 1e4 }
100
+ ).doOn("meta.process_signal_queue_requested");
102
101
  this.getSignalsTask = Cadenza.createMetaTask("Get signals", (ctx) => {
103
102
  return {
104
103
  __signals: Array.from(this.signalObservers.keys()),
@@ -137,7 +136,6 @@ var SignalBroker = class _SignalBroker {
137
136
  * @param context The payload.
138
137
  * @edge Fire-and-forget; guards against loops per execId (from context.__graphExecId).
139
138
  * @edge For distribution, SignalTask can prefix and proxy remote.
140
- * @throws Error on detected loop.
141
139
  */
142
140
  emit(signal, context = {}) {
143
141
  const execId = context.__routineExecId || "global";
@@ -149,6 +147,7 @@ var SignalBroker = class _SignalBroker {
149
147
  executed = this.execute(signal, context);
150
148
  } finally {
151
149
  if (executed) stack.delete(signal);
150
+ if (stack.size === 0) this.emitStacks.delete(execId);
152
151
  }
153
152
  }
154
153
  execute(signal, context) {
@@ -159,6 +158,12 @@ var SignalBroker = class _SignalBroker {
159
158
  const parent = parts.slice(0, i).join(".");
160
159
  executed = executed || this.executeListener(parent + ".*", context);
161
160
  }
161
+ if (this.debug) {
162
+ console.log(
163
+ `Emitted signal ${signal} with context ${JSON.stringify(context)}`,
164
+ executed ? "\u2705" : "\u274C"
165
+ );
166
+ }
162
167
  return executed;
163
168
  }
164
169
  executeListener(signal, context) {
@@ -166,11 +171,6 @@ var SignalBroker = class _SignalBroker {
166
171
  const runner = signal.startsWith("meta") ? this.metaRunner : this.runner;
167
172
  if (obs && obs.tasks.size && runner) {
168
173
  obs.fn(runner, Array.from(obs.tasks), context);
169
- if (this.debug) {
170
- console.log(
171
- `Emitted signal ${signal} with context ${JSON.stringify(context)}`
172
- );
173
- }
174
174
  return true;
175
175
  }
176
176
  return false;
@@ -185,6 +185,7 @@ var SignalBroker = class _SignalBroker {
185
185
  this.emit("meta.signal_broker.added", { __signalName: signal });
186
186
  }
187
187
  }
188
+ // TODO schedule signals
188
189
  /**
189
190
  * Lists all observed signals.
190
191
  * @returns Array of signals.
@@ -638,7 +639,7 @@ var SignalEmitter = class {
638
639
 
639
640
  // src/graph/execution/GraphNode.ts
640
641
  var GraphNode = class _GraphNode extends SignalEmitter {
641
- constructor(task, context, routineExecId, prevNodes = []) {
642
+ constructor(task, context, routineExecId, prevNodes = [], debug = false) {
642
643
  super(task.isMeta);
643
644
  this.divided = false;
644
645
  this.splitGroupId = "";
@@ -652,12 +653,17 @@ var GraphNode = class _GraphNode extends SignalEmitter {
652
653
  this.failed = false;
653
654
  this.errored = false;
654
655
  this.destroyed = false;
656
+ this.debug = false;
655
657
  this.task = task;
656
658
  this.context = context;
657
659
  this.previousNodes = prevNodes;
658
660
  this.id = (0, import_uuid3.v4)();
659
661
  this.routineExecId = routineExecId;
660
662
  this.splitGroupId = routineExecId;
663
+ this.debug = debug;
664
+ }
665
+ setDebug(value) {
666
+ this.debug = value;
661
667
  }
662
668
  isUnique() {
663
669
  return this.task.isUnique;
@@ -729,6 +735,9 @@ var GraphNode = class _GraphNode extends SignalEmitter {
729
735
  if (this.previousNodes.length === 0) {
730
736
  this.emit("meta.node.started_routine_execution", memento);
731
737
  }
738
+ if (this.debug) {
739
+ this.log();
740
+ }
732
741
  this.emit("meta.node.started", memento);
733
742
  return this.executionStart;
734
743
  }
@@ -754,7 +763,6 @@ var GraphNode = class _GraphNode extends SignalEmitter {
754
763
  }
755
764
  execute() {
756
765
  if (!this.divided && !this.processing) {
757
- this.start();
758
766
  this.processing = true;
759
767
  const inputValidation = this.task.validateInput(
760
768
  this.context.getContext()
@@ -925,7 +933,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
925
933
  return this;
926
934
  }
927
935
  clone() {
928
- return new _GraphNode(this.task, this.context, this.routineExecId, [this]);
936
+ return new _GraphNode(
937
+ this.task,
938
+ this.context,
939
+ this.routineExecId,
940
+ [this],
941
+ this.debug
942
+ );
929
943
  }
930
944
  consume(node) {
931
945
  this.context = this.context.combine(node.context);
@@ -1021,7 +1035,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1021
1035
  };
1022
1036
  }
1023
1037
  log() {
1024
- console.log(this.task.name, this.context.getContext(), this.executionTime);
1038
+ console.log(this.task.name, this.context.getContext(), this.routineExecId);
1025
1039
  }
1026
1040
  };
1027
1041
 
@@ -1892,13 +1906,10 @@ var GraphRunner = class extends SignalEmitter {
1892
1906
  __scheduled: Date.now()
1893
1907
  };
1894
1908
  this.emit("meta.runner.added_tasks", data);
1895
- if (this.debug) {
1896
- console.log(
1897
- `Running routine ${routineName} with context ${JSON.stringify(ctx.getContext())}`
1898
- );
1899
- }
1900
1909
  allTasks.forEach(
1901
- (task) => this.currentRun.addNode(new GraphNode(task, ctx, routineExecId))
1910
+ (task) => this.currentRun.addNode(
1911
+ new GraphNode(task, ctx, routineExecId, [], this.debug)
1912
+ )
1902
1913
  );
1903
1914
  }
1904
1915
  /**
@@ -1929,9 +1940,6 @@ var GraphRunner = class extends SignalEmitter {
1929
1940
  return this.reset();
1930
1941
  }
1931
1942
  reset() {
1932
- if (this.debug) {
1933
- this.currentRun.log();
1934
- }
1935
1943
  this.isRunning = false;
1936
1944
  const lastRun = this.currentRun;
1937
1945
  if (!this.debug) {
@@ -1968,7 +1976,7 @@ var GraphRunner = class extends SignalEmitter {
1968
1976
 
1969
1977
  // src/graph/definition/DebounceTask.ts
1970
1978
  var DebounceTask = class extends Task {
1971
- 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) {
1979
+ 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) {
1972
1980
  super(
1973
1981
  name,
1974
1982
  task,
@@ -1985,15 +1993,19 @@ var DebounceTask = class extends Task {
1985
1993
  validateOutputSchema
1986
1994
  );
1987
1995
  this.timer = null;
1996
+ this.maxTimer = null;
1997
+ this.hasLaterCall = false;
1988
1998
  this.lastResolve = null;
1989
1999
  this.lastReject = null;
1990
2000
  this.lastContext = null;
1991
2001
  this.lastTimeout = null;
2002
+ this.lastProgressCallback = null;
1992
2003
  this.debounceTime = debounceTime;
1993
2004
  this.leading = leading;
1994
2005
  this.trailing = trailing;
2006
+ this.maxWait = maxWait;
1995
2007
  }
1996
- executeFunction(progressCallback) {
2008
+ executeFunction() {
1997
2009
  if (this.lastTimeout) {
1998
2010
  clearTimeout(this.lastTimeout);
1999
2011
  }
@@ -2001,7 +2013,7 @@ var DebounceTask = class extends Task {
2001
2013
  try {
2002
2014
  result = this.taskFunction(
2003
2015
  this.lastContext.getClonedContext(),
2004
- progressCallback
2016
+ this.lastProgressCallback
2005
2017
  );
2006
2018
  } catch (error) {
2007
2019
  if (this.lastResolve) {
@@ -2019,21 +2031,46 @@ var DebounceTask = class extends Task {
2019
2031
  }
2020
2032
  debouncedTrigger(resolve, reject, context, timeout, progressCallback) {
2021
2033
  const callNow = this.leading && this.timer === null;
2034
+ const isNewBurst = this.timer === null;
2022
2035
  if (this.timer !== null) {
2023
2036
  clearTimeout(this.timer);
2037
+ this.timer = null;
2024
2038
  }
2025
2039
  this.lastResolve = resolve;
2026
2040
  this.lastReject = reject;
2027
2041
  this.lastContext = context;
2028
2042
  this.lastTimeout = timeout;
2043
+ this.lastProgressCallback = progressCallback;
2044
+ if (!callNow) {
2045
+ this.hasLaterCall = true;
2046
+ }
2029
2047
  this.timer = setTimeout(() => {
2030
2048
  this.timer = null;
2031
- if (this.trailing) {
2032
- this.executeFunction(progressCallback);
2049
+ if (this.trailing && this.hasLaterCall) {
2050
+ this.executeFunction();
2051
+ this.hasLaterCall = false;
2052
+ }
2053
+ if (this.maxTimer) {
2054
+ clearTimeout(this.maxTimer);
2055
+ this.maxTimer = null;
2033
2056
  }
2034
2057
  }, this.debounceTime);
2035
2058
  if (callNow) {
2036
- this.executeFunction(progressCallback);
2059
+ this.executeFunction();
2060
+ this.hasLaterCall = false;
2061
+ }
2062
+ if (this.maxWait > 0 && isNewBurst) {
2063
+ this.maxTimer = setTimeout(() => {
2064
+ this.maxTimer = null;
2065
+ if (this.trailing && this.hasLaterCall) {
2066
+ if (this.timer) {
2067
+ clearTimeout(this.timer);
2068
+ this.timer = null;
2069
+ }
2070
+ this.executeFunction();
2071
+ this.hasLaterCall = false;
2072
+ }
2073
+ }, this.maxWait);
2037
2074
  }
2038
2075
  }
2039
2076
  execute(context, progressCallback) {
@@ -2167,8 +2204,15 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
2167
2204
  this.nodes = [];
2168
2205
  this.executionTime = 0;
2169
2206
  this.executionStart = 0;
2207
+ this.debug = false;
2170
2208
  this.index = index;
2171
2209
  }
2210
+ setDebug(value) {
2211
+ this.debug = value;
2212
+ for (const node of this.nodes) {
2213
+ node.setDebug(value);
2214
+ }
2215
+ }
2172
2216
  get hasPreceding() {
2173
2217
  return !!this.previous && this.previous instanceof _GraphLayer;
2174
2218
  }
@@ -2298,6 +2342,10 @@ var GraphBuilder = class {
2298
2342
  constructor() {
2299
2343
  this.topLayerIndex = 0;
2300
2344
  this.layers = [];
2345
+ this.debug = false;
2346
+ }
2347
+ setDebug(value) {
2348
+ this.debug = value;
2301
2349
  }
2302
2350
  getResult() {
2303
2351
  return this.graph;
@@ -2343,7 +2391,9 @@ var GraphBuilder = class {
2343
2391
  }
2344
2392
  }
2345
2393
  createLayer(index) {
2346
- return new SyncGraphLayer(index);
2394
+ const layer = new SyncGraphLayer(index);
2395
+ layer.setDebug(this.debug);
2396
+ return layer;
2347
2397
  }
2348
2398
  getLayer(layerIndex) {
2349
2399
  return this.layers[layerIndex - this.topLayerIndex];
@@ -2555,7 +2605,9 @@ var GraphAsyncQueueBuilder = class extends GraphBuilder {
2555
2605
  }
2556
2606
  }
2557
2607
  createLayer(index) {
2558
- return new AsyncGraphLayer(index);
2608
+ const layer = new AsyncGraphLayer(index);
2609
+ layer.setDebug(this.debug);
2610
+ return layer;
2559
2611
  }
2560
2612
  };
2561
2613
 
@@ -2599,6 +2651,10 @@ var Cadenza = class {
2599
2651
  this.runner = new GraphRunner();
2600
2652
  this.metaRunner = new GraphRunner(true);
2601
2653
  this.broker.bootstrap(this.runner, this.metaRunner);
2654
+ if (this.mode === "debug" || this.mode === "dev") {
2655
+ this.broker.setDebug(true);
2656
+ this.runner.setDebug(true);
2657
+ }
2602
2658
  this.registry = GraphRegistry.instance;
2603
2659
  this.broker.init();
2604
2660
  this.runner.init();
@@ -2612,8 +2668,10 @@ var Cadenza = class {
2612
2668
  }
2613
2669
  static setMode(mode) {
2614
2670
  this.mode = mode;
2671
+ this.bootstrap();
2615
2672
  if (mode === "debug" || mode === "dev") {
2616
2673
  this.broker.setDebug(true);
2674
+ this.runner.setDebug(true);
2617
2675
  }
2618
2676
  }
2619
2677
  /**
@@ -2807,6 +2865,7 @@ var Cadenza = class {
2807
2865
  register: true,
2808
2866
  leading: false,
2809
2867
  trailing: true,
2868
+ maxWait: 0,
2810
2869
  isUnique: false,
2811
2870
  isMeta: false,
2812
2871
  inputSchema: void 0,
@@ -2823,6 +2882,7 @@ var Cadenza = class {
2823
2882
  debounceTime,
2824
2883
  options.leading,
2825
2884
  options.trailing,
2885
+ options.maxWait,
2826
2886
  options.concurrency,
2827
2887
  options.timeout,
2828
2888
  options.register,
@@ -2849,6 +2909,7 @@ var Cadenza = class {
2849
2909
  register: true,
2850
2910
  leading: false,
2851
2911
  trailing: true,
2912
+ maxWait: 0,
2852
2913
  isUnique: false,
2853
2914
  isMeta: false,
2854
2915
  inputSchema: void 0,