@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.mjs CHANGED
@@ -1225,6 +1225,11 @@ var merge = createAssigner_default(function(object, source, srcIndex) {
1225
1225
  });
1226
1226
  var merge_default = merge;
1227
1227
 
1228
+ // src/utils/promise.ts
1229
+ function sleep(ms) {
1230
+ return new Promise((resolve) => setTimeout(resolve, ms));
1231
+ }
1232
+
1228
1233
  // src/engine/SignalBroker.ts
1229
1234
  var SignalBroker = class _SignalBroker {
1230
1235
  constructor() {
@@ -1233,6 +1238,8 @@ var SignalBroker = class _SignalBroker {
1233
1238
  this.debouncedEmitters = /* @__PURE__ */ new Map();
1234
1239
  this.squashedEmitters = /* @__PURE__ */ new Map();
1235
1240
  this.squashedContexts = /* @__PURE__ */ new Map();
1241
+ this.throttleEmitters = /* @__PURE__ */ new Map();
1242
+ this.throttleQueues = /* @__PURE__ */ new Map();
1236
1243
  // TODO: Signals should be a class with a the observers, registered flag and other data.
1237
1244
  this.signalObservers = /* @__PURE__ */ new Map();
1238
1245
  this.emittedSignalsRegistry = /* @__PURE__ */ new Set();
@@ -1433,6 +1440,35 @@ var SignalBroker = class _SignalBroker {
1433
1440
  }
1434
1441
  debouncedEmitter(context);
1435
1442
  }
1443
+ throttle(signal, context, options = { delayMs: 1e3 }) {
1444
+ let { groupId, delayMs = 300 } = options;
1445
+ if (!groupId) {
1446
+ groupId = signal;
1447
+ }
1448
+ if (!this.throttleQueues.has(groupId)) {
1449
+ this.throttleQueues.set(groupId, []);
1450
+ }
1451
+ const queue = this.throttleQueues.get(groupId);
1452
+ queue.push([signal, context]);
1453
+ if (!this.throttleEmitters.has(groupId)) {
1454
+ this.throttleEmitters.set(groupId, async () => {
1455
+ while (queue.length > 0) {
1456
+ let batchSize = options.throttleBatch ?? 1;
1457
+ if (batchSize > queue.length) {
1458
+ batchSize = queue.length;
1459
+ }
1460
+ for (let i = 0; i < batchSize; i++) {
1461
+ const [nextSignal, nextContext] = queue.shift();
1462
+ this.emit(nextSignal, nextContext);
1463
+ }
1464
+ await sleep(delayMs);
1465
+ }
1466
+ this.throttleEmitters.delete(groupId);
1467
+ this.throttleQueues.delete(groupId);
1468
+ });
1469
+ this.throttleEmitters.get(groupId)();
1470
+ }
1471
+ }
1436
1472
  /**
1437
1473
  * Aggregates and debounces multiple events with the same identifier to minimize redundant operations.
1438
1474
  *
@@ -1445,7 +1481,7 @@ var SignalBroker = class _SignalBroker {
1445
1481
  * @return {void} Does not return a value.
1446
1482
  */
1447
1483
  squash(signal, context, options = { squash: true }) {
1448
- let { squashId, delayMs = 500 } = options;
1484
+ let { squashId, delayMs = 300 } = options;
1449
1485
  if (!squashId) {
1450
1486
  squashId = signal;
1451
1487
  }
@@ -1453,10 +1489,11 @@ var SignalBroker = class _SignalBroker {
1453
1489
  this.squashedEmitters.set(
1454
1490
  squashId,
1455
1491
  debounce_default(() => {
1456
- this.emit(signal, this.squashedContexts.get(squashId));
1492
+ options.squash = false;
1493
+ this.emit(signal, this.squashedContexts.get(squashId), options);
1457
1494
  this.squashedEmitters.delete(squashId);
1458
1495
  this.squashedContexts.delete(squashId);
1459
- }, delayMs ?? 500)
1496
+ }, delayMs ?? 300)
1460
1497
  );
1461
1498
  this.squashedContexts.set(squashId, context);
1462
1499
  } else {
@@ -2041,7 +2078,7 @@ var SignalEmitter = class {
2041
2078
  * @param options
2042
2079
  */
2043
2080
  emit(signal, data = {}, options = {}) {
2044
- Cadenza.broker.emit(signal, data);
2081
+ Cadenza.broker.emit(signal, data, options);
2045
2082
  }
2046
2083
  /**
2047
2084
  * Emits a signal via the broker if not silent.
@@ -2053,15 +2090,10 @@ var SignalEmitter = class {
2053
2090
  if (this.silent) {
2054
2091
  return;
2055
2092
  }
2056
- Cadenza.broker.emit(signal, data);
2093
+ Cadenza.broker.emit(signal, data, options);
2057
2094
  }
2058
2095
  };
2059
2096
 
2060
- // src/utils/promise.ts
2061
- function sleep(ms) {
2062
- return new Promise((resolve) => setTimeout(resolve, ms));
2063
- }
2064
-
2065
2097
  // src/graph/execution/GraphNode.ts
2066
2098
  var GraphNode = class _GraphNode extends SignalEmitter {
2067
2099
  constructor(task, context, routineExecId, prevNodes = [], debug = false, verbose = false) {
@@ -2305,7 +2337,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2305
2337
  },
2306
2338
  filter: { uuid: this.routineExecId }
2307
2339
  },
2308
- { squash: true, squashId: this.routineExecId }
2340
+ { squash: true, squashId: this.routineExecId, delayMs: 200 }
2309
2341
  );
2310
2342
  }
2311
2343
  if (this.debug && !this.task.isSubMeta && !this.context.getMetadata().__isSubMeta || this.verbose) {
@@ -2366,7 +2398,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2366
2398
  },
2367
2399
  filter: { uuid: this.id }
2368
2400
  },
2369
- { squash: true, squashId: this.id }
2401
+ { squash: true, squashId: this.id, delayMs: 0 }
2370
2402
  );
2371
2403
  if (this.graphDone()) {
2372
2404
  const context2 = this.context.getFullContext();
@@ -2388,7 +2420,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
2388
2420
  },
2389
2421
  filter: { uuid: this.routineExecId }
2390
2422
  },
2391
- { squash: true, squashId: this.routineExecId }
2423
+ { squash: true, squashId: this.routineExecId, delayMs: 0 }
2392
2424
  );
2393
2425
  }
2394
2426
  return end;
@@ -3269,7 +3301,7 @@ var GraphRunner = class extends SignalEmitter {
3269
3301
  __executionTraceId: executionTraceId
3270
3302
  }
3271
3303
  },
3272
- { squash: true, squashId: routineExecId }
3304
+ { squash: true, squashId: routineExecId, delayMs: 200 }
3273
3305
  );
3274
3306
  }
3275
3307
  allTasks.forEach(
@@ -3444,6 +3476,7 @@ var Task = class _Task extends SignalEmitter {
3444
3476
  this.signalsToEmitAfter = /* @__PURE__ */ new Set();
3445
3477
  this.signalsToEmitOnFail = /* @__PURE__ */ new Set();
3446
3478
  this.observedSignals = /* @__PURE__ */ new Set();
3479
+ this.intents = /* @__PURE__ */ new Set();
3447
3480
  this.name = name;
3448
3481
  this.taskFunction = task;
3449
3482
  this.description = description;
@@ -3520,6 +3553,9 @@ var Task = class _Task extends SignalEmitter {
3520
3553
  signalsToEmitAfter: Array.from(this.signalsToEmitAfter),
3521
3554
  signalsToEmitOnFail: Array.from(this.signalsToEmitOnFail),
3522
3555
  observed: Array.from(this.observedSignals)
3556
+ },
3557
+ intents: {
3558
+ handles: Array.from(this.intents)
3523
3559
  }
3524
3560
  },
3525
3561
  taskInstance: this,
@@ -4174,6 +4210,10 @@ var Task = class _Task extends SignalEmitter {
4174
4210
  this.signalsToEmitAfter.clear();
4175
4211
  return this;
4176
4212
  }
4213
+ handlesIntent(intent) {
4214
+ this.intents.add(intent);
4215
+ return this;
4216
+ }
4177
4217
  /**
4178
4218
  * Maps over the signals in the `signalsToEmitAfter` set and applies a callback function to each signal.
4179
4219
  *