@cadenza.io/core 3.13.2 → 3.14.0

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
@@ -937,7 +937,7 @@ declare class Task extends SignalEmitter implements Graph {
937
937
  * @return {this} The current task instance for method chaining.
938
938
  * @throws {Error} Throws an error if adding a predecessor creates a cycle in the task structure.
939
939
  */
940
- doAfter(...tasks: Task[]): this;
940
+ doAfter(...tasks: (Task | undefined)[]): this;
941
941
  /**
942
942
  * Adds a sequence of tasks as successors to the current task, ensuring no cyclic dependencies are introduced.
943
943
  * Metrics are emitted when a relationship is successfully added.
@@ -946,7 +946,7 @@ declare class Task extends SignalEmitter implements Graph {
946
946
  * @return {this} Returns the current task instance for method chaining.
947
947
  * @throws {Error} Throws an error if adding a task causes a cyclic dependency.
948
948
  */
949
- then(...tasks: Task[]): this;
949
+ then(...tasks: (Task | undefined)[]): this;
950
950
  /**
951
951
  * Decouples the current task from the provided task by removing mutual references.
952
952
  *
@@ -1431,6 +1431,7 @@ declare class SignalBroker {
1431
1431
  validateSignalName(signalName: string): void;
1432
1432
  runner: GraphRunner | undefined;
1433
1433
  metaRunner: GraphRunner | undefined;
1434
+ debouncedEmitters: Map<string, any>;
1434
1435
  clearSignalsTask: Task | undefined;
1435
1436
  getSignalsTask: Task | undefined;
1436
1437
  registerSignalTask: Task | undefined;
@@ -1488,6 +1489,7 @@ declare class SignalBroker {
1488
1489
  * @returns a handle with `clear()` to stop the loop.
1489
1490
  */
1490
1491
  throttle(signal: string, context: AnyObject, intervalMs?: number, leading?: boolean, startDateTime?: Date): ThrottleHandle;
1492
+ debounce(signal: string, context: any, delayMs?: number): void;
1491
1493
  /**
1492
1494
  * Emits a signal with the specified context, triggering any associated handlers for that signal.
1493
1495
  *
@@ -1804,6 +1806,7 @@ declare class Cadenza {
1804
1806
  static emit(event: string, data?: AnyObject): void;
1805
1807
  static schedule(taskName: string, context: AnyObject, timeoutMs: number, exactDateTime?: Date): void;
1806
1808
  static throttle(taskName: string, context: AnyObject, intervalMs: number, leading?: boolean, startDateTime?: Date): void;
1809
+ static debounce(signalName: string, context: any, delayMs: number): void;
1807
1810
  static get(taskName: string): Task | undefined;
1808
1811
  /**
1809
1812
  * Creates and registers a new task with the specified parameters and options.
package/dist/index.d.ts CHANGED
@@ -937,7 +937,7 @@ declare class Task extends SignalEmitter implements Graph {
937
937
  * @return {this} The current task instance for method chaining.
938
938
  * @throws {Error} Throws an error if adding a predecessor creates a cycle in the task structure.
939
939
  */
940
- doAfter(...tasks: Task[]): this;
940
+ doAfter(...tasks: (Task | undefined)[]): this;
941
941
  /**
942
942
  * Adds a sequence of tasks as successors to the current task, ensuring no cyclic dependencies are introduced.
943
943
  * Metrics are emitted when a relationship is successfully added.
@@ -946,7 +946,7 @@ declare class Task extends SignalEmitter implements Graph {
946
946
  * @return {this} Returns the current task instance for method chaining.
947
947
  * @throws {Error} Throws an error if adding a task causes a cyclic dependency.
948
948
  */
949
- then(...tasks: Task[]): this;
949
+ then(...tasks: (Task | undefined)[]): this;
950
950
  /**
951
951
  * Decouples the current task from the provided task by removing mutual references.
952
952
  *
@@ -1431,6 +1431,7 @@ declare class SignalBroker {
1431
1431
  validateSignalName(signalName: string): void;
1432
1432
  runner: GraphRunner | undefined;
1433
1433
  metaRunner: GraphRunner | undefined;
1434
+ debouncedEmitters: Map<string, any>;
1434
1435
  clearSignalsTask: Task | undefined;
1435
1436
  getSignalsTask: Task | undefined;
1436
1437
  registerSignalTask: Task | undefined;
@@ -1488,6 +1489,7 @@ declare class SignalBroker {
1488
1489
  * @returns a handle with `clear()` to stop the loop.
1489
1490
  */
1490
1491
  throttle(signal: string, context: AnyObject, intervalMs?: number, leading?: boolean, startDateTime?: Date): ThrottleHandle;
1492
+ debounce(signal: string, context: any, delayMs?: number): void;
1491
1493
  /**
1492
1494
  * Emits a signal with the specified context, triggering any associated handlers for that signal.
1493
1495
  *
@@ -1804,6 +1806,7 @@ declare class Cadenza {
1804
1806
  static emit(event: string, data?: AnyObject): void;
1805
1807
  static schedule(taskName: string, context: AnyObject, timeoutMs: number, exactDateTime?: Date): void;
1806
1808
  static throttle(taskName: string, context: AnyObject, intervalMs: number, leading?: boolean, startDateTime?: Date): void;
1809
+ static debounce(signalName: string, context: any, delayMs: number): void;
1807
1810
  static get(taskName: string): Task | undefined;
1808
1811
  /**
1809
1812
  * Creates and registers a new task with the specified parameters and options.
package/dist/index.js CHANGED
@@ -79,11 +79,227 @@ function formatTimestamp(timestamp) {
79
79
 
80
80
  // src/engine/SignalBroker.ts
81
81
  var import_uuid = require("uuid");
82
+
83
+ // node_modules/lodash-es/isObject.js
84
+ function isObject(value) {
85
+ var type = typeof value;
86
+ return value != null && (type == "object" || type == "function");
87
+ }
88
+ var isObject_default = isObject;
89
+
90
+ // node_modules/lodash-es/_freeGlobal.js
91
+ var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
92
+ var freeGlobal_default = freeGlobal;
93
+
94
+ // node_modules/lodash-es/_root.js
95
+ var freeSelf = typeof self == "object" && self && self.Object === Object && self;
96
+ var root = freeGlobal_default || freeSelf || Function("return this")();
97
+ var root_default = root;
98
+
99
+ // node_modules/lodash-es/now.js
100
+ var now = function() {
101
+ return root_default.Date.now();
102
+ };
103
+ var now_default = now;
104
+
105
+ // node_modules/lodash-es/_trimmedEndIndex.js
106
+ var reWhitespace = /\s/;
107
+ function trimmedEndIndex(string) {
108
+ var index = string.length;
109
+ while (index-- && reWhitespace.test(string.charAt(index))) {
110
+ }
111
+ return index;
112
+ }
113
+ var trimmedEndIndex_default = trimmedEndIndex;
114
+
115
+ // node_modules/lodash-es/_baseTrim.js
116
+ var reTrimStart = /^\s+/;
117
+ function baseTrim(string) {
118
+ return string ? string.slice(0, trimmedEndIndex_default(string) + 1).replace(reTrimStart, "") : string;
119
+ }
120
+ var baseTrim_default = baseTrim;
121
+
122
+ // node_modules/lodash-es/_Symbol.js
123
+ var Symbol2 = root_default.Symbol;
124
+ var Symbol_default = Symbol2;
125
+
126
+ // node_modules/lodash-es/_getRawTag.js
127
+ var objectProto = Object.prototype;
128
+ var hasOwnProperty = objectProto.hasOwnProperty;
129
+ var nativeObjectToString = objectProto.toString;
130
+ var symToStringTag = Symbol_default ? Symbol_default.toStringTag : void 0;
131
+ function getRawTag(value) {
132
+ var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
133
+ try {
134
+ value[symToStringTag] = void 0;
135
+ var unmasked = true;
136
+ } catch (e) {
137
+ }
138
+ var result = nativeObjectToString.call(value);
139
+ if (unmasked) {
140
+ if (isOwn) {
141
+ value[symToStringTag] = tag;
142
+ } else {
143
+ delete value[symToStringTag];
144
+ }
145
+ }
146
+ return result;
147
+ }
148
+ var getRawTag_default = getRawTag;
149
+
150
+ // node_modules/lodash-es/_objectToString.js
151
+ var objectProto2 = Object.prototype;
152
+ var nativeObjectToString2 = objectProto2.toString;
153
+ function objectToString(value) {
154
+ return nativeObjectToString2.call(value);
155
+ }
156
+ var objectToString_default = objectToString;
157
+
158
+ // node_modules/lodash-es/_baseGetTag.js
159
+ var nullTag = "[object Null]";
160
+ var undefinedTag = "[object Undefined]";
161
+ var symToStringTag2 = Symbol_default ? Symbol_default.toStringTag : void 0;
162
+ function baseGetTag(value) {
163
+ if (value == null) {
164
+ return value === void 0 ? undefinedTag : nullTag;
165
+ }
166
+ return symToStringTag2 && symToStringTag2 in Object(value) ? getRawTag_default(value) : objectToString_default(value);
167
+ }
168
+ var baseGetTag_default = baseGetTag;
169
+
170
+ // node_modules/lodash-es/isObjectLike.js
171
+ function isObjectLike(value) {
172
+ return value != null && typeof value == "object";
173
+ }
174
+ var isObjectLike_default = isObjectLike;
175
+
176
+ // node_modules/lodash-es/isSymbol.js
177
+ var symbolTag = "[object Symbol]";
178
+ function isSymbol(value) {
179
+ return typeof value == "symbol" || isObjectLike_default(value) && baseGetTag_default(value) == symbolTag;
180
+ }
181
+ var isSymbol_default = isSymbol;
182
+
183
+ // node_modules/lodash-es/toNumber.js
184
+ var NAN = 0 / 0;
185
+ var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
186
+ var reIsBinary = /^0b[01]+$/i;
187
+ var reIsOctal = /^0o[0-7]+$/i;
188
+ var freeParseInt = parseInt;
189
+ function toNumber(value) {
190
+ if (typeof value == "number") {
191
+ return value;
192
+ }
193
+ if (isSymbol_default(value)) {
194
+ return NAN;
195
+ }
196
+ if (isObject_default(value)) {
197
+ var other = typeof value.valueOf == "function" ? value.valueOf() : value;
198
+ value = isObject_default(other) ? other + "" : other;
199
+ }
200
+ if (typeof value != "string") {
201
+ return value === 0 ? value : +value;
202
+ }
203
+ value = baseTrim_default(value);
204
+ var isBinary = reIsBinary.test(value);
205
+ return isBinary || reIsOctal.test(value) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : reIsBadHex.test(value) ? NAN : +value;
206
+ }
207
+ var toNumber_default = toNumber;
208
+
209
+ // node_modules/lodash-es/debounce.js
210
+ var FUNC_ERROR_TEXT = "Expected a function";
211
+ var nativeMax = Math.max;
212
+ var nativeMin = Math.min;
213
+ function debounce(func, wait, options) {
214
+ var lastArgs, lastThis, maxWait, result, timerId, lastCallTime, lastInvokeTime = 0, leading = false, maxing = false, trailing = true;
215
+ if (typeof func != "function") {
216
+ throw new TypeError(FUNC_ERROR_TEXT);
217
+ }
218
+ wait = toNumber_default(wait) || 0;
219
+ if (isObject_default(options)) {
220
+ leading = !!options.leading;
221
+ maxing = "maxWait" in options;
222
+ maxWait = maxing ? nativeMax(toNumber_default(options.maxWait) || 0, wait) : maxWait;
223
+ trailing = "trailing" in options ? !!options.trailing : trailing;
224
+ }
225
+ function invokeFunc(time) {
226
+ var args = lastArgs, thisArg = lastThis;
227
+ lastArgs = lastThis = void 0;
228
+ lastInvokeTime = time;
229
+ result = func.apply(thisArg, args);
230
+ return result;
231
+ }
232
+ function leadingEdge(time) {
233
+ lastInvokeTime = time;
234
+ timerId = setTimeout(timerExpired, wait);
235
+ return leading ? invokeFunc(time) : result;
236
+ }
237
+ function remainingWait(time) {
238
+ var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime, timeWaiting = wait - timeSinceLastCall;
239
+ return maxing ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting;
240
+ }
241
+ function shouldInvoke(time) {
242
+ var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime;
243
+ return lastCallTime === void 0 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
244
+ }
245
+ function timerExpired() {
246
+ var time = now_default();
247
+ if (shouldInvoke(time)) {
248
+ return trailingEdge(time);
249
+ }
250
+ timerId = setTimeout(timerExpired, remainingWait(time));
251
+ }
252
+ function trailingEdge(time) {
253
+ timerId = void 0;
254
+ if (trailing && lastArgs) {
255
+ return invokeFunc(time);
256
+ }
257
+ lastArgs = lastThis = void 0;
258
+ return result;
259
+ }
260
+ function cancel() {
261
+ if (timerId !== void 0) {
262
+ clearTimeout(timerId);
263
+ }
264
+ lastInvokeTime = 0;
265
+ lastArgs = lastCallTime = lastThis = timerId = void 0;
266
+ }
267
+ function flush() {
268
+ return timerId === void 0 ? result : trailingEdge(now_default());
269
+ }
270
+ function debounced() {
271
+ var time = now_default(), isInvoking = shouldInvoke(time);
272
+ lastArgs = arguments;
273
+ lastThis = this;
274
+ lastCallTime = time;
275
+ if (isInvoking) {
276
+ if (timerId === void 0) {
277
+ return leadingEdge(lastCallTime);
278
+ }
279
+ if (maxing) {
280
+ clearTimeout(timerId);
281
+ timerId = setTimeout(timerExpired, wait);
282
+ return invokeFunc(lastCallTime);
283
+ }
284
+ }
285
+ if (timerId === void 0) {
286
+ timerId = setTimeout(timerExpired, wait);
287
+ }
288
+ return result;
289
+ }
290
+ debounced.cancel = cancel;
291
+ debounced.flush = flush;
292
+ return debounced;
293
+ }
294
+ var debounce_default = debounce;
295
+
296
+ // src/engine/SignalBroker.ts
82
297
  var SignalBroker = class _SignalBroker {
83
298
  // execId -> emitted signals
84
299
  constructor() {
85
300
  this.debug = false;
86
301
  this.verbose = false;
302
+ this.debouncedEmitters = /* @__PURE__ */ new Map();
87
303
  // TODO: Signals should be a class with a the observers, registered flag and other data.
88
304
  this.signalObservers = /* @__PURE__ */ new Map();
89
305
  this.emitStacks = /* @__PURE__ */ new Map();
@@ -170,15 +386,15 @@ var SignalBroker = class _SignalBroker {
170
386
  }
171
387
  }));
172
388
  return {
173
- __signals: processedSignals,
389
+ signals: processedSignals,
174
390
  ...ctx
175
391
  };
176
392
  });
177
393
  this.registerSignalTask = Cadenza.createMetaTask(
178
394
  "Register signal",
179
395
  (ctx) => {
180
- const { __signalName } = ctx;
181
- this.signalObservers.get(__signalName).registered = true;
396
+ const { signalName } = ctx;
397
+ this.signalObservers.get(signalName).registered = true;
182
398
  }
183
399
  ).doOn("meta.signal.registered");
184
400
  }
@@ -247,20 +463,20 @@ var SignalBroker = class _SignalBroker {
247
463
  }
248
464
  const emit = () => this.emit(signal, context);
249
465
  if (leading) {
250
- const now = Date.now();
466
+ const now2 = Date.now();
251
467
  const start = startDateTime?.getTime();
252
- if (!start || start <= now) {
468
+ if (!start || start <= now2) {
253
469
  emit();
254
470
  }
255
471
  }
256
472
  let firstDelay = intervalMs;
257
473
  if (startDateTime) {
258
474
  let slot = startDateTime.getTime();
259
- const now = Date.now();
260
- while (slot < now) {
475
+ const now2 = Date.now();
476
+ while (slot < now2) {
261
477
  slot += intervalMs;
262
478
  }
263
- firstDelay = slot - now;
479
+ firstDelay = slot - now2;
264
480
  }
265
481
  let timer = null;
266
482
  let stopped = false;
@@ -277,6 +493,19 @@ var SignalBroker = class _SignalBroker {
277
493
  }
278
494
  };
279
495
  }
496
+ debounce(signal, context, delayMs = 500) {
497
+ let debouncedEmitter = this.debouncedEmitters.get(signal);
498
+ if (!debouncedEmitter) {
499
+ this.debouncedEmitters.set(
500
+ signal,
501
+ debounce_default((ctx) => {
502
+ this.emit(signal, ctx);
503
+ }, delayMs)
504
+ );
505
+ debouncedEmitter = this.debouncedEmitters.get(signal);
506
+ }
507
+ debouncedEmitter(context);
508
+ }
280
509
  /**
281
510
  * Emits a signal with the specified context, triggering any associated handlers for that signal.
282
511
  *
@@ -444,7 +673,7 @@ var SignalBroker = class _SignalBroker {
444
673
  return;
445
674
  }
446
675
  }
447
- this.emit("meta.signal_broker.added", { __signalName: _signal });
676
+ this.emit("meta.signal_broker.added", { signalName: _signal });
448
677
  }
449
678
  }
450
679
  /**
@@ -2420,6 +2649,7 @@ var Task = class _Task extends SignalEmitter {
2420
2649
  */
2421
2650
  doAfter(...tasks) {
2422
2651
  for (const pred of tasks) {
2652
+ if (!pred) continue;
2423
2653
  if (this.predecessorTasks.has(pred)) continue;
2424
2654
  pred.nextTasks.add(this);
2425
2655
  this.predecessorTasks.add(pred);
@@ -2450,6 +2680,7 @@ var Task = class _Task extends SignalEmitter {
2450
2680
  */
2451
2681
  then(...tasks) {
2452
2682
  for (const next of tasks) {
2683
+ if (!next) continue;
2453
2684
  if (this.nextTasks.has(next)) continue;
2454
2685
  this.nextTasks.add(next);
2455
2686
  next.predecessorTasks.add(this);
@@ -4323,6 +4554,9 @@ var Cadenza = class {
4323
4554
  startDateTime
4324
4555
  );
4325
4556
  }
4557
+ static debounce(signalName, context, delayMs) {
4558
+ this.broker?.debounce(signalName, context, delayMs);
4559
+ }
4326
4560
  static get(taskName) {
4327
4561
  return this.registry?.tasks.get(taskName);
4328
4562
  }