@cadenza.io/core 3.1.9 → 3.2.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
@@ -164,14 +164,13 @@ var SignalBroker = class _SignalBroker {
164
164
  }
165
165
  }
166
166
  execute(signal, context) {
167
- var _a, _b, _c, _d;
168
167
  const isMeta = signal.includes("meta.");
169
168
  const isSubMeta = signal.includes("sub_meta.") || context.__isSubMeta;
170
- const isMetric = (_a = context.__signalEmission) == null ? void 0 : _a.isMetric;
169
+ const isMetric = context.__signalEmission?.isMetric;
171
170
  if (!isSubMeta && (!isMeta || this.debug)) {
172
171
  const emittedAt = Date.now();
173
172
  context.__signalEmission = {
174
- ...(_b = context.__signalEmission) != null ? _b : {},
173
+ ...context.__signalEmission ?? {},
175
174
  signalName: signal,
176
175
  emittedAt: formatTimestamp(emittedAt),
177
176
  consumed: false,
@@ -186,7 +185,7 @@ var SignalBroker = class _SignalBroker {
186
185
  }
187
186
  if (this.debug && (!isMetric && !isSubMeta || this.verbose)) {
188
187
  console.log(
189
- `EMITTING ${signal} to listeners ${(_d = (_c = this.signalObservers.get(signal)) == null ? void 0 : _c.tasks.size) != null ? _d : 0} with context ${this.verbose ? JSON.stringify(context) : JSON.stringify(context).slice(0, 100)}`
188
+ `EMITTING ${signal} to listeners ${this.signalObservers.get(signal)?.tasks.size ?? 0} with context ${this.verbose ? JSON.stringify(context) : JSON.stringify(context).slice(0, 100)}`
190
189
  );
191
190
  }
192
191
  let executed;
@@ -459,29 +458,26 @@ var GraphRun = class {
459
458
  }
460
459
  // Composite function
461
460
  destroy() {
462
- var _a;
463
- (_a = this.graph) == null ? void 0 : _a.destroy();
461
+ this.graph?.destroy();
464
462
  this.graph = void 0;
465
463
  this.exporter = void 0;
466
464
  }
467
465
  // Composite function
468
466
  log() {
469
- var _a;
470
467
  console.log("vvvvvvvvvvvvvvvvv");
471
468
  console.log("GraphRun");
472
469
  console.log("vvvvvvvvvvvvvvvvv");
473
- (_a = this.graph) == null ? void 0 : _a.log();
470
+ this.graph?.log();
474
471
  console.log("=================");
475
472
  }
476
473
  // Memento
477
474
  export() {
478
- var _a, _b;
479
475
  if (this.exporter && this.graph) {
480
476
  const data = this.strategy.export();
481
477
  return {
482
478
  __id: this.id,
483
- __label: (_a = data.__startTime) != null ? _a : this.id,
484
- __graph: (_b = this.exporter) == null ? void 0 : _b.exportGraph(this.graph),
479
+ __label: data.__startTime ?? this.id,
480
+ __graph: this.exporter?.exportGraph(this.graph),
485
481
  __data: data
486
482
  };
487
483
  }
@@ -661,9 +657,8 @@ function sleep(ms) {
661
657
  // src/graph/execution/GraphNode.ts
662
658
  var GraphNode = class _GraphNode extends SignalEmitter {
663
659
  constructor(task, context, routineExecId, prevNodes = [], debug = false, verbose = false) {
664
- var _a;
665
660
  super(
666
- task.isMeta && !debug || task.isSubMeta || ((_a = context == null ? void 0 : context.getMetadata()) == null ? void 0 : _a.__isSubMeta)
661
+ task.isMeta && !debug || task.isSubMeta || context?.getMetadata()?.__isSubMeta
667
662
  );
668
663
  this.divided = false;
669
664
  this.splitGroupId = "";
@@ -740,7 +735,6 @@ var GraphNode = class _GraphNode extends SignalEmitter {
740
735
  return this.task.getTag(this.context);
741
736
  }
742
737
  scheduleOn(layer) {
743
- var _a, _b, _c;
744
738
  let shouldSchedule = true;
745
739
  const nodes = layer.getNodesByRoutineExecId(this.routineExecId);
746
740
  for (const node of nodes) {
@@ -763,7 +757,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
763
757
  data: {
764
758
  uuid: this.id,
765
759
  routineExecutionId: this.routineExecId,
766
- executionTraceId: (_b = context.__executionTraceId) != null ? _b : (_a = context.__metadata) == null ? void 0 : _a.__executionTraceId,
760
+ executionTraceId: context.__executionTraceId ?? context.__metadata?.__executionTraceId,
767
761
  context: this.previousNodes.length === 0 ? this.context.id : this.context.export(),
768
762
  taskName: this.task.name,
769
763
  taskVersion: this.task.version,
@@ -787,7 +781,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
787
781
  }
788
782
  });
789
783
  });
790
- if (((_c = context.__signalEmission) == null ? void 0 : _c.consumed) === false && (!this.isMeta() || this.debug)) {
784
+ if (context.__signalEmission?.consumed === false && (!this.isMeta() || this.debug)) {
791
785
  this.emitMetricsWithMetadata("meta.node.consumed_signal", {
792
786
  data: {
793
787
  signalName: context.__signalEmission.signalName,
@@ -934,7 +928,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
934
928
  this.emitWithMetadata.bind(this),
935
929
  this.onProgress.bind(this)
936
930
  );
937
- if ((result == null ? void 0 : result.errored) || (result == null ? void 0 : result.failed)) {
931
+ if (result?.errored || result?.failed) {
938
932
  return this.retry(result);
939
933
  }
940
934
  return result;
@@ -950,9 +944,8 @@ var GraphNode = class _GraphNode extends SignalEmitter {
950
944
  }
951
945
  }
952
946
  emitWithMetadata(signal, ctx) {
953
- var _a, _b, _c;
954
947
  const data = { ...ctx };
955
- if (!((_a = this.task) == null ? void 0 : _a.isHidden)) {
948
+ if (!this.task?.isHidden) {
956
949
  data.__signalEmission = {
957
950
  taskName: this.task.name,
958
951
  taskVersion: this.task.version,
@@ -962,15 +955,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
962
955
  data.__metadata = {
963
956
  ...data.__metadata,
964
957
  __routineExecId: this.routineExecId,
965
- __executionTraceId: (_c = (_b = context.__metadata) == null ? void 0 : _b.__executionTraceId) != null ? _c : context.__executionTraceId
958
+ __executionTraceId: context.__metadata?.__executionTraceId ?? context.__executionTraceId
966
959
  };
967
960
  }
968
961
  this.emit(signal, data);
969
962
  }
970
963
  emitMetricsWithMetadata(signal, ctx) {
971
- var _a, _b, _c;
972
964
  const data = { ...ctx };
973
- if (!((_a = this.task) == null ? void 0 : _a.isHidden)) {
965
+ if (!this.task?.isHidden) {
974
966
  data.__signalEmission = {
975
967
  taskName: this.task.name,
976
968
  taskVersion: this.task.version,
@@ -981,13 +973,12 @@ var GraphNode = class _GraphNode extends SignalEmitter {
981
973
  data.__metadata = {
982
974
  ...data.__metadata,
983
975
  __routineExecId: this.routineExecId,
984
- __executionTraceId: (_c = (_b = context.__metadata) == null ? void 0 : _b.__executionTraceId) != null ? _c : context.__executionTraceId
976
+ __executionTraceId: context.__metadata?.__executionTraceId ?? context.__executionTraceId
985
977
  };
986
978
  }
987
979
  this.emitMetrics(signal, data);
988
980
  }
989
981
  onProgress(progress) {
990
- var _a, _b;
991
982
  progress = Math.min(Math.max(0, progress), 1);
992
983
  this.emitMetricsWithMetadata("meta.node.progress", {
993
984
  data: {
@@ -1001,7 +992,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1001
992
  `meta.node.routine_execution_progress:${this.routineExecId}`,
1002
993
  {
1003
994
  data: {
1004
- progress: progress * this.task.progressWeight / ((_b = (_a = this.layer) == null ? void 0 : _a.getIdenticalNodes(this).length) != null ? _b : 1)
995
+ progress: progress * this.task.progressWeight / (this.layer?.getIdenticalNodes(this).length ?? 1)
1005
996
  },
1006
997
  filter: {
1007
998
  uuid: this.routineExecId
@@ -1084,9 +1075,8 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1084
1075
  }
1085
1076
  }
1086
1077
  divide() {
1087
- var _a;
1088
1078
  const newNodes = [];
1089
- if (((_a = this.result) == null ? void 0 : _a.next) && typeof this.result.next === "function") {
1079
+ if (this.result?.next && typeof this.result.next === "function") {
1090
1080
  const generator = this.result;
1091
1081
  let current = generator.next();
1092
1082
  if (current instanceof Promise) {
@@ -1200,11 +1190,10 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1200
1190
  return newNodes;
1201
1191
  }
1202
1192
  differentiate(task) {
1203
- var _a, _b;
1204
1193
  this.task = task;
1205
1194
  this.retryCount = task.retryCount;
1206
1195
  this.retryDelay = task.retryDelay;
1207
- this.silent = task.isMeta && !this.debug || task.isSubMeta || ((_b = (_a = this.context) == null ? void 0 : _a.getMetadata()) == null ? void 0 : _b.__isSubMeta);
1196
+ this.silent = task.isMeta && !this.debug || task.isSubMeta || this.context?.getMetadata()?.__isSubMeta;
1208
1197
  return this;
1209
1198
  }
1210
1199
  migrate(ctx) {
@@ -2333,7 +2322,6 @@ var GraphRunner = class extends SignalEmitter {
2333
2322
  * @edge Empty tasks warns no-op.
2334
2323
  */
2335
2324
  addTasks(tasks, context = {}) {
2336
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2337
2325
  let _tasks = Array.isArray(tasks) ? tasks : [tasks];
2338
2326
  if (_tasks.length === 0) {
2339
2327
  console.warn("No tasks/routines to add.");
@@ -2355,10 +2343,10 @@ var GraphRunner = class extends SignalEmitter {
2355
2343
  });
2356
2344
  const isSubMeta = allTasks.some((t) => t.isSubMeta) || !!context.__isSubMeta;
2357
2345
  context.__isSubMeta = isSubMeta;
2358
- const isNewTrace = !context.__routineExecId && !((_a = context.__metadata) == null ? void 0 : _a.__executionTraceId) && !context.__executionTraceId;
2359
- const executionTraceId = (_d = (_c = (_b = context.__metadata) == null ? void 0 : _b.__executionTraceId) != null ? _c : context.__executionTraceId) != null ? _d : uuid4();
2346
+ const isNewTrace = !context.__routineExecId && !context.__metadata?.__executionTraceId && !context.__executionTraceId;
2347
+ const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? uuid4();
2360
2348
  context.__executionTraceId = executionTraceId;
2361
- const routineExecId = (_e = context.__routineExecId) != null ? _e : uuid4();
2349
+ const routineExecId = context.__routineExecId ?? uuid4();
2362
2350
  context.__routineExecId = routineExecId;
2363
2351
  const ctx = new GraphContext(context || {});
2364
2352
  if (!isSubMeta) {
@@ -2369,9 +2357,9 @@ var GraphRunner = class extends SignalEmitter {
2369
2357
  uuid: executionTraceId,
2370
2358
  issuer_type: "service",
2371
2359
  // TODO: Add issuer type
2372
- issuer_id: (_h = (_g = (_f = context.__metadata) == null ? void 0 : _f.__issuerId) != null ? _g : context.__issuerId) != null ? _h : null,
2360
+ issuer_id: context.__metadata?.__issuerId ?? context.__issuerId ?? null,
2373
2361
  issued_at: formatTimestamp(Date.now()),
2374
- intent: (_k = (_j = (_i = context.__metadata) == null ? void 0 : _i.__intent) != null ? _j : context.__intent) != null ? _k : null,
2362
+ intent: context.__metadata?.__intent ?? context.__intent ?? null,
2375
2363
  context: contextData,
2376
2364
  is_meta: isMeta
2377
2365
  },
@@ -2388,7 +2376,7 @@ var GraphRunner = class extends SignalEmitter {
2388
2376
  isMeta,
2389
2377
  executionTraceId,
2390
2378
  context: isNewTrace ? contextData.id : contextData,
2391
- previousRoutineExecution: (_n = (_m = context.__localRoutineExecId) != null ? _m : (_l = context.__metadata) == null ? void 0 : _l.__routineExecId) != null ? _n : null,
2379
+ previousRoutineExecution: context.__localRoutineExecId ?? context.__metadata?.__routineExecId ?? null,
2392
2380
  // TODO: There is a chance this is not added to the database yet...
2393
2381
  created: formatTimestamp(Date.now())
2394
2382
  },
@@ -2412,7 +2400,7 @@ var GraphRunner = class extends SignalEmitter {
2412
2400
  */
2413
2401
  run(tasks, context) {
2414
2402
  if (tasks) {
2415
- this.addTasks(tasks, context != null ? context : {});
2403
+ this.addTasks(tasks, context ?? {});
2416
2404
  }
2417
2405
  if (this.isRunning) {
2418
2406
  return this.currentRun;
@@ -2456,12 +2444,11 @@ var GraphRunner = class extends SignalEmitter {
2456
2444
  }
2457
2445
  // TODO This should not live here. This is deputy related.
2458
2446
  startRun(context, emit) {
2459
- var _a, _b, _c;
2460
2447
  if (context.__task || context.__routine) {
2461
- const routine = (_a = context.__task) != null ? _a : context.__routine;
2448
+ const routine = context.__task ?? context.__routine;
2462
2449
  delete context.__task;
2463
2450
  delete context.__routine;
2464
- context.__routineExecId = (_c = (_b = context.__metadata) == null ? void 0 : _b.__deputyExecId) != null ? _c : null;
2451
+ context.__routineExecId = context.__metadata?.__deputyExecId ?? null;
2465
2452
  context.__isDeputy = true;
2466
2453
  this.run(routine, context);
2467
2454
  return true;
@@ -2687,22 +2674,20 @@ var GraphLayerIterator = class {
2687
2674
  return this.currentLayer;
2688
2675
  }
2689
2676
  getFirst() {
2690
- var _a;
2691
2677
  if (!this.currentLayer) {
2692
2678
  this.currentLayer = this.graph;
2693
2679
  }
2694
2680
  while (this.hasPrevious()) {
2695
- this.currentLayer = (_a = this.currentLayer) == null ? void 0 : _a.getPreceding();
2681
+ this.currentLayer = this.currentLayer?.getPreceding();
2696
2682
  }
2697
2683
  return this.currentLayer;
2698
2684
  }
2699
2685
  getLast() {
2700
- var _a;
2701
2686
  if (!this.currentLayer) {
2702
2687
  this.currentLayer = this.graph;
2703
2688
  }
2704
2689
  while (this.hasNext()) {
2705
- this.currentLayer = (_a = this.currentLayer) == null ? void 0 : _a.getNext();
2690
+ this.currentLayer = this.currentLayer?.getNext();
2706
2691
  }
2707
2692
  return this.currentLayer;
2708
2693
  }
@@ -2791,7 +2776,7 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
2791
2776
  this.nodes = [];
2792
2777
  if (this.hasNext) {
2793
2778
  const layer = this.getNext();
2794
- layer == null ? void 0 : layer.destroy();
2779
+ layer?.destroy();
2795
2780
  }
2796
2781
  this.decouple();
2797
2782
  }
@@ -2954,8 +2939,7 @@ var GraphRunStrategy = class {
2954
2939
  this.graphBuilder.addNode(node);
2955
2940
  }
2956
2941
  updateRunInstance() {
2957
- var _a;
2958
- (_a = this.runInstance) == null ? void 0 : _a.setGraph(this.graphBuilder.getResult());
2942
+ this.runInstance?.setGraph(this.graphBuilder.getResult());
2959
2943
  }
2960
2944
  };
2961
2945
 
@@ -2980,20 +2964,19 @@ var ThrottleEngine = class _ThrottleEngine {
2980
2964
  this.maxConcurrencyPerTag[tag] = limit;
2981
2965
  }
2982
2966
  throttle(fn, node, tag = "default") {
2983
- var _a, _b, _c, _d;
2967
+ var _a, _b;
2984
2968
  const functionPromise = new Promise((resolve) => {
2985
2969
  this.functionIdToPromiseResolve[node.id] = resolve;
2986
2970
  });
2987
- (_b = (_a = this.queues)[tag]) != null ? _b : _a[tag] = [];
2971
+ (_a = this.queues)[tag] ?? (_a[tag] = []);
2988
2972
  this.queues[tag].push([fn, node]);
2989
- (_d = (_c = this.maxConcurrencyPerTag)[tag]) != null ? _d : _c[tag] = 1;
2973
+ (_b = this.maxConcurrencyPerTag)[tag] ?? (_b[tag] = 1);
2990
2974
  this.processQueue(tag);
2991
2975
  return functionPromise;
2992
2976
  }
2993
2977
  processQueue(tag) {
2994
- var _a, _b, _c, _d, _e;
2995
2978
  const maxAllowed = this.maxConcurrencyPerTag[tag];
2996
- while (((_b = (_a = this.queues[tag]) == null ? void 0 : _a.length) != null ? _b : 0) > 0 && ((_c = this.runningCounts[tag]) != null ? _c : 0) < maxAllowed) {
2979
+ while ((this.queues[tag]?.length ?? 0) > 0 && (this.runningCounts[tag] ?? 0) < maxAllowed) {
2997
2980
  this.runningCounts[tag] = (this.runningCounts[tag] || 0) + 1;
2998
2981
  const item = this.queues[tag].shift();
2999
2982
  this.process(item).then(() => {
@@ -3001,7 +2984,7 @@ var ThrottleEngine = class _ThrottleEngine {
3001
2984
  this.processQueue(tag);
3002
2985
  });
3003
2986
  }
3004
- if (((_e = (_d = this.queues[tag]) == null ? void 0 : _d.length) != null ? _e : 0) === 0 && this.runningCounts[tag] === 0) {
2987
+ if ((this.queues[tag]?.length ?? 0) === 0 && this.runningCounts[tag] === 0) {
3005
2988
  delete this.queues[tag];
3006
2989
  delete this.runningCounts[tag];
3007
2990
  }
@@ -3027,7 +3010,7 @@ var AsyncGraphLayer = class extends GraphLayer {
3027
3010
  this.waitingNodes.push(node);
3028
3011
  }
3029
3012
  execute() {
3030
- var _a, _b;
3013
+ var _a;
3031
3014
  if (this.waitingNodes.length === 0) {
3032
3015
  return {};
3033
3016
  }
@@ -3039,9 +3022,9 @@ var AsyncGraphLayer = class extends GraphLayer {
3039
3022
  break;
3040
3023
  }
3041
3024
  this.processingNodes.add(node);
3042
- (_b = result[_a = node.routineExecId]) != null ? _b : result[_a] = [];
3025
+ result[_a = node.routineExecId] ?? (result[_a] = []);
3043
3026
  let nextNodes;
3044
- if (node == null ? void 0 : node.getConcurrency()) {
3027
+ if (node?.getConcurrency()) {
3045
3028
  const tag = node.getTag();
3046
3029
  ThrottleEngine.instance.setConcurrencyLimit(tag, node.getConcurrency());
3047
3030
  nextNodes = ThrottleEngine.instance.throttle(
@@ -3506,9 +3489,8 @@ var Cadenza = class {
3506
3489
  return new GraphRoutine(name, tasks, description, true);
3507
3490
  }
3508
3491
  static reset() {
3509
- var _a, _b;
3510
- (_a = this.broker) == null ? void 0 : _a.reset();
3511
- (_b = this.registry) == null ? void 0 : _b.reset();
3492
+ this.broker?.reset();
3493
+ this.registry?.reset();
3512
3494
  this.isBootstrapped = false;
3513
3495
  }
3514
3496
  };