@cadenza.io/core 3.16.3 → 3.17.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
@@ -817,6 +817,7 @@ declare class Task extends SignalEmitter implements Graph {
817
817
  signalsToEmitAfter: Set<string>;
818
818
  signalsToEmitOnFail: Set<string>;
819
819
  observedSignals: Set<string>;
820
+ intents: Set<string>;
820
821
  readonly taskFunction: TaskFunction;
821
822
  /**
822
823
  * Constructs an instance of the task with the specified properties and configuration options.
@@ -1052,6 +1053,7 @@ declare class Task extends SignalEmitter implements Graph {
1052
1053
  * @return {this} Returns the current instance to allow method chaining.
1053
1054
  */
1054
1055
  detachAllSignals(): this;
1056
+ handlesIntent(intent: string): this;
1055
1057
  /**
1056
1058
  * Maps over the signals in the `signalsToEmitAfter` set and applies a callback function to each signal.
1057
1059
  *
@@ -1401,11 +1403,14 @@ declare class GraphRunner extends SignalEmitter {
1401
1403
  interface EmitOptions {
1402
1404
  squash?: boolean;
1403
1405
  squashId?: string | null;
1406
+ groupId?: string | null;
1404
1407
  mergeFunction?: ((oldContext: AnyObject, newContext: AnyObject) => AnyObject) | null;
1405
1408
  debounce?: boolean;
1409
+ throttle?: boolean;
1406
1410
  delayMs?: number;
1407
1411
  schedule?: boolean;
1408
1412
  exactDateTime?: Date | null;
1413
+ throttleBatch?: number;
1409
1414
  }
1410
1415
  /**
1411
1416
  * This class manages signals and observers, enabling communication across different parts of an application.
@@ -1433,6 +1438,8 @@ declare class SignalBroker {
1433
1438
  debouncedEmitters: Map<string, any>;
1434
1439
  squashedEmitters: Map<string, any>;
1435
1440
  squashedContexts: Map<string, any>;
1441
+ throttleEmitters: Map<string, any>;
1442
+ throttleQueues: Map<string, any>;
1436
1443
  getSignalsTask: Task | undefined;
1437
1444
  registerSignalTask: Task | undefined;
1438
1445
  signalObservers: Map<string, {
@@ -1492,6 +1499,7 @@ declare class SignalBroker {
1492
1499
  debounce(signal: string, context: any, options?: {
1493
1500
  delayMs: number;
1494
1501
  }): void;
1502
+ throttle(signal: string, context: any, options?: EmitOptions): void;
1495
1503
  /**
1496
1504
  * Aggregates and debounces multiple events with the same identifier to minimize redundant operations.
1497
1505
  *
package/dist/index.d.ts CHANGED
@@ -817,6 +817,7 @@ declare class Task extends SignalEmitter implements Graph {
817
817
  signalsToEmitAfter: Set<string>;
818
818
  signalsToEmitOnFail: Set<string>;
819
819
  observedSignals: Set<string>;
820
+ intents: Set<string>;
820
821
  readonly taskFunction: TaskFunction;
821
822
  /**
822
823
  * Constructs an instance of the task with the specified properties and configuration options.
@@ -1052,6 +1053,7 @@ declare class Task extends SignalEmitter implements Graph {
1052
1053
  * @return {this} Returns the current instance to allow method chaining.
1053
1054
  */
1054
1055
  detachAllSignals(): this;
1056
+ handlesIntent(intent: string): this;
1055
1057
  /**
1056
1058
  * Maps over the signals in the `signalsToEmitAfter` set and applies a callback function to each signal.
1057
1059
  *
@@ -1401,11 +1403,14 @@ declare class GraphRunner extends SignalEmitter {
1401
1403
  interface EmitOptions {
1402
1404
  squash?: boolean;
1403
1405
  squashId?: string | null;
1406
+ groupId?: string | null;
1404
1407
  mergeFunction?: ((oldContext: AnyObject, newContext: AnyObject) => AnyObject) | null;
1405
1408
  debounce?: boolean;
1409
+ throttle?: boolean;
1406
1410
  delayMs?: number;
1407
1411
  schedule?: boolean;
1408
1412
  exactDateTime?: Date | null;
1413
+ throttleBatch?: number;
1409
1414
  }
1410
1415
  /**
1411
1416
  * This class manages signals and observers, enabling communication across different parts of an application.
@@ -1433,6 +1438,8 @@ declare class SignalBroker {
1433
1438
  debouncedEmitters: Map<string, any>;
1434
1439
  squashedEmitters: Map<string, any>;
1435
1440
  squashedContexts: Map<string, any>;
1441
+ throttleEmitters: Map<string, any>;
1442
+ throttleQueues: Map<string, any>;
1436
1443
  getSignalsTask: Task | undefined;
1437
1444
  registerSignalTask: Task | undefined;
1438
1445
  signalObservers: Map<string, {
@@ -1492,6 +1499,7 @@ declare class SignalBroker {
1492
1499
  debounce(signal: string, context: any, options?: {
1493
1500
  delayMs: number;
1494
1501
  }): void;
1502
+ throttle(signal: string, context: any, options?: EmitOptions): void;
1495
1503
  /**
1496
1504
  * Aggregates and debounces multiple events with the same identifier to minimize redundant operations.
1497
1505
  *
package/dist/index.js CHANGED
@@ -1262,6 +1262,11 @@ var merge = createAssigner_default(function(object, source, srcIndex) {
1262
1262
  });
1263
1263
  var merge_default = merge;
1264
1264
 
1265
+ // src/utils/promise.ts
1266
+ function sleep(ms) {
1267
+ return new Promise((resolve) => setTimeout(resolve, ms));
1268
+ }
1269
+
1265
1270
  // src/engine/SignalBroker.ts
1266
1271
  var SignalBroker = class _SignalBroker {
1267
1272
  constructor() {
@@ -1270,6 +1275,8 @@ var SignalBroker = class _SignalBroker {
1270
1275
  this.debouncedEmitters = /* @__PURE__ */ new Map();
1271
1276
  this.squashedEmitters = /* @__PURE__ */ new Map();
1272
1277
  this.squashedContexts = /* @__PURE__ */ new Map();
1278
+ this.throttleEmitters = /* @__PURE__ */ new Map();
1279
+ this.throttleQueues = /* @__PURE__ */ new Map();
1273
1280
  // TODO: Signals should be a class with a the observers, registered flag and other data.
1274
1281
  this.signalObservers = /* @__PURE__ */ new Map();
1275
1282
  this.emittedSignalsRegistry = /* @__PURE__ */ new Set();
@@ -1470,6 +1477,35 @@ var SignalBroker = class _SignalBroker {
1470
1477
  }
1471
1478
  debouncedEmitter(context);
1472
1479
  }
1480
+ throttle(signal, context, options = { delayMs: 1e3 }) {
1481
+ let { groupId, delayMs = 300 } = options;
1482
+ if (!groupId) {
1483
+ groupId = signal;
1484
+ }
1485
+ if (!this.throttleQueues.has(groupId)) {
1486
+ this.throttleQueues.set(groupId, []);
1487
+ }
1488
+ const queue = this.throttleQueues.get(groupId);
1489
+ queue.push([signal, context]);
1490
+ if (!this.throttleEmitters.has(groupId)) {
1491
+ this.throttleEmitters.set(groupId, async () => {
1492
+ while (queue.length > 0) {
1493
+ let batchSize = options.throttleBatch ?? 1;
1494
+ if (batchSize > queue.length) {
1495
+ batchSize = queue.length;
1496
+ }
1497
+ for (let i = 0; i < batchSize; i++) {
1498
+ const [nextSignal, nextContext] = queue.shift();
1499
+ this.emit(nextSignal, nextContext);
1500
+ }
1501
+ await sleep(delayMs);
1502
+ }
1503
+ this.throttleEmitters.delete(groupId);
1504
+ this.throttleQueues.delete(groupId);
1505
+ });
1506
+ this.throttleEmitters.get(groupId)();
1507
+ }
1508
+ }
1473
1509
  /**
1474
1510
  * Aggregates and debounces multiple events with the same identifier to minimize redundant operations.
1475
1511
  *
@@ -1482,7 +1518,7 @@ var SignalBroker = class _SignalBroker {
1482
1518
  * @return {void} Does not return a value.
1483
1519
  */
1484
1520
  squash(signal, context, options = { squash: true }) {
1485
- let { squashId, delayMs = 500 } = options;
1521
+ let { squashId, delayMs = 300 } = options;
1486
1522
  if (!squashId) {
1487
1523
  squashId = signal;
1488
1524
  }
@@ -1490,10 +1526,11 @@ var SignalBroker = class _SignalBroker {
1490
1526
  this.squashedEmitters.set(
1491
1527
  squashId,
1492
1528
  debounce_default(() => {
1493
- this.emit(signal, this.squashedContexts.get(squashId));
1529
+ options.squash = false;
1530
+ this.emit(signal, this.squashedContexts.get(squashId), options);
1494
1531
  this.squashedEmitters.delete(squashId);
1495
1532
  this.squashedContexts.delete(squashId);
1496
- }, delayMs ?? 500)
1533
+ }, delayMs ?? 300)
1497
1534
  );
1498
1535
  this.squashedContexts.set(squashId, context);
1499
1536
  } else {
@@ -2078,7 +2115,7 @@ var SignalEmitter = class {
2078
2115
  * @param options
2079
2116
  */
2080
2117
  emit(signal, data = {}, options = {}) {
2081
- Cadenza.broker.emit(signal, data);
2118
+ Cadenza.broker.emit(signal, data, options);
2082
2119
  }
2083
2120
  /**
2084
2121
  * Emits a signal via the broker if not silent.
@@ -2090,15 +2127,10 @@ var SignalEmitter = class {
2090
2127
  if (this.silent) {
2091
2128
  return;
2092
2129
  }
2093
- Cadenza.broker.emit(signal, data);
2130
+ Cadenza.broker.emit(signal, data, options);
2094
2131
  }
2095
2132
  };
2096
2133
 
2097
- // src/utils/promise.ts
2098
- function sleep(ms) {
2099
- return new Promise((resolve) => setTimeout(resolve, ms));
2100
- }
2101
-
2102
2134
  // src/graph/execution/GraphNode.ts
2103
2135
  var GraphNode = class _GraphNode extends SignalEmitter {
2104
2136
  constructor(task, context, routineExecId, prevNodes = [], debug = false, verbose = false) {
@@ -2342,7 +2374,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2342
2374
  },
2343
2375
  filter: { uuid: this.routineExecId }
2344
2376
  },
2345
- { squash: true, squashId: this.routineExecId }
2377
+ { squash: true, squashId: this.routineExecId, delayMs: 200 }
2346
2378
  );
2347
2379
  }
2348
2380
  if (this.debug && !this.task.isSubMeta && !this.context.getMetadata().__isSubMeta || this.verbose) {
@@ -2403,7 +2435,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2403
2435
  },
2404
2436
  filter: { uuid: this.id }
2405
2437
  },
2406
- { squash: true, squashId: this.id }
2438
+ { squash: true, squashId: this.id, delayMs: 0 }
2407
2439
  );
2408
2440
  if (this.graphDone()) {
2409
2441
  const context2 = this.context.getFullContext();
@@ -2425,7 +2457,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2425
2457
  },
2426
2458
  filter: { uuid: this.routineExecId }
2427
2459
  },
2428
- { squash: true, squashId: this.routineExecId }
2460
+ { squash: true, squashId: this.routineExecId, delayMs: 0 }
2429
2461
  );
2430
2462
  }
2431
2463
  return end;
@@ -3306,7 +3338,7 @@ var GraphRunner = class extends SignalEmitter {
3306
3338
  __executionTraceId: executionTraceId
3307
3339
  }
3308
3340
  },
3309
- { squash: true, squashId: routineExecId }
3341
+ { squash: true, squashId: routineExecId, delayMs: 200 }
3310
3342
  );
3311
3343
  }
3312
3344
  allTasks.forEach(
@@ -3481,6 +3513,7 @@ var Task = class _Task extends SignalEmitter {
3481
3513
  this.signalsToEmitAfter = /* @__PURE__ */ new Set();
3482
3514
  this.signalsToEmitOnFail = /* @__PURE__ */ new Set();
3483
3515
  this.observedSignals = /* @__PURE__ */ new Set();
3516
+ this.intents = /* @__PURE__ */ new Set();
3484
3517
  this.name = name;
3485
3518
  this.taskFunction = task;
3486
3519
  this.description = description;
@@ -3557,6 +3590,9 @@ var Task = class _Task extends SignalEmitter {
3557
3590
  signalsToEmitAfter: Array.from(this.signalsToEmitAfter),
3558
3591
  signalsToEmitOnFail: Array.from(this.signalsToEmitOnFail),
3559
3592
  observed: Array.from(this.observedSignals)
3593
+ },
3594
+ intents: {
3595
+ handles: Array.from(this.intents)
3560
3596
  }
3561
3597
  },
3562
3598
  taskInstance: this,
@@ -4211,6 +4247,10 @@ var Task = class _Task extends SignalEmitter {
4211
4247
  this.signalsToEmitAfter.clear();
4212
4248
  return this;
4213
4249
  }
4250
+ handlesIntent(intent) {
4251
+ this.intents.add(intent);
4252
+ return this;
4253
+ }
4214
4254
  /**
4215
4255
  * Maps over the signals in the `signalsToEmitAfter` set and applies a callback function to each signal.
4216
4256
  *