@cadenza.io/core 3.12.4 → 3.13.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
@@ -819,6 +819,8 @@ declare class Task extends SignalEmitter implements Graph {
819
819
  destroyed: boolean;
820
820
  register: boolean;
821
821
  registered: boolean;
822
+ registeredSignals: Set<string>;
823
+ taskMapRegistration: Set<string>;
822
824
  emitsSignals: Set<string>;
823
825
  signalsToEmitAfter: Set<string>;
824
826
  signalsToEmitOnFail: Set<string>;
@@ -848,6 +850,7 @@ declare class Task extends SignalEmitter implements Graph {
848
850
  * @param {number} [retryDelayFactor=1] - The factor by which the retry delay increases after each attempt.
849
851
  */
850
852
  constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, isSubMeta?: boolean, isHidden?: boolean, getTagCallback?: ThrottleTagGetter | undefined, inputSchema?: SchemaDefinition | undefined, validateInputContext?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputContext?: boolean, retryCount?: number, retryDelay?: number, retryDelayMax?: number, retryDelayFactor?: number);
853
+ clone(traverse?: boolean, includeSignals?: boolean): Task;
851
854
  /**
852
855
  * Retrieves the tag associated with the instance.
853
856
  * Can be overridden by subclasses.
@@ -1799,6 +1802,9 @@ declare class Cadenza {
1799
1802
  * ```
1800
1803
  */
1801
1804
  static emit(event: string, data?: AnyObject): void;
1805
+ static schedule(taskName: string, context: AnyObject, timeoutMs: number, exactDateTime?: Date): void;
1806
+ static throttle(taskName: string, context: AnyObject, intervalMs: number, leading?: boolean, startDateTime?: Date): void;
1807
+ static get(taskName: string): Task | undefined;
1802
1808
  /**
1803
1809
  * Creates and registers a new task with the specified parameters and options.
1804
1810
  * Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.
package/dist/index.d.ts CHANGED
@@ -819,6 +819,8 @@ declare class Task extends SignalEmitter implements Graph {
819
819
  destroyed: boolean;
820
820
  register: boolean;
821
821
  registered: boolean;
822
+ registeredSignals: Set<string>;
823
+ taskMapRegistration: Set<string>;
822
824
  emitsSignals: Set<string>;
823
825
  signalsToEmitAfter: Set<string>;
824
826
  signalsToEmitOnFail: Set<string>;
@@ -848,6 +850,7 @@ declare class Task extends SignalEmitter implements Graph {
848
850
  * @param {number} [retryDelayFactor=1] - The factor by which the retry delay increases after each attempt.
849
851
  */
850
852
  constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, isSubMeta?: boolean, isHidden?: boolean, getTagCallback?: ThrottleTagGetter | undefined, inputSchema?: SchemaDefinition | undefined, validateInputContext?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputContext?: boolean, retryCount?: number, retryDelay?: number, retryDelayMax?: number, retryDelayFactor?: number);
853
+ clone(traverse?: boolean, includeSignals?: boolean): Task;
851
854
  /**
852
855
  * Retrieves the tag associated with the instance.
853
856
  * Can be overridden by subclasses.
@@ -1799,6 +1802,9 @@ declare class Cadenza {
1799
1802
  * ```
1800
1803
  */
1801
1804
  static emit(event: string, data?: AnyObject): void;
1805
+ static schedule(taskName: string, context: AnyObject, timeoutMs: number, exactDateTime?: Date): void;
1806
+ static throttle(taskName: string, context: AnyObject, intervalMs: number, leading?: boolean, startDateTime?: Date): void;
1807
+ static get(taskName: string): Task | undefined;
1802
1808
  /**
1803
1809
  * Creates and registers a new task with the specified parameters and options.
1804
1810
  * Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.
package/dist/index.js CHANGED
@@ -461,7 +461,7 @@ var SignalBroker = class _SignalBroker {
461
461
  };
462
462
 
463
463
  // src/engine/GraphRunner.ts
464
- var import_uuid5 = require("uuid");
464
+ var import_uuid6 = require("uuid");
465
465
 
466
466
  // src/engine/GraphRun.ts
467
467
  var import_uuid2 = require("uuid");
@@ -1316,6 +1316,9 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1316
1316
  };
1317
1317
  }
1318
1318
  this.emit(signal, data);
1319
+ if (!this.task.emitsSignals.has(signal)) {
1320
+ this.task.emitsSignals.add(signal);
1321
+ }
1319
1322
  }
1320
1323
  /**
1321
1324
  * Emits metrics with additional metadata describing the task execution and context.
@@ -1341,6 +1344,9 @@ var GraphNode = class _GraphNode extends SignalEmitter {
1341
1344
  };
1342
1345
  }
1343
1346
  this.emitMetrics(signal, data);
1347
+ if (!this.task.emitsSignals.has(signal)) {
1348
+ this.task.emitsSignals.add(signal);
1349
+ }
1344
1350
  }
1345
1351
  /**
1346
1352
  * Updates the progress of a task and emits metrics with associated metadata.
@@ -1960,6 +1966,9 @@ var GraphRoutine = class extends SignalEmitter {
1960
1966
  }
1961
1967
  };
1962
1968
 
1969
+ // src/graph/definition/Task.ts
1970
+ var import_uuid5 = require("uuid");
1971
+
1963
1972
  // src/graph/iterators/TaskIterator.ts
1964
1973
  var TaskIterator = class {
1965
1974
  constructor(task) {
@@ -1992,7 +2001,7 @@ var TaskIterator = class {
1992
2001
  };
1993
2002
 
1994
2003
  // src/graph/definition/Task.ts
1995
- var Task = class extends SignalEmitter {
2004
+ var Task = class _Task extends SignalEmitter {
1996
2005
  /**
1997
2006
  * Constructs an instance of the task with the specified properties and configuration options.
1998
2007
  *
@@ -2044,12 +2053,14 @@ var Task = class extends SignalEmitter {
2044
2053
  this.destroyed = false;
2045
2054
  this.register = true;
2046
2055
  this.registered = false;
2056
+ this.registeredSignals = /* @__PURE__ */ new Set();
2057
+ this.taskMapRegistration = /* @__PURE__ */ new Set();
2047
2058
  this.emitsSignals = /* @__PURE__ */ new Set();
2048
2059
  this.signalsToEmitAfter = /* @__PURE__ */ new Set();
2049
2060
  this.signalsToEmitOnFail = /* @__PURE__ */ new Set();
2050
2061
  this.observedSignals = /* @__PURE__ */ new Set();
2051
2062
  this.name = name;
2052
- this.taskFunction = task.bind(this);
2063
+ this.taskFunction = task;
2053
2064
  this.description = description;
2054
2065
  this.concurrency = concurrency;
2055
2066
  this.timeout = timeout;
@@ -2103,6 +2114,41 @@ var Task = class extends SignalEmitter {
2103
2114
  });
2104
2115
  }
2105
2116
  }
2117
+ clone(traverse = false, includeSignals = false) {
2118
+ const clonedTask = new _Task(
2119
+ `${this.name} (clone ${(0, import_uuid5.v4)().slice(0, 8)})`,
2120
+ this.taskFunction,
2121
+ this.description,
2122
+ this.concurrency,
2123
+ this.timeout,
2124
+ this.register,
2125
+ this.isUnique,
2126
+ this.isMeta,
2127
+ this.isSubMeta,
2128
+ this.isHidden,
2129
+ this.getTag,
2130
+ this.inputContextSchema,
2131
+ this.validateInputContext,
2132
+ this.outputContextSchema,
2133
+ this.validateOutputContext,
2134
+ this.retryCount,
2135
+ this.retryDelay,
2136
+ this.retryDelayMax,
2137
+ this.retryDelayFactor
2138
+ );
2139
+ if (includeSignals) {
2140
+ clonedTask.doOn(...this.observedSignals);
2141
+ clonedTask.emits(...this.signalsToEmitAfter);
2142
+ clonedTask.emitsOnFail(...this.signalsToEmitOnFail);
2143
+ clonedTask.emitsSignals = new Set(Array.from(this.emitsSignals));
2144
+ }
2145
+ if (traverse) {
2146
+ this.mapNext((t) => {
2147
+ clonedTask.then(t.clone(traverse, includeSignals));
2148
+ });
2149
+ }
2150
+ return clonedTask;
2151
+ }
2106
2152
  /**
2107
2153
  * Retrieves the tag associated with the instance.
2108
2154
  * Can be overridden by subclasses.
@@ -2564,6 +2610,10 @@ var Task = class extends SignalEmitter {
2564
2610
  doOn(...signals) {
2565
2611
  signals.forEach((signal) => {
2566
2612
  if (this.observedSignals.has(signal)) return;
2613
+ if (this.emitsSignals.has(signal))
2614
+ throw new Error(
2615
+ `Detected signal loop for task ${this.name}. Signal name: ${signal}`
2616
+ );
2567
2617
  Cadenza.broker.observe(signal, this);
2568
2618
  this.observedSignals.add(signal);
2569
2619
  if (this.register) {
@@ -2586,6 +2636,10 @@ var Task = class extends SignalEmitter {
2586
2636
  */
2587
2637
  emits(...signals) {
2588
2638
  signals.forEach((signal) => {
2639
+ if (this.observedSignals.has(signal))
2640
+ throw new Error(
2641
+ `Detected signal loop for task ${this.name}. Signal name: ${signal}`
2642
+ );
2589
2643
  this.signalsToEmitAfter.add(signal);
2590
2644
  this.attachSignal(signal);
2591
2645
  });
@@ -3064,9 +3118,9 @@ var GraphRunner = class extends SignalEmitter {
3064
3118
  const isSubMeta = allTasks.some((t) => t.isSubMeta) || !!context.__isSubMeta;
3065
3119
  context.__isSubMeta = isSubMeta;
3066
3120
  const isNewTrace = !context.__routineExecId && !context.__metadata?.__executionTraceId && !context.__executionTraceId;
3067
- const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? (0, import_uuid5.v4)();
3121
+ const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? (0, import_uuid6.v4)();
3068
3122
  context.__executionTraceId = executionTraceId;
3069
- const routineExecId = context.__routineExecId ?? (0, import_uuid5.v4)();
3123
+ const routineExecId = context.__routineExecId ?? (0, import_uuid6.v4)();
3070
3124
  context.__routineExecId = routineExecId;
3071
3125
  const ctx = new GraphContext(context || {});
3072
3126
  if (!isSubMeta) {
@@ -4257,6 +4311,21 @@ var Cadenza = class {
4257
4311
  static emit(event, data = {}) {
4258
4312
  this.broker?.emit(event, data);
4259
4313
  }
4314
+ static schedule(taskName, context, timeoutMs, exactDateTime) {
4315
+ this.broker?.schedule(taskName, context, timeoutMs, exactDateTime);
4316
+ }
4317
+ static throttle(taskName, context, intervalMs, leading = false, startDateTime) {
4318
+ this.broker?.throttle(
4319
+ taskName,
4320
+ context,
4321
+ intervalMs,
4322
+ leading,
4323
+ startDateTime
4324
+ );
4325
+ }
4326
+ static get(taskName) {
4327
+ return this.registry?.tasks.get(taskName);
4328
+ }
4260
4329
  /**
4261
4330
  * Creates and registers a new task with the specified parameters and options.
4262
4331
  * Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.