@cadenza.io/core 3.10.0 → 3.12.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 +1354 -184
- package/dist/index.d.ts +1354 -184
- package/dist/index.js +1348 -206
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1348 -206
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -78,6 +78,7 @@ function formatTimestamp(timestamp) {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
// src/engine/SignalBroker.ts
|
|
81
|
+
var import_uuid = require("uuid");
|
|
81
82
|
var SignalBroker = class _SignalBroker {
|
|
82
83
|
// execId -> emitted signals
|
|
83
84
|
constructor() {
|
|
@@ -88,10 +89,6 @@ var SignalBroker = class _SignalBroker {
|
|
|
88
89
|
this.emitStacks = /* @__PURE__ */ new Map();
|
|
89
90
|
this.addSignal("meta.signal_broker.added");
|
|
90
91
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Singleton instance for signal management.
|
|
93
|
-
* @returns The broker instance.
|
|
94
|
-
*/
|
|
95
92
|
static get instance() {
|
|
96
93
|
if (!this.instance_) {
|
|
97
94
|
this.instance_ = new _SignalBroker();
|
|
@@ -104,6 +101,15 @@ var SignalBroker = class _SignalBroker {
|
|
|
104
101
|
setVerbose(value) {
|
|
105
102
|
this.verbose = value;
|
|
106
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Validates the provided signal name string to ensure it adheres to specific formatting rules.
|
|
106
|
+
* Throws an error if any of the validation checks fail.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} signalName - The signal name to be validated.
|
|
109
|
+
* @return {void} - Returns nothing if the signal name is valid.
|
|
110
|
+
* @throws {Error} - Throws an error if the signal name is longer than 100 characters, contains spaces,
|
|
111
|
+
* contains backslashes, or contains uppercase letters in restricted parts of the name.
|
|
112
|
+
*/
|
|
107
113
|
validateSignalName(signalName) {
|
|
108
114
|
if (signalName.length > 100) {
|
|
109
115
|
throw new Error(
|
|
@@ -133,6 +139,11 @@ var SignalBroker = class _SignalBroker {
|
|
|
133
139
|
this.runner = runner;
|
|
134
140
|
this.metaRunner = metaRunner;
|
|
135
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Initializes and sets up the various tasks for managing and processing signals.
|
|
144
|
+
*
|
|
145
|
+
* @return {void} This method does not return a value.
|
|
146
|
+
*/
|
|
136
147
|
init() {
|
|
137
148
|
this.clearSignalsTask = Cadenza.createDebounceMetaTask(
|
|
138
149
|
"Execute and clear queued signals",
|
|
@@ -196,6 +207,15 @@ var SignalBroker = class _SignalBroker {
|
|
|
196
207
|
}
|
|
197
208
|
}
|
|
198
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Schedules a signal to be emitted after a specified delay or at an exact date and time.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} signal - The name of the signal to be emitted.
|
|
214
|
+
* @param {AnyObject} context - The context to be passed along with the signal.
|
|
215
|
+
* @param {number} [timeoutMs=60000] - The delay in milliseconds before the signal is emitted. Defaults to 60,000 ms.
|
|
216
|
+
* @param {Date} [exactDateTime] - An exact date and time at which to emit the signal. If provided, this overrides the `timeoutMs`.
|
|
217
|
+
* @return {AbortController} An AbortController instance that can be used to cancel the scheduled signal emission.
|
|
218
|
+
*/
|
|
199
219
|
schedule(signal, context, timeoutMs = 6e4, exactDateTime) {
|
|
200
220
|
let delay = timeoutMs;
|
|
201
221
|
if (exactDateTime != null) {
|
|
@@ -258,11 +278,12 @@ var SignalBroker = class _SignalBroker {
|
|
|
258
278
|
};
|
|
259
279
|
}
|
|
260
280
|
/**
|
|
261
|
-
* Emits a signal
|
|
262
|
-
*
|
|
263
|
-
* @param
|
|
264
|
-
* @
|
|
265
|
-
*
|
|
281
|
+
* Emits a signal with the specified context, triggering any associated handlers for that signal.
|
|
282
|
+
*
|
|
283
|
+
* @param {string} signal - The name of the signal to emit.
|
|
284
|
+
* @param {AnyObject} [context={}] - An optional context object containing additional information or metadata
|
|
285
|
+
* associated with the signal. If the context includes a `__routineExecId`, it will be handled accordingly.
|
|
286
|
+
* @return {void} This method does not return a value.
|
|
266
287
|
*/
|
|
267
288
|
emit(signal, context = {}) {
|
|
268
289
|
const execId = context.__routineExecId || "global";
|
|
@@ -279,20 +300,62 @@ var SignalBroker = class _SignalBroker {
|
|
|
279
300
|
if (stack.size === 0) this.emitStacks.delete(execId);
|
|
280
301
|
}
|
|
281
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Executes a signal by emitting events, updating context, and invoking listeners.
|
|
305
|
+
* Creates a new execution trace if necessary and updates the context with relevant metadata.
|
|
306
|
+
* Handles specific, hierarchy-based, and wildcard signals.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} signal - The signal name to be executed, potentially including namespaces or tags (e.g., "meta.*" or "signal:type").
|
|
309
|
+
* @param {AnyObject} context - An object containing relevant metadata and execution details used for handling the signal.
|
|
310
|
+
* @return {boolean} Returns true if any listeners were successfully executed, otherwise false.
|
|
311
|
+
*/
|
|
282
312
|
execute(signal, context) {
|
|
283
313
|
const isMeta = signal.includes("meta.");
|
|
284
314
|
const isSubMeta = signal.includes("sub_meta.") || context.__isSubMeta;
|
|
285
315
|
const isMetric = context.__signalEmission?.isMetric;
|
|
286
316
|
if (!isSubMeta && (!isMeta || this.debug)) {
|
|
317
|
+
const isNewTrace = !context.__signalEmission?.executionTraceId && !context.__metadata?.__executionTraceId && !context.__executionTraceId;
|
|
318
|
+
const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? (0, import_uuid.v4)();
|
|
319
|
+
if (isNewTrace) {
|
|
320
|
+
this.emit("sub_meta.signal_broker.new_trace", {
|
|
321
|
+
data: {
|
|
322
|
+
uuid: executionTraceId,
|
|
323
|
+
issuer_type: "service",
|
|
324
|
+
// TODO: Add issuer type
|
|
325
|
+
issuer_id: context.__metadata?.__issuerId ?? context.__issuerId ?? null,
|
|
326
|
+
issued_at: formatTimestamp(Date.now()),
|
|
327
|
+
intent: context.__metadata?.__intent ?? context.__intent ?? null,
|
|
328
|
+
context: {
|
|
329
|
+
id: (0, import_uuid.v4)(),
|
|
330
|
+
context
|
|
331
|
+
},
|
|
332
|
+
is_meta: isMeta
|
|
333
|
+
},
|
|
334
|
+
metadata: {
|
|
335
|
+
__executionTraceId: executionTraceId
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
context.__metadata = {
|
|
339
|
+
...context.__metadata,
|
|
340
|
+
__executionTraceId: executionTraceId
|
|
341
|
+
};
|
|
342
|
+
}
|
|
287
343
|
const emittedAt = Date.now();
|
|
344
|
+
const signalParts = signal.split(":");
|
|
345
|
+
const signalName = signalParts[0];
|
|
346
|
+
const signalTag = signalParts.length > 1 ? signalParts[1] : null;
|
|
288
347
|
context.__signalEmission = {
|
|
289
348
|
...context.__signalEmission ?? {},
|
|
290
|
-
|
|
349
|
+
uuid: (0, import_uuid.v4)(),
|
|
350
|
+
executionTraceId,
|
|
351
|
+
signalName,
|
|
352
|
+
signalTag,
|
|
291
353
|
emittedAt: formatTimestamp(emittedAt),
|
|
292
354
|
consumed: false,
|
|
293
355
|
consumedBy: null,
|
|
294
356
|
isMeta
|
|
295
357
|
};
|
|
358
|
+
this.emit("sub_meta.signal_broker.emitting_signal", context);
|
|
296
359
|
} else if (isSubMeta) {
|
|
297
360
|
context.__isSubMeta = true;
|
|
298
361
|
delete context.__signalEmission;
|
|
@@ -315,6 +378,15 @@ var SignalBroker = class _SignalBroker {
|
|
|
315
378
|
}
|
|
316
379
|
return executed;
|
|
317
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Executes the tasks associated with a given signal and context.
|
|
383
|
+
* It processes both normal and meta tasks depending on the signal type
|
|
384
|
+
* and the availability of the appropriate runner.
|
|
385
|
+
*
|
|
386
|
+
* @param {string} signal - The signal identifier that determines which tasks to execute.
|
|
387
|
+
* @param {AnyObject} context - The context object passed to the task execution function.
|
|
388
|
+
* @return {boolean} - Returns true if tasks were executed; otherwise, false.
|
|
389
|
+
*/
|
|
318
390
|
executeListener(signal, context) {
|
|
319
391
|
const obs = this.signalObservers.get(signal);
|
|
320
392
|
if (!obs || obs.tasks.size === 0) {
|
|
@@ -340,6 +412,15 @@ var SignalBroker = class _SignalBroker {
|
|
|
340
412
|
}
|
|
341
413
|
return false;
|
|
342
414
|
}
|
|
415
|
+
/**
|
|
416
|
+
* Adds a signal to the signalObservers for tracking and execution.
|
|
417
|
+
* Performs validation on the signal name and emits a meta signal event when added.
|
|
418
|
+
* If the signal contains a namespace (denoted by a colon ":"), its base signal is
|
|
419
|
+
* also added if it doesn't already exist.
|
|
420
|
+
*
|
|
421
|
+
* @param {string} signal - The name of the signal to be added.
|
|
422
|
+
* @return {void} This method does not return any value.
|
|
423
|
+
*/
|
|
343
424
|
addSignal(signal) {
|
|
344
425
|
let _signal = signal;
|
|
345
426
|
if (!this.signalObservers.has(_signal)) {
|
|
@@ -365,7 +446,6 @@ var SignalBroker = class _SignalBroker {
|
|
|
365
446
|
this.emit("meta.signal_broker.added", { __signalName: _signal });
|
|
366
447
|
}
|
|
367
448
|
}
|
|
368
|
-
// TODO schedule signals
|
|
369
449
|
/**
|
|
370
450
|
* Lists all observed signals.
|
|
371
451
|
* @returns Array of signals.
|
|
@@ -380,10 +460,10 @@ var SignalBroker = class _SignalBroker {
|
|
|
380
460
|
};
|
|
381
461
|
|
|
382
462
|
// src/engine/GraphRunner.ts
|
|
383
|
-
var
|
|
463
|
+
var import_uuid5 = require("uuid");
|
|
384
464
|
|
|
385
465
|
// src/engine/GraphRun.ts
|
|
386
|
-
var
|
|
466
|
+
var import_uuid2 = require("uuid");
|
|
387
467
|
|
|
388
468
|
// src/utils/ColorRandomizer.ts
|
|
389
469
|
var ColorRandomizer = class {
|
|
@@ -574,7 +654,7 @@ var VueFlowExporter = class {
|
|
|
574
654
|
// src/engine/GraphRun.ts
|
|
575
655
|
var GraphRun = class {
|
|
576
656
|
constructor(strategy) {
|
|
577
|
-
this.id = (0,
|
|
657
|
+
this.id = (0, import_uuid2.v4)();
|
|
578
658
|
this.strategy = strategy;
|
|
579
659
|
this.strategy.setRunInstance(this);
|
|
580
660
|
this.exporter = new VueFlowExporter();
|
|
@@ -628,10 +708,10 @@ var GraphRun = class {
|
|
|
628
708
|
};
|
|
629
709
|
|
|
630
710
|
// src/graph/execution/GraphNode.ts
|
|
631
|
-
var
|
|
711
|
+
var import_uuid4 = require("uuid");
|
|
632
712
|
|
|
633
713
|
// src/graph/context/GraphContext.ts
|
|
634
|
-
var
|
|
714
|
+
var import_uuid3 = require("uuid");
|
|
635
715
|
var GraphContext = class _GraphContext {
|
|
636
716
|
// __keys, frozen
|
|
637
717
|
constructor(context) {
|
|
@@ -645,7 +725,7 @@ var GraphContext = class _GraphContext {
|
|
|
645
725
|
this.metadata = Object.fromEntries(
|
|
646
726
|
Object.entries(this.fullContext).filter(([key]) => key.startsWith("__"))
|
|
647
727
|
);
|
|
648
|
-
this.id = (0,
|
|
728
|
+
this.id = (0, import_uuid3.v4)();
|
|
649
729
|
}
|
|
650
730
|
/**
|
|
651
731
|
* Gets frozen user data (read-only, no clone).
|
|
@@ -654,6 +734,11 @@ var GraphContext = class _GraphContext {
|
|
|
654
734
|
getContext() {
|
|
655
735
|
return this.userData;
|
|
656
736
|
}
|
|
737
|
+
/**
|
|
738
|
+
* Clones the current user context data and returns a deep-cloned copy of it.
|
|
739
|
+
*
|
|
740
|
+
* @return {AnyObject} A deep-cloned copy of the user context data.
|
|
741
|
+
*/
|
|
657
742
|
getClonedContext() {
|
|
658
743
|
return deepCloneFilter(this.userData);
|
|
659
744
|
}
|
|
@@ -664,6 +749,11 @@ var GraphContext = class _GraphContext {
|
|
|
664
749
|
getFullContext() {
|
|
665
750
|
return this.fullContext;
|
|
666
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* Creates and returns a deep-cloned version of the fullContext object.
|
|
754
|
+
*
|
|
755
|
+
* @return {AnyObject} A deep copy of the fullContext instance, preserving all nested structures and data.
|
|
756
|
+
*/
|
|
667
757
|
getClonedFullContext() {
|
|
668
758
|
return deepCloneFilter(this.fullContext);
|
|
669
759
|
}
|
|
@@ -690,10 +780,11 @@ var GraphContext = class _GraphContext {
|
|
|
690
780
|
return new _GraphContext(context);
|
|
691
781
|
}
|
|
692
782
|
/**
|
|
693
|
-
* Combines with another
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
* @
|
|
783
|
+
* Combines the current GraphContext with another GraphContext, merging their user data
|
|
784
|
+
* and full context into a new GraphContext instance.
|
|
785
|
+
*
|
|
786
|
+
* @param {GraphContext} otherContext - The other GraphContext to combine with the current one.
|
|
787
|
+
* @return {GraphContext} A new GraphContext instance containing merged data from both contexts.
|
|
697
788
|
*/
|
|
698
789
|
combine(otherContext) {
|
|
699
790
|
const newUser = { ...this.userData };
|
|
@@ -811,7 +902,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
811
902
|
this.destroyed = false;
|
|
812
903
|
this.debug = false;
|
|
813
904
|
this.verbose = false;
|
|
814
|
-
this.id = (0,
|
|
905
|
+
this.id = (0, import_uuid4.v4)();
|
|
815
906
|
this.task = task;
|
|
816
907
|
this.context = context;
|
|
817
908
|
this.retryCount = task.retryCount;
|
|
@@ -821,6 +912,8 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
821
912
|
this.splitGroupId = routineExecId;
|
|
822
913
|
this.debug = debug;
|
|
823
914
|
this.verbose = verbose;
|
|
915
|
+
const ctx = context.getMetadata();
|
|
916
|
+
this.executionTraceId = ctx.__executionTraceId ?? ctx.__metadata?.__executionTraceId;
|
|
824
917
|
if (!this.task || !this.task.validateInput) {
|
|
825
918
|
console.log("task not found", this.task, this.context);
|
|
826
919
|
}
|
|
@@ -846,15 +939,40 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
846
939
|
graphDone() {
|
|
847
940
|
return this.graphComplete;
|
|
848
941
|
}
|
|
942
|
+
/**
|
|
943
|
+
* Compares the current GraphNode instance with another GraphNode to determine if they are considered equal.
|
|
944
|
+
*
|
|
945
|
+
* @param {GraphNode} node - The GraphNode object to compare with the current instance.
|
|
946
|
+
* @return {boolean} Returns true if the nodes share the same task, context, and belong to the same graph; otherwise, false.
|
|
947
|
+
*/
|
|
849
948
|
isEqualTo(node) {
|
|
850
949
|
return this.sharesTaskWith(node) && this.sharesContextWith(node) && this.isPartOfSameGraph(node);
|
|
851
950
|
}
|
|
951
|
+
/**
|
|
952
|
+
* Determines if the given node is part of the same graph as the current node.
|
|
953
|
+
*
|
|
954
|
+
* @param {GraphNode} node - The node to compare with the current node.
|
|
955
|
+
* @return {boolean} Returns true if the provided node is part of the same graph
|
|
956
|
+
* (i.e., has the same routineExecId), otherwise false.
|
|
957
|
+
*/
|
|
852
958
|
isPartOfSameGraph(node) {
|
|
853
959
|
return this.routineExecId === node.routineExecId;
|
|
854
960
|
}
|
|
961
|
+
/**
|
|
962
|
+
* Determines whether the current instance shares a task with the provided node.
|
|
963
|
+
*
|
|
964
|
+
* @param {GraphNode} node - The graph node to compare with the current instance.
|
|
965
|
+
* @return {boolean} Returns true if the task names of both nodes match, otherwise false.
|
|
966
|
+
*/
|
|
855
967
|
sharesTaskWith(node) {
|
|
856
968
|
return this.task.name === node.task.name;
|
|
857
969
|
}
|
|
970
|
+
/**
|
|
971
|
+
* Determines whether the current node shares the same context as the specified node.
|
|
972
|
+
*
|
|
973
|
+
* @param {GraphNode} node - The graph node to compare with the current node's context.
|
|
974
|
+
* @return {boolean} True if both nodes share the same context; otherwise, false.
|
|
975
|
+
*/
|
|
858
976
|
sharesContextWith(node) {
|
|
859
977
|
return this.context.id === node.context.id;
|
|
860
978
|
}
|
|
@@ -864,9 +982,26 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
864
982
|
getConcurrency() {
|
|
865
983
|
return this.task.concurrency;
|
|
866
984
|
}
|
|
985
|
+
/**
|
|
986
|
+
* Retrieves the tag associated with the current task and context.
|
|
987
|
+
*
|
|
988
|
+
* @return {string} The tag retrieved from the task within the given context.
|
|
989
|
+
*/
|
|
867
990
|
getTag() {
|
|
868
991
|
return this.task.getTag(this.context);
|
|
869
992
|
}
|
|
993
|
+
/**
|
|
994
|
+
* Schedules the current node/task on the specified graph layer if applicable.
|
|
995
|
+
*
|
|
996
|
+
* This method assesses whether the current node/task should be scheduled
|
|
997
|
+
* on the given graph layer. It ensures that tasks are only scheduled
|
|
998
|
+
* under certain conditions, such as checking if the task shares
|
|
999
|
+
* execution contexts or dependencies with other nodes, and handles
|
|
1000
|
+
* various metadata emissions and context updates during the scheduling process.
|
|
1001
|
+
*
|
|
1002
|
+
* @param {GraphLayer} layer - The graph layer on which the current task should be scheduled.
|
|
1003
|
+
* @returns {void} Does not return a value.
|
|
1004
|
+
*/
|
|
870
1005
|
scheduleOn(layer) {
|
|
871
1006
|
let shouldSchedule = true;
|
|
872
1007
|
const nodes = layer.getNodesByRoutineExecId(this.routineExecId);
|
|
@@ -890,7 +1025,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
890
1025
|
data: {
|
|
891
1026
|
uuid: this.id,
|
|
892
1027
|
routineExecutionId: this.routineExecId,
|
|
893
|
-
executionTraceId:
|
|
1028
|
+
executionTraceId: this.executionTraceId,
|
|
894
1029
|
context: this.previousNodes.length === 0 ? this.context.id : this.context.export(),
|
|
895
1030
|
taskName: this.task.name,
|
|
896
1031
|
taskVersion: this.task.version,
|
|
@@ -934,10 +1069,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
934
1069
|
if (context.__signalEmission?.consumed === false && (!this.isMeta() || this.debug)) {
|
|
935
1070
|
this.emitMetricsWithMetadata("meta.node.consumed_signal", {
|
|
936
1071
|
data: {
|
|
1072
|
+
signalEmissionId: context.__signalEmission.uuid,
|
|
937
1073
|
signalName: context.__signalEmission.signalName,
|
|
1074
|
+
signalTag: context.__signalEmission.signalTag,
|
|
938
1075
|
taskName: this.task.name,
|
|
939
1076
|
taskVersion: this.task.version,
|
|
940
1077
|
taskExecutionId: this.id,
|
|
1078
|
+
routineExecutionId: this.routineExecId,
|
|
1079
|
+
executionTraceId: this.executionTraceId,
|
|
941
1080
|
consumedAt: formatTimestamp(scheduledAt)
|
|
942
1081
|
}
|
|
943
1082
|
});
|
|
@@ -946,6 +1085,18 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
946
1085
|
}
|
|
947
1086
|
}
|
|
948
1087
|
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Starts the execution process by initializing the execution start timestamp,
|
|
1090
|
+
* emitting relevant metadata, and logging debug information if applicable.
|
|
1091
|
+
*
|
|
1092
|
+
* The method performs the following actions:
|
|
1093
|
+
* 1. Sets the execution start timestamp if it's not already initialized.
|
|
1094
|
+
* 2. Emits metrics with metadata about the routine execution starting, including additional data if there are no previous nodes.
|
|
1095
|
+
* 3. Optionally logs debug or verbose information based on the current settings.
|
|
1096
|
+
* 4. Emits additional metrics to indicate that the execution has started.
|
|
1097
|
+
*
|
|
1098
|
+
* @return {number} The timestamp indicating when the execution started.
|
|
1099
|
+
*/
|
|
949
1100
|
start() {
|
|
950
1101
|
if (this.executionStart === 0) {
|
|
951
1102
|
this.executionStart = Date.now();
|
|
@@ -971,6 +1122,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
971
1122
|
});
|
|
972
1123
|
return this.executionStart;
|
|
973
1124
|
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Marks the end of an execution process, performs necessary cleanup, emits
|
|
1127
|
+
* metrics with associated metadata, and signals the completion of execution.
|
|
1128
|
+
* Also handles specific cases when the graph completes.
|
|
1129
|
+
*
|
|
1130
|
+
* @return {number} The timestamp corresponding to the end of execution. If execution
|
|
1131
|
+
* was not started, it returns 0.
|
|
1132
|
+
*/
|
|
974
1133
|
end() {
|
|
975
1134
|
if (this.executionStart === 0) {
|
|
976
1135
|
return 0;
|
|
@@ -1023,6 +1182,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1023
1182
|
}
|
|
1024
1183
|
return end;
|
|
1025
1184
|
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Executes the main logic of the task, including input validation, processing, and post-processing.
|
|
1187
|
+
* Handles both synchronous and asynchronous workflows.
|
|
1188
|
+
*
|
|
1189
|
+
* @return {Array|Promise|undefined} Returns the next nodes to process if available.
|
|
1190
|
+
* If asynchronous processing is required, it returns a Promise that resolves to the next nodes.
|
|
1191
|
+
* Returns undefined in case of an error during input validation or preconditions that prevent processing.
|
|
1192
|
+
*/
|
|
1026
1193
|
execute() {
|
|
1027
1194
|
if (!this.divided && !this.processing) {
|
|
1028
1195
|
this.processing = true;
|
|
@@ -1046,6 +1213,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1046
1213
|
}
|
|
1047
1214
|
return this.nextNodes;
|
|
1048
1215
|
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Executes an asynchronous workflow that processes a result and retries on errors.
|
|
1218
|
+
* The method handles different result states, checks for error properties, and invokes
|
|
1219
|
+
* error handling when necessary.
|
|
1220
|
+
*
|
|
1221
|
+
* @return {Promise<void>} A promise that resolves when the operation completes successfully,
|
|
1222
|
+
* or rejects if an unhandled error occurs.
|
|
1223
|
+
*/
|
|
1049
1224
|
async workAsync() {
|
|
1050
1225
|
try {
|
|
1051
1226
|
this.result = await this.result;
|
|
@@ -1062,6 +1237,12 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1062
1237
|
}
|
|
1063
1238
|
}
|
|
1064
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Executes an asynchronous operation, processes the result, and determines the next nodes to execute.
|
|
1242
|
+
* This method will manage asynchronous work, handle post-processing of results, and ensure proper handling of both synchronous and asynchronous next node configurations.
|
|
1243
|
+
*
|
|
1244
|
+
* @return {Promise<any>} A promise resolving to the next nodes to be executed. Can be the result of post-processing or a directly resolved next nodes object.
|
|
1245
|
+
*/
|
|
1065
1246
|
async executeAsync() {
|
|
1066
1247
|
await this.workAsync();
|
|
1067
1248
|
const nextNodes = this.postProcess();
|
|
@@ -1071,6 +1252,16 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1071
1252
|
this.nextNodes = nextNodes;
|
|
1072
1253
|
return this.nextNodes;
|
|
1073
1254
|
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Executes the task associated with the current instance, using the given context,
|
|
1257
|
+
* progress callback, and metadata. If the task fails or an error occurs, it attempts
|
|
1258
|
+
* to retry the execution. If the retry is not successful, it propagates the error and
|
|
1259
|
+
* returns the result.
|
|
1260
|
+
*
|
|
1261
|
+
* @return {TaskResult | Promise<TaskResult>} The result of the task execution, or a
|
|
1262
|
+
* promise that resolves to the task result. This includes handling for retries on
|
|
1263
|
+
* failure and error propagation.
|
|
1264
|
+
*/
|
|
1074
1265
|
work() {
|
|
1075
1266
|
try {
|
|
1076
1267
|
const result = this.task.execute(
|
|
@@ -1094,39 +1285,67 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1094
1285
|
});
|
|
1095
1286
|
}
|
|
1096
1287
|
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Emits a signal along with its associated metadata. The metadata includes
|
|
1290
|
+
* task-specific information such as task name, version, execution ID, and
|
|
1291
|
+
* additional context metadata like routine execution ID and execution trace ID.
|
|
1292
|
+
* This method is designed to enrich emitted signals with relevant details
|
|
1293
|
+
* before broadcasting them.
|
|
1294
|
+
*
|
|
1295
|
+
* @param {string} signal - The name of the signal to be emitted.
|
|
1296
|
+
* @param {AnyObject} data - The data object to be sent along with the signal. Metadata
|
|
1297
|
+
* will be injected into this object before being emitted.
|
|
1298
|
+
* @return {void} No return value.
|
|
1299
|
+
*/
|
|
1097
1300
|
emitWithMetadata(signal, data) {
|
|
1098
1301
|
if (!this.task?.isHidden) {
|
|
1099
1302
|
data.__signalEmission = {
|
|
1100
1303
|
taskName: this.task.name,
|
|
1101
1304
|
taskVersion: this.task.version,
|
|
1102
|
-
taskExecutionId: this.id
|
|
1305
|
+
taskExecutionId: this.id,
|
|
1306
|
+
routineExecutionId: this.routineExecId,
|
|
1307
|
+
executionTraceId: this.executionTraceId,
|
|
1308
|
+
isMetric: false
|
|
1103
1309
|
};
|
|
1104
|
-
const context = this.context.getMetadata();
|
|
1105
1310
|
data.__metadata = {
|
|
1106
1311
|
...data.__metadata,
|
|
1107
1312
|
__routineExecId: this.routineExecId,
|
|
1108
|
-
__executionTraceId:
|
|
1313
|
+
__executionTraceId: this.executionTraceId
|
|
1109
1314
|
};
|
|
1110
1315
|
}
|
|
1111
1316
|
this.emit(signal, data);
|
|
1112
1317
|
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Emits metrics with additional metadata describing the task execution and context.
|
|
1320
|
+
*
|
|
1321
|
+
* @param {string} signal - The signal name being emitted.
|
|
1322
|
+
* @param {AnyObject} data - The data associated with the signal emission, enriched with metadata.
|
|
1323
|
+
* @return {void} Emits the signal with enriched data and does not return a value.
|
|
1324
|
+
*/
|
|
1113
1325
|
emitMetricsWithMetadata(signal, data) {
|
|
1114
1326
|
if (!this.task?.isHidden) {
|
|
1115
1327
|
data.__signalEmission = {
|
|
1116
1328
|
taskName: this.task.name,
|
|
1117
1329
|
taskVersion: this.task.version,
|
|
1118
1330
|
taskExecutionId: this.id,
|
|
1331
|
+
routineExecutionId: this.routineExecId,
|
|
1332
|
+
executionTraceId: this.executionTraceId,
|
|
1119
1333
|
isMetric: true
|
|
1120
1334
|
};
|
|
1121
|
-
const context = this.context.getMetadata();
|
|
1122
1335
|
data.__metadata = {
|
|
1123
1336
|
...data.__metadata,
|
|
1124
1337
|
__routineExecId: this.routineExecId,
|
|
1125
|
-
__executionTraceId:
|
|
1338
|
+
__executionTraceId: this.executionTraceId
|
|
1126
1339
|
};
|
|
1127
1340
|
}
|
|
1128
1341
|
this.emitMetrics(signal, data);
|
|
1129
1342
|
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Updates the progress of a task and emits metrics with associated metadata.
|
|
1345
|
+
*
|
|
1346
|
+
* @param {number} progress - A number representing the progress value, which will be clamped between 0 and 1.
|
|
1347
|
+
* @return {void} This method does not return a value.
|
|
1348
|
+
*/
|
|
1130
1349
|
onProgress(progress) {
|
|
1131
1350
|
progress = Math.min(Math.max(0, progress), 1);
|
|
1132
1351
|
this.emitMetricsWithMetadata("meta.node.progress", {
|
|
@@ -1149,6 +1368,18 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1149
1368
|
}
|
|
1150
1369
|
);
|
|
1151
1370
|
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Processes the result of the current operation, validates it, and determines the next set of nodes.
|
|
1373
|
+
*
|
|
1374
|
+
* This method ensures that results of certain types such as strings or arrays
|
|
1375
|
+
* are flagged as errors. It divides the current context into subsequent nodes
|
|
1376
|
+
* for further processing. If the division returns a promise, it delegates the
|
|
1377
|
+
* processing to `postProcessAsync`. For synchronous division, it sets the
|
|
1378
|
+
* `nextNodes` and finalizes the operation.
|
|
1379
|
+
*
|
|
1380
|
+
* @return {(Array|undefined)} Returns an array of next nodes for further processing,
|
|
1381
|
+
* or undefined if no further processing is required.
|
|
1382
|
+
*/
|
|
1152
1383
|
postProcess() {
|
|
1153
1384
|
if (typeof this.result === "string") {
|
|
1154
1385
|
this.onError(
|
|
@@ -1166,11 +1397,23 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1166
1397
|
this.finalize();
|
|
1167
1398
|
return this.nextNodes;
|
|
1168
1399
|
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Asynchronously processes and finalizes the provided graph nodes.
|
|
1402
|
+
*
|
|
1403
|
+
* @param {Promise<GraphNode[]>} nextNodes A promise that resolves to an array of graph nodes to be processed.
|
|
1404
|
+
* @return {Promise<GraphNode[]>} A promise that resolves to the processed array of graph nodes.
|
|
1405
|
+
*/
|
|
1169
1406
|
async postProcessAsync(nextNodes) {
|
|
1170
1407
|
this.nextNodes = await nextNodes;
|
|
1171
1408
|
this.finalize();
|
|
1172
1409
|
return this.nextNodes;
|
|
1173
1410
|
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Finalizes the current task execution by determining if the task is complete, handles any errors or failures,
|
|
1413
|
+
* emits relevant signals based on the task outcomes, and ensures proper end of the task lifecycle.
|
|
1414
|
+
*
|
|
1415
|
+
* @return {void} Does not return a value.
|
|
1416
|
+
*/
|
|
1174
1417
|
finalize() {
|
|
1175
1418
|
if (this.nextNodes.length === 0) {
|
|
1176
1419
|
this.completeSubgraph();
|
|
@@ -1186,6 +1429,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1186
1429
|
}
|
|
1187
1430
|
this.end();
|
|
1188
1431
|
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Handles an error event, processes the error, and updates the state accordingly.
|
|
1434
|
+
*
|
|
1435
|
+
* @param {unknown} error - The error object or message that occurred.
|
|
1436
|
+
* @param {AnyObject} [errorData={}] - Additional error data to include in the result.
|
|
1437
|
+
* @return {void} This method does not return any value.
|
|
1438
|
+
*/
|
|
1189
1439
|
onError(error, errorData = {}) {
|
|
1190
1440
|
this.result = {
|
|
1191
1441
|
...this.context.getFullContext(),
|
|
@@ -1199,6 +1449,12 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1199
1449
|
this.migrate(this.result);
|
|
1200
1450
|
this.errored = true;
|
|
1201
1451
|
}
|
|
1452
|
+
/**
|
|
1453
|
+
* Retries a task based on the defined retry count and delay time. If the retry count is 0, it immediately resolves with the provided previous result.
|
|
1454
|
+
*
|
|
1455
|
+
* @param {any} [prevResult] - The result from a previous attempt, if any, to return when no retries are performed.
|
|
1456
|
+
* @return {Promise<TaskResult>} - A promise that resolves with the result of the retried task or the previous result if no retries occur.
|
|
1457
|
+
*/
|
|
1202
1458
|
async retry(prevResult) {
|
|
1203
1459
|
if (this.retryCount === 0) {
|
|
1204
1460
|
return prevResult;
|
|
@@ -1206,6 +1462,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1206
1462
|
await this.delayRetry();
|
|
1207
1463
|
return this.work();
|
|
1208
1464
|
}
|
|
1465
|
+
/**
|
|
1466
|
+
* Retries an asynchronous operation and returns its result.
|
|
1467
|
+
* If the retry count is zero, the method immediately returns the provided previous result.
|
|
1468
|
+
*
|
|
1469
|
+
* @param {any} [prevResult] - The optional result from a previous operation attempt, if applicable.
|
|
1470
|
+
* @return {Promise<TaskResult>} A promise that resolves to the result of the retried operation.
|
|
1471
|
+
*/
|
|
1209
1472
|
async retryAsync(prevResult) {
|
|
1210
1473
|
if (this.retryCount === 0) {
|
|
1211
1474
|
return prevResult;
|
|
@@ -1223,6 +1486,15 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1223
1486
|
this.retryDelay = this.task.retryDelayMax;
|
|
1224
1487
|
}
|
|
1225
1488
|
}
|
|
1489
|
+
/**
|
|
1490
|
+
* Processes the result of a task by generating new nodes based on the task output.
|
|
1491
|
+
* The method handles synchronous and asynchronous generators, validates task output,
|
|
1492
|
+
* and creates new nodes accordingly. If errors occur, the method attempts to handle them
|
|
1493
|
+
* by generating alternative task nodes.
|
|
1494
|
+
*
|
|
1495
|
+
* @return {GraphNode[] | Promise<GraphNode[]>} Returns an array of generated GraphNode objects
|
|
1496
|
+
* (synchronously or wrapped in a Promise) based on the task result, or propagates errors if validation fails.
|
|
1497
|
+
*/
|
|
1226
1498
|
divide() {
|
|
1227
1499
|
const newNodes = [];
|
|
1228
1500
|
if (this.result?.next && typeof this.result.next === "function") {
|
|
@@ -1261,7 +1533,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1261
1533
|
if (this.errored) {
|
|
1262
1534
|
newNodes.push(
|
|
1263
1535
|
...this.task.mapNext(
|
|
1264
|
-
(t) => this.clone().split((0,
|
|
1536
|
+
(t) => this.clone().split((0, import_uuid4.v4)()).differentiate(t).migrate({ ...this.result }),
|
|
1265
1537
|
true
|
|
1266
1538
|
)
|
|
1267
1539
|
);
|
|
@@ -1274,6 +1546,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1274
1546
|
});
|
|
1275
1547
|
return newNodes;
|
|
1276
1548
|
}
|
|
1549
|
+
/**
|
|
1550
|
+
* Processes an asynchronous iterator result, validates its output, and generates new graph nodes accordingly.
|
|
1551
|
+
* Additionally, continues to process and validate results from an asynchronous generator.
|
|
1552
|
+
*
|
|
1553
|
+
* @param {Promise<IteratorResult<any>>} current - A promise resolving to the current step result from an asynchronous iterator.
|
|
1554
|
+
* @return {Promise<GraphNode[]>} A promise resolving to an array of generated GraphNode objects based on validated outputs.
|
|
1555
|
+
*/
|
|
1277
1556
|
async divideAsync(current) {
|
|
1278
1557
|
const nextNodes = [];
|
|
1279
1558
|
const _current = await current;
|
|
@@ -1296,8 +1575,14 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1296
1575
|
this.divided = true;
|
|
1297
1576
|
return nextNodes;
|
|
1298
1577
|
}
|
|
1578
|
+
/**
|
|
1579
|
+
* Generates new nodes based on the provided result and task configuration.
|
|
1580
|
+
*
|
|
1581
|
+
* @param {any} result - The result of the previous operation, which determines the configuration and context for new nodes. It can be a boolean or an object containing details like failure, errors, or metadata.
|
|
1582
|
+
* @return {GraphNode[]} An array of newly generated graph nodes configured based on the task and context.
|
|
1583
|
+
*/
|
|
1299
1584
|
generateNewNodes(result) {
|
|
1300
|
-
const groupId = (0,
|
|
1585
|
+
const groupId = (0, import_uuid4.v4)();
|
|
1301
1586
|
const newNodes = [];
|
|
1302
1587
|
if (typeof result !== "boolean") {
|
|
1303
1588
|
const failed = result.failed !== void 0 && result.failed || result.error !== void 0;
|
|
@@ -1338,6 +1623,12 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1338
1623
|
}
|
|
1339
1624
|
return newNodes;
|
|
1340
1625
|
}
|
|
1626
|
+
/**
|
|
1627
|
+
* Executes the differentiation process based on a given task and updates the instance properties accordingly.
|
|
1628
|
+
*
|
|
1629
|
+
* @param {Task} task - The task object containing information such as retry count, retry delay, and metadata status.
|
|
1630
|
+
* @return {GraphNode} The updated instance after processing the task.
|
|
1631
|
+
*/
|
|
1341
1632
|
differentiate(task) {
|
|
1342
1633
|
this.task = task;
|
|
1343
1634
|
this.retryCount = task.retryCount;
|
|
@@ -1345,14 +1636,32 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1345
1636
|
this.silent = task.isMeta && !this.debug || task.isSubMeta || this.context?.getMetadata()?.__isSubMeta;
|
|
1346
1637
|
return this;
|
|
1347
1638
|
}
|
|
1639
|
+
/**
|
|
1640
|
+
* Migrates the current instance to a new context and returns the updated instance.
|
|
1641
|
+
*
|
|
1642
|
+
* @param {any} ctx - The context data to be used for migration.
|
|
1643
|
+
* @return {GraphNode} The updated instance after migration.
|
|
1644
|
+
*/
|
|
1348
1645
|
migrate(ctx) {
|
|
1349
1646
|
this.context = new GraphContext(ctx);
|
|
1350
1647
|
return this;
|
|
1351
1648
|
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Splits the current node into a new group identified by the provided ID.
|
|
1651
|
+
*
|
|
1652
|
+
* @param {string} id - The unique identifier for the new split group.
|
|
1653
|
+
* @return {GraphNode} The current instance of the GraphNode with the updated split group ID.
|
|
1654
|
+
*/
|
|
1352
1655
|
split(id) {
|
|
1353
1656
|
this.splitGroupId = id;
|
|
1354
1657
|
return this;
|
|
1355
1658
|
}
|
|
1659
|
+
/**
|
|
1660
|
+
* Creates a new instance of the GraphNode with the current node's properties.
|
|
1661
|
+
* This method allows for duplicating the existing graph node.
|
|
1662
|
+
*
|
|
1663
|
+
* @return {GraphNode} A new instance of GraphNode that is a copy of the current node.
|
|
1664
|
+
*/
|
|
1356
1665
|
clone() {
|
|
1357
1666
|
return new _GraphNode(
|
|
1358
1667
|
this.task,
|
|
@@ -1363,6 +1672,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1363
1672
|
this.verbose
|
|
1364
1673
|
);
|
|
1365
1674
|
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Consumes the given graph node by combining contexts, merging previous nodes,
|
|
1677
|
+
* and performing associated operations on the provided node.
|
|
1678
|
+
*
|
|
1679
|
+
* @param {GraphNode} node - The graph node to be consumed.
|
|
1680
|
+
* @return {void} This method does not return a value.
|
|
1681
|
+
*/
|
|
1366
1682
|
consume(node) {
|
|
1367
1683
|
this.context = this.context.combine(node.context);
|
|
1368
1684
|
this.previousNodes = this.previousNodes.concat(node.previousNodes);
|
|
@@ -1370,9 +1686,22 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1370
1686
|
node.changeIdentity(this.id);
|
|
1371
1687
|
node.destroy();
|
|
1372
1688
|
}
|
|
1689
|
+
/**
|
|
1690
|
+
* Changes the identity of the current instance by updating the `id` property.
|
|
1691
|
+
*
|
|
1692
|
+
* @param {string} id - The new identity value to be assigned.
|
|
1693
|
+
* @return {void} Does not return a value.
|
|
1694
|
+
*/
|
|
1373
1695
|
changeIdentity(id) {
|
|
1374
1696
|
this.id = id;
|
|
1375
1697
|
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Completes the subgraph for the current node and recursively for its previous nodes
|
|
1700
|
+
* once all next nodes have their subgraphs marked as done. If there are no previous nodes,
|
|
1701
|
+
* it completes the entire graph.
|
|
1702
|
+
*
|
|
1703
|
+
* @return {void} Does not return a value.
|
|
1704
|
+
*/
|
|
1376
1705
|
completeSubgraph() {
|
|
1377
1706
|
for (const node of this.nextNodes) {
|
|
1378
1707
|
if (!node.subgraphDone()) {
|
|
@@ -1386,10 +1715,22 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1386
1715
|
}
|
|
1387
1716
|
this.previousNodes.forEach((n) => n.completeSubgraph());
|
|
1388
1717
|
}
|
|
1718
|
+
/**
|
|
1719
|
+
* Completes the current graph by setting a flag indicating the graph has been completed
|
|
1720
|
+
* and recursively completes all subsequent nodes in the graph.
|
|
1721
|
+
*
|
|
1722
|
+
* @return {void} Does not return a value.
|
|
1723
|
+
*/
|
|
1389
1724
|
completeGraph() {
|
|
1390
1725
|
this.graphComplete = true;
|
|
1391
1726
|
this.nextNodes.forEach((n) => n.completeGraph());
|
|
1392
1727
|
}
|
|
1728
|
+
/**
|
|
1729
|
+
* Destroys the current instance by releasing resources, breaking references,
|
|
1730
|
+
* and resetting properties to ensure proper cleanup.
|
|
1731
|
+
*
|
|
1732
|
+
* @return {void} No return value.
|
|
1733
|
+
*/
|
|
1393
1734
|
destroy() {
|
|
1394
1735
|
this.context = null;
|
|
1395
1736
|
this.task = null;
|
|
@@ -1402,15 +1743,40 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
1402
1743
|
this.layer = void 0;
|
|
1403
1744
|
this.destroyed = true;
|
|
1404
1745
|
}
|
|
1746
|
+
/**
|
|
1747
|
+
* Retrieves an iterator for traversing through the graph nodes.
|
|
1748
|
+
*
|
|
1749
|
+
* @return {GraphNodeIterator} An iterator instance specific to this graph node.
|
|
1750
|
+
*/
|
|
1405
1751
|
getIterator() {
|
|
1406
1752
|
return new GraphNodeIterator(this);
|
|
1407
1753
|
}
|
|
1754
|
+
/**
|
|
1755
|
+
* Applies a callback function to each node in the `nextNodes` array and returns
|
|
1756
|
+
* the resulting array from the map operation.
|
|
1757
|
+
*
|
|
1758
|
+
* @param {function} callback - A function to execute on each `GraphNode` in the `nextNodes` array.
|
|
1759
|
+
* The function receives a `GraphNode` as its argument.
|
|
1760
|
+
* @return {Array} The resulting array after applying the callback function to each node in `nextNodes`.
|
|
1761
|
+
*/
|
|
1408
1762
|
mapNext(callback) {
|
|
1409
1763
|
return this.nextNodes.map(callback);
|
|
1410
1764
|
}
|
|
1765
|
+
/**
|
|
1766
|
+
* Accepts a visitor object and calls its visitNode method with the current instance.
|
|
1767
|
+
*
|
|
1768
|
+
* @param {GraphVisitor} visitor - The visitor instance implementing the GraphVisitor interface.
|
|
1769
|
+
* @return {void} This method does not return a value.
|
|
1770
|
+
*/
|
|
1411
1771
|
accept(visitor) {
|
|
1412
1772
|
visitor.visitNode(this);
|
|
1413
1773
|
}
|
|
1774
|
+
/**
|
|
1775
|
+
* Exports the current object's state and returns it in a serialized format.
|
|
1776
|
+
* The exported object contains metadata, task details, context information, execution times, node relationships, routine execution status, and other state information.
|
|
1777
|
+
*
|
|
1778
|
+
* @return {Object} An object representing the current state.
|
|
1779
|
+
*/
|
|
1414
1780
|
export() {
|
|
1415
1781
|
return {
|
|
1416
1782
|
__id: this.id,
|
|
@@ -1508,9 +1874,12 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
1508
1874
|
});
|
|
1509
1875
|
}
|
|
1510
1876
|
/**
|
|
1511
|
-
*
|
|
1512
|
-
*
|
|
1513
|
-
*
|
|
1877
|
+
* Iterates over each task in the `tasks` collection and applies the provided callback function.
|
|
1878
|
+
* If the callback returns a Promise, resolves all Promises concurrently.
|
|
1879
|
+
*
|
|
1880
|
+
* @param {function} callBack - A function to be executed on each task from the `tasks` collection.
|
|
1881
|
+
* The callback receives the current task as its argument.
|
|
1882
|
+
* @return {Promise<void>} A Promise that resolves once all callback executions, including asynchronous ones, are complete.
|
|
1514
1883
|
*/
|
|
1515
1884
|
async forEachTask(callBack) {
|
|
1516
1885
|
const promises = [];
|
|
@@ -1529,10 +1898,10 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
1529
1898
|
this.emit("meta.routine.global_version_set", { version: this.version });
|
|
1530
1899
|
}
|
|
1531
1900
|
/**
|
|
1532
|
-
* Subscribes to signals
|
|
1533
|
-
*
|
|
1534
|
-
* @
|
|
1535
|
-
* @
|
|
1901
|
+
* Subscribes the current instance to the specified signals, enabling it to observe them.
|
|
1902
|
+
*
|
|
1903
|
+
* @param {...string} signals - The names of the signals to observe.
|
|
1904
|
+
* @return {this} Returns the instance to allow for method chaining.
|
|
1536
1905
|
*/
|
|
1537
1906
|
doOn(...signals) {
|
|
1538
1907
|
signals.forEach((signal) => {
|
|
@@ -1543,8 +1912,11 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
1543
1912
|
return this;
|
|
1544
1913
|
}
|
|
1545
1914
|
/**
|
|
1546
|
-
* Unsubscribes from all observed signals
|
|
1547
|
-
*
|
|
1915
|
+
* Unsubscribes from all observed signals and clears the internal collection
|
|
1916
|
+
* of observed signals. This ensures that the instance is no longer listening
|
|
1917
|
+
* or reacting to any previously subscribed signals.
|
|
1918
|
+
*
|
|
1919
|
+
* @return {this} Returns the current instance for chaining purposes.
|
|
1548
1920
|
*/
|
|
1549
1921
|
unsubscribeAll() {
|
|
1550
1922
|
this.observedSignals.forEach(
|
|
@@ -1554,10 +1926,10 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
1554
1926
|
return this;
|
|
1555
1927
|
}
|
|
1556
1928
|
/**
|
|
1557
|
-
* Unsubscribes from
|
|
1558
|
-
*
|
|
1559
|
-
* @
|
|
1560
|
-
* @
|
|
1929
|
+
* Unsubscribes the current instance from the specified signals.
|
|
1930
|
+
*
|
|
1931
|
+
* @param {...string} signals - The signals to unsubscribe from.
|
|
1932
|
+
* @return {this} The current instance for method chaining.
|
|
1561
1933
|
*/
|
|
1562
1934
|
unsubscribe(...signals) {
|
|
1563
1935
|
signals.forEach((signal) => {
|
|
@@ -1569,7 +1941,12 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
1569
1941
|
return this;
|
|
1570
1942
|
}
|
|
1571
1943
|
/**
|
|
1572
|
-
*
|
|
1944
|
+
* Cleans up resources and emits an event indicating the destruction of the routine.
|
|
1945
|
+
*
|
|
1946
|
+
* This method unsubscribes from all events, clears the tasks list,
|
|
1947
|
+
* and emits a "meta.routine.destroyed" event with details of the destruction.
|
|
1948
|
+
*
|
|
1949
|
+
* @return {void}
|
|
1573
1950
|
*/
|
|
1574
1951
|
destroy() {
|
|
1575
1952
|
this.unsubscribeAll();
|
|
@@ -1615,27 +1992,27 @@ var TaskIterator = class {
|
|
|
1615
1992
|
// src/graph/definition/Task.ts
|
|
1616
1993
|
var Task = class extends SignalEmitter {
|
|
1617
1994
|
/**
|
|
1618
|
-
* Constructs
|
|
1619
|
-
*
|
|
1620
|
-
* @param task
|
|
1621
|
-
* @param
|
|
1622
|
-
* @param
|
|
1623
|
-
* @param
|
|
1624
|
-
* @param
|
|
1625
|
-
* @param
|
|
1626
|
-
* @param
|
|
1627
|
-
* @param
|
|
1628
|
-
* @param
|
|
1629
|
-
* @param
|
|
1630
|
-
* @param
|
|
1631
|
-
* @param
|
|
1632
|
-
* @param
|
|
1633
|
-
* @param
|
|
1634
|
-
* @param
|
|
1635
|
-
* @param
|
|
1636
|
-
* @param
|
|
1637
|
-
* @param
|
|
1638
|
-
* @
|
|
1995
|
+
* Constructs an instance of the task with the specified properties and configuration options.
|
|
1996
|
+
*
|
|
1997
|
+
* @param {string} name - The name of the task.
|
|
1998
|
+
* @param {TaskFunction} task - The function that represents the task logic.
|
|
1999
|
+
* @param {string} [description=""] - A description of the task.
|
|
2000
|
+
* @param {number} [concurrency=0] - The number of concurrent executions allowed for the task.
|
|
2001
|
+
* @param {number} [timeout=0] - The maximum execution time for the task in milliseconds.
|
|
2002
|
+
* @param {boolean} [register=true] - Indicates if the task should be registered or not.
|
|
2003
|
+
* @param {boolean} [isUnique=false] - Specifies if the task should only allow one instance to exist at any time.
|
|
2004
|
+
* @param {boolean} [isMeta=false] - Indicates if the task is a meta-task.
|
|
2005
|
+
* @param {boolean} [isSubMeta=false] - Indicates if the task is a sub-meta-task.
|
|
2006
|
+
* @param {boolean} [isHidden=false] - Determines if the task is hidden and not exposed publicly.
|
|
2007
|
+
* @param {ThrottleTagGetter} [getTagCallback=undefined] - A callback to generate a throttle tag for the task.
|
|
2008
|
+
* @param {SchemaDefinition} [inputSchema=undefined] - The input schema for validating the task's input context.
|
|
2009
|
+
* @param {boolean} [validateInputContext=false] - Specifies if the input context should be validated against the input schema.
|
|
2010
|
+
* @param {SchemaDefinition} [outputSchema=undefined] - The output schema for validating the task's output context.
|
|
2011
|
+
* @param {boolean} [validateOutputContext=false] - Specifies if the output context should be validated against the output schema.
|
|
2012
|
+
* @param {number} [retryCount=0] - The number of retry attempts allowed for the task in case of failure.
|
|
2013
|
+
* @param {number} [retryDelay=0] - The initial delay (in milliseconds) between retry attempts.
|
|
2014
|
+
* @param {number} [retryDelayMax=0] - The maximum delay (in milliseconds) allowed between retries.
|
|
2015
|
+
* @param {number} [retryDelayFactor=1] - The factor by which the retry delay increases after each attempt.
|
|
1639
2016
|
*/
|
|
1640
2017
|
constructor(name, task, description = "", concurrency = 0, timeout = 0, register = true, isUnique = false, isMeta = false, isSubMeta = false, isHidden = false, getTagCallback = void 0, inputSchema = void 0, validateInputContext = false, outputSchema = void 0, validateOutputContext = false, retryCount = 0, retryDelay = 0, retryDelayMax = 0, retryDelayFactor = 1) {
|
|
1641
2018
|
super(isSubMeta || isHidden);
|
|
@@ -1724,6 +2101,13 @@ var Task = class extends SignalEmitter {
|
|
|
1724
2101
|
});
|
|
1725
2102
|
}
|
|
1726
2103
|
}
|
|
2104
|
+
/**
|
|
2105
|
+
* Retrieves the tag associated with the instance.
|
|
2106
|
+
* Can be overridden by subclasses.
|
|
2107
|
+
*
|
|
2108
|
+
* @param {AnyObject} [context] - Optional context parameter that can be provided.
|
|
2109
|
+
* @return {string} The tag value of the instance.
|
|
2110
|
+
*/
|
|
1727
2111
|
getTag(context) {
|
|
1728
2112
|
return this.name;
|
|
1729
2113
|
}
|
|
@@ -1754,16 +2138,35 @@ var Task = class extends SignalEmitter {
|
|
|
1754
2138
|
setValidateOutputContext(value) {
|
|
1755
2139
|
this.validateOutputContext = value;
|
|
1756
2140
|
}
|
|
2141
|
+
/**
|
|
2142
|
+
* Emits a signal along with metadata if certain conditions are met.
|
|
2143
|
+
*
|
|
2144
|
+
* This method sends a signal with optional context data and adds metadata
|
|
2145
|
+
* to the emitted data if the instance is not hidden and not a subordinate metadata object.
|
|
2146
|
+
*
|
|
2147
|
+
* @param {string} signal - The name of the signal to emit.
|
|
2148
|
+
* @param {AnyObject} [ctx={}] - Additional context data to include with the emitted signal.
|
|
2149
|
+
* @return {void} Does not return a value.
|
|
2150
|
+
*/
|
|
1757
2151
|
emitWithMetadata(signal, ctx = {}) {
|
|
1758
2152
|
const data = { ...ctx };
|
|
1759
2153
|
if (!this.isHidden && !this.isSubMeta) {
|
|
1760
2154
|
data.__signalEmission = {
|
|
1761
2155
|
taskName: this.name,
|
|
1762
|
-
taskVersion: this.version
|
|
2156
|
+
taskVersion: this.version,
|
|
2157
|
+
isMetric: false
|
|
1763
2158
|
};
|
|
1764
2159
|
}
|
|
1765
2160
|
this.emit(signal, data);
|
|
1766
2161
|
}
|
|
2162
|
+
/**
|
|
2163
|
+
* Emits metrics with additional metadata enhancement based on the context and the state of the instance.
|
|
2164
|
+
* This is used to prevent loops on the meta layer in debug mode.
|
|
2165
|
+
*
|
|
2166
|
+
* @param {string} signal - The signal identifier for the metric being emitted.
|
|
2167
|
+
* @param {AnyObject} [ctx={}] - Optional context object to provide additional information with the metric.
|
|
2168
|
+
* @return {void} This method does not return any value.
|
|
2169
|
+
*/
|
|
1767
2170
|
emitMetricsWithMetadata(signal, ctx = {}) {
|
|
1768
2171
|
const data = { ...ctx };
|
|
1769
2172
|
if (!this.isHidden && !this.isSubMeta) {
|
|
@@ -1776,12 +2179,13 @@ var Task = class extends SignalEmitter {
|
|
|
1776
2179
|
this.emitMetrics(signal, data);
|
|
1777
2180
|
}
|
|
1778
2181
|
/**
|
|
1779
|
-
* Validates a
|
|
1780
|
-
*
|
|
1781
|
-
* @param
|
|
1782
|
-
* @param
|
|
1783
|
-
* @
|
|
1784
|
-
* @
|
|
2182
|
+
* Validates a data object against a specified schema definition and returns validation results.
|
|
2183
|
+
*
|
|
2184
|
+
* @param {any} data - The target object to validate against the schema.
|
|
2185
|
+
* @param {SchemaDefinition | undefined} schema - The schema definition describing the expected structure and constraints of the data.
|
|
2186
|
+
* @param {string} [path="context"] - The base path or context for traversing the data, used in generating error messages.
|
|
2187
|
+
* @return {{ valid: boolean, errors: Record<string, string> }} - An object containing a validity flag (`valid`)
|
|
2188
|
+
* and a map (`errors`) of validation error messages keyed by property paths.
|
|
1785
2189
|
*/
|
|
1786
2190
|
validateSchema(data, schema, path = "context") {
|
|
1787
2191
|
const errors = {};
|
|
@@ -1884,6 +2288,12 @@ var Task = class extends SignalEmitter {
|
|
|
1884
2288
|
}
|
|
1885
2289
|
return { valid: true, errors: {} };
|
|
1886
2290
|
}
|
|
2291
|
+
/**
|
|
2292
|
+
* Validates the input context against the predefined schema and emits metadata if validation fails.
|
|
2293
|
+
*
|
|
2294
|
+
* @param {AnyObject} context - The input context to validate.
|
|
2295
|
+
* @return {true | AnyObject} - Returns `true` if validation succeeds, otherwise returns an error object containing details of the validation failure.
|
|
2296
|
+
*/
|
|
1887
2297
|
validateInput(context) {
|
|
1888
2298
|
if (this.validateInputContext) {
|
|
1889
2299
|
const validationResult = this.validateSchema(
|
|
@@ -1906,6 +2316,13 @@ var Task = class extends SignalEmitter {
|
|
|
1906
2316
|
}
|
|
1907
2317
|
return true;
|
|
1908
2318
|
}
|
|
2319
|
+
/**
|
|
2320
|
+
* Validates the output context using the provided schema and emits metadata if validation fails.
|
|
2321
|
+
*
|
|
2322
|
+
* @param {AnyObject} context - The output context to validate.
|
|
2323
|
+
* @return {true | AnyObject} Returns `true` if the output context is valid; otherwise, returns an object
|
|
2324
|
+
* containing error information when validation fails.
|
|
2325
|
+
*/
|
|
1909
2326
|
validateOutput(context) {
|
|
1910
2327
|
if (this.validateOutputContext) {
|
|
1911
2328
|
const validationResult = this.validateSchema(
|
|
@@ -1929,14 +2346,13 @@ var Task = class extends SignalEmitter {
|
|
|
1929
2346
|
return true;
|
|
1930
2347
|
}
|
|
1931
2348
|
/**
|
|
1932
|
-
* Executes
|
|
1933
|
-
*
|
|
1934
|
-
* @param
|
|
1935
|
-
* @param
|
|
1936
|
-
* @param
|
|
1937
|
-
* @
|
|
1938
|
-
* @
|
|
1939
|
-
* @edge If validateOutputContext is true, validates output; on failure, emits 'meta.task.outputValidationFailed' with detailed errors.
|
|
2349
|
+
* Executes a task within a given context, optionally emitting signals and reporting progress.
|
|
2350
|
+
*
|
|
2351
|
+
* @param {GraphContext} context The execution context which provides data and functions necessary for the task.
|
|
2352
|
+
* @param {function(string, AnyObject): void} emit A function to emit signals and communicate intermediate results or states.
|
|
2353
|
+
* @param {function(number): void} progressCallback A callback function used to report task progress as a percentage (0 to 100).
|
|
2354
|
+
* @param {{ nodeId: string; routineExecId: string }} nodeData An object containing identifiers related to the node and execution routine.
|
|
2355
|
+
* @return {TaskResult} The result of the executed task.
|
|
1940
2356
|
*/
|
|
1941
2357
|
execute(context, emit, progressCallback, nodeData) {
|
|
1942
2358
|
return this.taskFunction(
|
|
@@ -1945,6 +2361,15 @@ var Task = class extends SignalEmitter {
|
|
|
1945
2361
|
progressCallback
|
|
1946
2362
|
);
|
|
1947
2363
|
}
|
|
2364
|
+
/**
|
|
2365
|
+
* Adds tasks as predecessors to the current task and establishes dependencies between them.
|
|
2366
|
+
* Ensures that adding predecessors does not create cyclic dependencies.
|
|
2367
|
+
* Updates task relationships, progress weights, and emits relevant metrics after operations.
|
|
2368
|
+
*
|
|
2369
|
+
* @param {Task[]} tasks - An array of tasks to be added as predecessors to the current task.
|
|
2370
|
+
* @return {this} The current task instance for method chaining.
|
|
2371
|
+
* @throws {Error} Throws an error if adding a predecessor creates a cycle in the task structure.
|
|
2372
|
+
*/
|
|
1948
2373
|
doAfter(...tasks) {
|
|
1949
2374
|
for (const pred of tasks) {
|
|
1950
2375
|
if (this.predecessorTasks.has(pred)) continue;
|
|
@@ -1967,6 +2392,14 @@ var Task = class extends SignalEmitter {
|
|
|
1967
2392
|
this.updateProgressWeights();
|
|
1968
2393
|
return this;
|
|
1969
2394
|
}
|
|
2395
|
+
/**
|
|
2396
|
+
* Adds a sequence of tasks as successors to the current task, ensuring no cyclic dependencies are introduced.
|
|
2397
|
+
* Metrics are emitted when a relationship is successfully added.
|
|
2398
|
+
*
|
|
2399
|
+
* @param {...Task} tasks - The tasks to be added as successors to the current task.
|
|
2400
|
+
* @return {this} Returns the current task instance for method chaining.
|
|
2401
|
+
* @throws {Error} Throws an error if adding a task causes a cyclic dependency.
|
|
2402
|
+
*/
|
|
1970
2403
|
then(...tasks) {
|
|
1971
2404
|
for (const next of tasks) {
|
|
1972
2405
|
if (this.nextTasks.has(next)) continue;
|
|
@@ -1989,6 +2422,12 @@ var Task = class extends SignalEmitter {
|
|
|
1989
2422
|
this.updateProgressWeights();
|
|
1990
2423
|
return this;
|
|
1991
2424
|
}
|
|
2425
|
+
/**
|
|
2426
|
+
* Decouples the current task from the provided task by removing mutual references.
|
|
2427
|
+
*
|
|
2428
|
+
* @param {Task} task - The task to decouple from the current task.
|
|
2429
|
+
* @return {void} This method does not return a value.
|
|
2430
|
+
*/
|
|
1992
2431
|
decouple(task) {
|
|
1993
2432
|
if (task.nextTasks.has(this)) {
|
|
1994
2433
|
task.nextTasks.delete(this);
|
|
@@ -2000,6 +2439,14 @@ var Task = class extends SignalEmitter {
|
|
|
2000
2439
|
}
|
|
2001
2440
|
this.updateLayerFromPredecessors();
|
|
2002
2441
|
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Updates the progress weights for tasks within each layer of the subgraph.
|
|
2444
|
+
* The progress weight for each task is calculated based on the inverse proportion
|
|
2445
|
+
* of the number of layers and the number of tasks in each layer. This ensures an
|
|
2446
|
+
* even distribution of progress weight across the tasks in the layers.
|
|
2447
|
+
*
|
|
2448
|
+
* @return {void} Does not return a value.
|
|
2449
|
+
*/
|
|
2003
2450
|
updateProgressWeights() {
|
|
2004
2451
|
const layers = this.getSubgraphLayers();
|
|
2005
2452
|
const numLayers = layers.size;
|
|
@@ -2013,6 +2460,12 @@ var Task = class extends SignalEmitter {
|
|
|
2013
2460
|
);
|
|
2014
2461
|
});
|
|
2015
2462
|
}
|
|
2463
|
+
/**
|
|
2464
|
+
* Retrieves a mapping of layer indices to sets of tasks within each layer of a subgraph.
|
|
2465
|
+
* This method traverses the task dependencies and organizes tasks by their respective layer indices.
|
|
2466
|
+
*
|
|
2467
|
+
* @return {Map<number, Set<Task>>} A map where the key is the layer index (number) and the value is a set of tasks (Set<Task>) belonging to that layer.
|
|
2468
|
+
*/
|
|
2016
2469
|
getSubgraphLayers() {
|
|
2017
2470
|
const layers = /* @__PURE__ */ new Map();
|
|
2018
2471
|
const queue = [this];
|
|
@@ -2027,6 +2480,13 @@ var Task = class extends SignalEmitter {
|
|
|
2027
2480
|
}
|
|
2028
2481
|
return layers;
|
|
2029
2482
|
}
|
|
2483
|
+
/**
|
|
2484
|
+
* Updates the `layerIndex` of the current task based on the maximum layer index of its predecessors
|
|
2485
|
+
* and propagates the update recursively to all subsequent tasks. If the `layerIndex` changes,
|
|
2486
|
+
* emits a metric event with metadata about the change.
|
|
2487
|
+
*
|
|
2488
|
+
* @return {void} This method does not return a value.
|
|
2489
|
+
*/
|
|
2030
2490
|
updateLayerFromPredecessors() {
|
|
2031
2491
|
const prevLayerIndex = this.layerIndex;
|
|
2032
2492
|
let maxPred = 0;
|
|
@@ -2049,6 +2509,12 @@ var Task = class extends SignalEmitter {
|
|
|
2049
2509
|
next.nextTasks.forEach((n) => queue.push(n));
|
|
2050
2510
|
}
|
|
2051
2511
|
}
|
|
2512
|
+
/**
|
|
2513
|
+
* Determines whether there is a cycle in the tasks graph.
|
|
2514
|
+
* This method performs a depth-first search (DFS) to detect cycles.
|
|
2515
|
+
*
|
|
2516
|
+
* @return {boolean} - Returns true if a cycle is found in the graph, otherwise false.
|
|
2517
|
+
*/
|
|
2052
2518
|
hasCycle() {
|
|
2053
2519
|
const visited = /* @__PURE__ */ new Set();
|
|
2054
2520
|
const recStack = /* @__PURE__ */ new Set();
|
|
@@ -2065,18 +2531,33 @@ var Task = class extends SignalEmitter {
|
|
|
2065
2531
|
};
|
|
2066
2532
|
return dfs(this);
|
|
2067
2533
|
}
|
|
2534
|
+
/**
|
|
2535
|
+
* Maps over the next set of tasks or failed tasks if specified, applying the provided callback function.
|
|
2536
|
+
*
|
|
2537
|
+
* @param {Function} callback A function that will be called with each task, transforming the task as needed. It receives a single parameter of type Task.
|
|
2538
|
+
* @param {boolean} [failed=false] A boolean that determines whether to map over the failed tasks (true) or the next tasks (false).
|
|
2539
|
+
* @return {any[]} An array of transformed tasks resulting from applying the callback function.
|
|
2540
|
+
*/
|
|
2068
2541
|
mapNext(callback, failed = false) {
|
|
2069
2542
|
const tasks = failed ? Array.from(this.onFailTasks) : Array.from(this.nextTasks);
|
|
2070
2543
|
return tasks.map(callback);
|
|
2071
2544
|
}
|
|
2545
|
+
/**
|
|
2546
|
+
* Maps through each task in the set of predecessor tasks and applies the provided callback function.
|
|
2547
|
+
*
|
|
2548
|
+
* @param {function} callback - A function to execute on each task in the predecessor tasks. The function receives a `Task` object as its argument and returns any value.
|
|
2549
|
+
* @return {any[]} An array containing the results of applying the callback function to each predecessor task.
|
|
2550
|
+
*/
|
|
2072
2551
|
mapPrevious(callback) {
|
|
2073
2552
|
return Array.from(this.predecessorTasks).map(callback);
|
|
2074
2553
|
}
|
|
2075
2554
|
/**
|
|
2076
|
-
*
|
|
2077
|
-
*
|
|
2078
|
-
*
|
|
2079
|
-
*
|
|
2555
|
+
* Adds the specified signals to the current instance, making it observe them.
|
|
2556
|
+
* If the instance is already observing a signal, it will be skipped.
|
|
2557
|
+
* The method also emits metadata information if the `register` property is set.
|
|
2558
|
+
*
|
|
2559
|
+
* @param {...string[]} signals - The array of signal names to observe.
|
|
2560
|
+
* @return {this} The current instance after adding the specified signals.
|
|
2080
2561
|
*/
|
|
2081
2562
|
doOn(...signals) {
|
|
2082
2563
|
signals.forEach((signal) => {
|
|
@@ -2096,9 +2577,10 @@ var Task = class extends SignalEmitter {
|
|
|
2096
2577
|
return this;
|
|
2097
2578
|
}
|
|
2098
2579
|
/**
|
|
2099
|
-
*
|
|
2100
|
-
*
|
|
2101
|
-
* @
|
|
2580
|
+
* Registers the specified signals to be emitted after the Task executes successfully and attaches them for further processing.
|
|
2581
|
+
*
|
|
2582
|
+
* @param {...string} signals - The list of signals to be registered for emission.
|
|
2583
|
+
* @return {this} The current instance for method chaining.
|
|
2102
2584
|
*/
|
|
2103
2585
|
emits(...signals) {
|
|
2104
2586
|
signals.forEach((signal) => {
|
|
@@ -2107,6 +2589,13 @@ var Task = class extends SignalEmitter {
|
|
|
2107
2589
|
});
|
|
2108
2590
|
return this;
|
|
2109
2591
|
}
|
|
2592
|
+
/**
|
|
2593
|
+
* Configures the instance to emit specified signals when the task execution fails.
|
|
2594
|
+
* A failure is defined as anything that does not return a successful result.
|
|
2595
|
+
*
|
|
2596
|
+
* @param {...string} signals - The names of the signals to emit upon failure.
|
|
2597
|
+
* @return {this} Returns the current instance for chaining.
|
|
2598
|
+
*/
|
|
2110
2599
|
emitsOnFail(...signals) {
|
|
2111
2600
|
signals.forEach((signal) => {
|
|
2112
2601
|
this.signalsToEmitOnFail.add(signal);
|
|
@@ -2114,6 +2603,13 @@ var Task = class extends SignalEmitter {
|
|
|
2114
2603
|
});
|
|
2115
2604
|
return this;
|
|
2116
2605
|
}
|
|
2606
|
+
/**
|
|
2607
|
+
* Attaches a signal to the current context and emits metadata if the register flag is set.
|
|
2608
|
+
*
|
|
2609
|
+
* @param {string} signal - The name of the signal to attach.
|
|
2610
|
+
* @param {boolean} [isOnFail=false] - Indicates if the signal should be marked as "on fail".
|
|
2611
|
+
* @return {void} This method does not return a value.
|
|
2612
|
+
*/
|
|
2117
2613
|
attachSignal(signal, isOnFail = false) {
|
|
2118
2614
|
this.emitsSignals.add(signal);
|
|
2119
2615
|
if (this.register) {
|
|
@@ -2128,10 +2624,12 @@ var Task = class extends SignalEmitter {
|
|
|
2128
2624
|
}
|
|
2129
2625
|
}
|
|
2130
2626
|
/**
|
|
2131
|
-
* Unsubscribes from
|
|
2132
|
-
*
|
|
2133
|
-
*
|
|
2134
|
-
*
|
|
2627
|
+
* Unsubscribes the current instance from the specified signals.
|
|
2628
|
+
* This method removes the signals from the observedSignals set, unsubscribes
|
|
2629
|
+
* from the underlying broker, and emits metadata for the unsubscription if applicable.
|
|
2630
|
+
*
|
|
2631
|
+
* @param {string[]} signals - The list of signal names to unsubscribe from.
|
|
2632
|
+
* @return {this} Returns the current instance for method chaining.
|
|
2135
2633
|
*/
|
|
2136
2634
|
unsubscribe(...signals) {
|
|
2137
2635
|
signals.forEach((signal) => {
|
|
@@ -2153,8 +2651,9 @@ var Task = class extends SignalEmitter {
|
|
|
2153
2651
|
return this;
|
|
2154
2652
|
}
|
|
2155
2653
|
/**
|
|
2156
|
-
* Unsubscribes from all observed signals.
|
|
2157
|
-
*
|
|
2654
|
+
* Unsubscribes from all currently observed signals and clears the list of observed signals.
|
|
2655
|
+
*
|
|
2656
|
+
* @return {this} The instance of the class to allow method chaining.
|
|
2158
2657
|
*/
|
|
2159
2658
|
unsubscribeAll() {
|
|
2160
2659
|
this.unsubscribe(...this.observedSignals);
|
|
@@ -2162,9 +2661,10 @@ var Task = class extends SignalEmitter {
|
|
|
2162
2661
|
return this;
|
|
2163
2662
|
}
|
|
2164
2663
|
/**
|
|
2165
|
-
* Detaches
|
|
2166
|
-
*
|
|
2167
|
-
* @
|
|
2664
|
+
* Detaches the specified signals from being emitted after execution and optionally emits metadata for each detached signal.
|
|
2665
|
+
*
|
|
2666
|
+
* @param {...string} signals - The list of signal names to be detached. Signals can be in the format "namespace:signalName".
|
|
2667
|
+
* @return {this} Returns the current instance of the object for method chaining.
|
|
2168
2668
|
*/
|
|
2169
2669
|
detachSignals(...signals) {
|
|
2170
2670
|
signals.forEach((signal) => {
|
|
@@ -2183,27 +2683,50 @@ var Task = class extends SignalEmitter {
|
|
|
2183
2683
|
return this;
|
|
2184
2684
|
}
|
|
2185
2685
|
/**
|
|
2186
|
-
* Detaches all
|
|
2187
|
-
*
|
|
2686
|
+
* Detaches all signals associated with the object by invoking the `detachSignals` method
|
|
2687
|
+
* and clearing the `signalsToEmitAfter` collection.
|
|
2688
|
+
*
|
|
2689
|
+
* @return {this} Returns the current instance to allow method chaining.
|
|
2188
2690
|
*/
|
|
2189
2691
|
detachAllSignals() {
|
|
2190
2692
|
this.detachSignals(...this.signalsToEmitAfter);
|
|
2191
2693
|
this.signalsToEmitAfter.clear();
|
|
2192
2694
|
return this;
|
|
2193
2695
|
}
|
|
2696
|
+
/**
|
|
2697
|
+
* Maps over the signals in the `signalsToEmitAfter` set and applies a callback function to each signal.
|
|
2698
|
+
*
|
|
2699
|
+
* @param {function(string): void} callback - A function that is called with each signal
|
|
2700
|
+
* in the `signalsToEmitAfter` set, providing the signal as an argument.
|
|
2701
|
+
* @return {Array<any>} An array containing the results of applying the callback
|
|
2702
|
+
* function to each signal.
|
|
2703
|
+
*/
|
|
2194
2704
|
mapSignals(callback) {
|
|
2195
2705
|
return Array.from(this.signalsToEmitAfter).map(callback);
|
|
2196
2706
|
}
|
|
2707
|
+
/**
|
|
2708
|
+
* Maps over the signals in `signalsToEmitOnFail` and applies the provided callback to each signal.
|
|
2709
|
+
*
|
|
2710
|
+
* @param {function(string): void} callback - A function that receives each signal as a string and performs an operation or transformation on it.
|
|
2711
|
+
* @return {Array} The array resulting from applying the callback to each signal in `signalsToEmitOnFail`.
|
|
2712
|
+
*/
|
|
2197
2713
|
mapOnFailSignals(callback) {
|
|
2198
2714
|
return Array.from(this.signalsToEmitOnFail).map(callback);
|
|
2199
2715
|
}
|
|
2716
|
+
/**
|
|
2717
|
+
* Maps over the observed signals with the provided callback function.
|
|
2718
|
+
*
|
|
2719
|
+
* @param {function(string): void} callback - A function to execute on each signal in the observed signals array.
|
|
2720
|
+
* @return {Array} A new array containing the results of calling the callback function on each observed signal.
|
|
2721
|
+
*/
|
|
2200
2722
|
mapObservedSignals(callback) {
|
|
2201
2723
|
return Array.from(this.observedSignals).map(callback);
|
|
2202
2724
|
}
|
|
2203
2725
|
/**
|
|
2204
|
-
* Emits
|
|
2205
|
-
*
|
|
2206
|
-
* @
|
|
2726
|
+
* Emits a collection of signals stored in the `signalsToEmitAfter` array.
|
|
2727
|
+
*
|
|
2728
|
+
* @param {GraphContext} context - The context object containing data or state to be passed to the emitted signals.
|
|
2729
|
+
* @return {void} This method does not return a value.
|
|
2207
2730
|
*/
|
|
2208
2731
|
emitSignals(context) {
|
|
2209
2732
|
this.signalsToEmitAfter.forEach((signal) => {
|
|
@@ -2211,15 +2734,29 @@ var Task = class extends SignalEmitter {
|
|
|
2211
2734
|
});
|
|
2212
2735
|
}
|
|
2213
2736
|
/**
|
|
2214
|
-
* Emits
|
|
2215
|
-
*
|
|
2216
|
-
* @
|
|
2737
|
+
* Emits registered signals when an operation fails.
|
|
2738
|
+
*
|
|
2739
|
+
* @param {GraphContext} context - The context from which the full context is derived and passed to the signals being emitted.
|
|
2740
|
+
* @return {void} This method does not return any value.
|
|
2217
2741
|
*/
|
|
2218
2742
|
emitOnFailSignals(context) {
|
|
2219
2743
|
this.signalsToEmitOnFail.forEach((signal) => {
|
|
2220
2744
|
this.emit(signal, context.getFullContext());
|
|
2221
2745
|
});
|
|
2222
2746
|
}
|
|
2747
|
+
/**
|
|
2748
|
+
* Cleans up and destroys the task instance, detaching it from other tasks and
|
|
2749
|
+
* performing necessary cleanup operations.
|
|
2750
|
+
*
|
|
2751
|
+
* This method:
|
|
2752
|
+
* - Unsubscribes from all signals and events.
|
|
2753
|
+
* - Detaches all associated signal handlers.
|
|
2754
|
+
* - Removes the task from successor and predecessor task mappings.
|
|
2755
|
+
* - Clears all task relationships and marks the task as destroyed.
|
|
2756
|
+
* - Emits destruction metrics, if applicable.
|
|
2757
|
+
*
|
|
2758
|
+
* @return {void} No value is returned because the function performs clean-up operations.
|
|
2759
|
+
*/
|
|
2223
2760
|
destroy() {
|
|
2224
2761
|
this.unsubscribeAll();
|
|
2225
2762
|
this.detachAllSignals();
|
|
@@ -2237,6 +2774,18 @@ var Task = class extends SignalEmitter {
|
|
|
2237
2774
|
});
|
|
2238
2775
|
}
|
|
2239
2776
|
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Exports the current state of the object as a structured plain object.
|
|
2779
|
+
*
|
|
2780
|
+
* @return {AnyObject} An object containing the serialized properties of the current instance. The exported object includes various metadata, schema information, task attributes, and related tasks, such as:
|
|
2781
|
+
* - Name and description of the task.
|
|
2782
|
+
* - Layer index, uniqueness, meta, and signal-related flags.
|
|
2783
|
+
* - Event triggers and attached signals.
|
|
2784
|
+
* - Throttling, concurrency, timeout settings, and ephemeral flag.
|
|
2785
|
+
* - Task function as a string.
|
|
2786
|
+
* - Serialization of getter callbacks and schemas for input/output validation.
|
|
2787
|
+
* - Relationships such as next tasks, failure tasks, and predecessor tasks.
|
|
2788
|
+
*/
|
|
2240
2789
|
export() {
|
|
2241
2790
|
return {
|
|
2242
2791
|
__name: this.name,
|
|
@@ -2263,9 +2812,20 @@ var Task = class extends SignalEmitter {
|
|
|
2263
2812
|
__previousTasks: Array.from(this.predecessorTasks).map((t) => t.name)
|
|
2264
2813
|
};
|
|
2265
2814
|
}
|
|
2815
|
+
/**
|
|
2816
|
+
* Returns an iterator for iterating over tasks associated with this instance.
|
|
2817
|
+
*
|
|
2818
|
+
* @return {TaskIterator} An iterator instance for tasks.
|
|
2819
|
+
*/
|
|
2266
2820
|
getIterator() {
|
|
2267
2821
|
return new TaskIterator(this);
|
|
2268
2822
|
}
|
|
2823
|
+
/**
|
|
2824
|
+
* Accepts a visitor object to perform operations on the current instance.
|
|
2825
|
+
*
|
|
2826
|
+
* @param {GraphVisitor} visitor - The visitor object implementing operations for this instance.
|
|
2827
|
+
* @return {void} This method does not return a value.
|
|
2828
|
+
*/
|
|
2269
2829
|
accept(visitor) {
|
|
2270
2830
|
visitor.visitTask(this);
|
|
2271
2831
|
}
|
|
@@ -2276,6 +2836,16 @@ var Task = class extends SignalEmitter {
|
|
|
2276
2836
|
|
|
2277
2837
|
// src/registry/GraphRegistry.ts
|
|
2278
2838
|
var GraphRegistry = class _GraphRegistry {
|
|
2839
|
+
/**
|
|
2840
|
+
* Constructs a new instance and sets up various meta tasks and routines.
|
|
2841
|
+
*
|
|
2842
|
+
* This constructor initializes several predefined tasks for managing operations
|
|
2843
|
+
* like registering tasks, updating schemas for tasks, fetching tasks or routines,
|
|
2844
|
+
* and performing actions on all tasks or routines. It also initializes routines
|
|
2845
|
+
* to handle similar operations and hardcodes the initial meta tasks and routines.
|
|
2846
|
+
*
|
|
2847
|
+
* It initializes the instance state by setting up tasks and routines.
|
|
2848
|
+
*/
|
|
2279
2849
|
constructor() {
|
|
2280
2850
|
this.tasks = /* @__PURE__ */ new Map();
|
|
2281
2851
|
this.routines = /* @__PURE__ */ new Map();
|
|
@@ -2460,12 +3030,14 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
2460
3030
|
);
|
|
2461
3031
|
}
|
|
2462
3032
|
/**
|
|
2463
|
-
* Adds tasks
|
|
2464
|
-
*
|
|
2465
|
-
*
|
|
2466
|
-
* @
|
|
2467
|
-
*
|
|
2468
|
-
* @
|
|
3033
|
+
* Adds tasks or routines to the current execution pipeline. Supports both individual tasks,
|
|
3034
|
+
* routines, or arrays of tasks and routines. Handles metadata and execution context management.
|
|
3035
|
+
*
|
|
3036
|
+
* @param {Task|GraphRoutine|(Task|GraphRoutine)[]} tasks - The task(s) or routine(s) to be added.
|
|
3037
|
+
* It can be a single task, a single routine, or an array of tasks and routines.
|
|
3038
|
+
* @param {AnyObject} [context={}] - Optional context object to provide execution trace and metadata.
|
|
3039
|
+
* Used to propagate information across task or routine executions.
|
|
3040
|
+
* @return {void} - This method does not return a value.
|
|
2469
3041
|
*/
|
|
2470
3042
|
addTasks(tasks, context = {}) {
|
|
2471
3043
|
let _tasks = Array.isArray(tasks) ? tasks : [tasks];
|
|
@@ -2490,9 +3062,9 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
2490
3062
|
const isSubMeta = allTasks.some((t) => t.isSubMeta) || !!context.__isSubMeta;
|
|
2491
3063
|
context.__isSubMeta = isSubMeta;
|
|
2492
3064
|
const isNewTrace = !context.__routineExecId && !context.__metadata?.__executionTraceId && !context.__executionTraceId;
|
|
2493
|
-
const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? (0,
|
|
3065
|
+
const executionTraceId = context.__metadata?.__executionTraceId ?? context.__executionTraceId ?? (0, import_uuid5.v4)();
|
|
2494
3066
|
context.__executionTraceId = executionTraceId;
|
|
2495
|
-
const routineExecId = context.__routineExecId ?? (0,
|
|
3067
|
+
const routineExecId = context.__routineExecId ?? (0, import_uuid5.v4)();
|
|
2496
3068
|
context.__routineExecId = routineExecId;
|
|
2497
3069
|
const ctx = new GraphContext(context || {});
|
|
2498
3070
|
if (!isSubMeta) {
|
|
@@ -2538,11 +3110,12 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
2538
3110
|
);
|
|
2539
3111
|
}
|
|
2540
3112
|
/**
|
|
2541
|
-
*
|
|
2542
|
-
*
|
|
2543
|
-
*
|
|
2544
|
-
* @
|
|
2545
|
-
* @
|
|
3113
|
+
* Executes the provided tasks or routines. Maintains the execution state
|
|
3114
|
+
* and handles synchronous or asynchronous processing.
|
|
3115
|
+
*
|
|
3116
|
+
* @param {Task|GraphRoutine|(Task|GraphRoutine)[]} [tasks] - A single task, a single routine, or an array of tasks or routines to execute. Optional.
|
|
3117
|
+
* @param {AnyObject} [context] - An optional context object to be used during task execution.
|
|
3118
|
+
* @return {GraphRun|Promise<GraphRun>} - Returns a `GraphRun` instance if the execution is synchronous, or a `Promise` resolving to a `GraphRun` for asynchronous execution.
|
|
2546
3119
|
*/
|
|
2547
3120
|
run(tasks, context) {
|
|
2548
3121
|
if (tasks) {
|
|
@@ -2560,10 +3133,23 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
2560
3133
|
}
|
|
2561
3134
|
return this.reset();
|
|
2562
3135
|
}
|
|
3136
|
+
/**
|
|
3137
|
+
* Executes the provided asynchronous operation and resets the state afterwards.
|
|
3138
|
+
*
|
|
3139
|
+
* @param {Promise<void>} run - A promise representing the asynchronous operation to execute.
|
|
3140
|
+
* @return {Promise<GraphRun>} A promise that resolves to the result of the reset operation after the asynchronous operation completes.
|
|
3141
|
+
*/
|
|
2563
3142
|
async runAsync(run) {
|
|
2564
3143
|
await run;
|
|
2565
3144
|
return this.reset();
|
|
2566
3145
|
}
|
|
3146
|
+
/**
|
|
3147
|
+
* Resets the current state of the graph, creating a new GraphRun instance
|
|
3148
|
+
* and returning the previous run instance.
|
|
3149
|
+
* If the debug mode is not enabled, it will destroy the existing resources.
|
|
3150
|
+
*
|
|
3151
|
+
* @return {GraphRun} The last GraphRun instance before the reset.
|
|
3152
|
+
*/
|
|
2567
3153
|
reset() {
|
|
2568
3154
|
this.isRunning = false;
|
|
2569
3155
|
const lastRun = this.currentRun;
|
|
@@ -2582,6 +3168,13 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
2582
3168
|
destroy() {
|
|
2583
3169
|
this.currentRun.destroy();
|
|
2584
3170
|
}
|
|
3171
|
+
/**
|
|
3172
|
+
* Sets the strategy to be used for running the graph and initializes
|
|
3173
|
+
* the current run with the provided strategy if no process is currently running.
|
|
3174
|
+
*
|
|
3175
|
+
* @param {GraphRunStrategy} strategy - The strategy to use for running the graph.
|
|
3176
|
+
* @return {void}
|
|
3177
|
+
*/
|
|
2585
3178
|
setStrategy(strategy) {
|
|
2586
3179
|
this.strategy = strategy;
|
|
2587
3180
|
if (!this.isRunning) {
|
|
@@ -2641,6 +3234,14 @@ var DebounceTask = class extends Task {
|
|
|
2641
3234
|
this.trailing = trailing;
|
|
2642
3235
|
this.maxWait = maxWait;
|
|
2643
3236
|
}
|
|
3237
|
+
/**
|
|
3238
|
+
* Executes the taskFunction with the provided context, emit function, and progress callback.
|
|
3239
|
+
* It clears any existing timeout before execution.
|
|
3240
|
+
* Handles synchronous and asynchronous results from taskFunction.
|
|
3241
|
+
* If an error occurs during execution, it resolves with the error.
|
|
3242
|
+
*
|
|
3243
|
+
* @return {void} This method does not return any value.
|
|
3244
|
+
*/
|
|
2644
3245
|
executeFunction() {
|
|
2645
3246
|
if (this.lastTimeout) {
|
|
2646
3247
|
clearTimeout(this.lastTimeout);
|
|
@@ -2666,6 +3267,19 @@ var DebounceTask = class extends Task {
|
|
|
2666
3267
|
}
|
|
2667
3268
|
}
|
|
2668
3269
|
}
|
|
3270
|
+
/**
|
|
3271
|
+
* Executes a debounced operation, ensuring controlled execution of functions
|
|
3272
|
+
* over a specified debounce time and maximum wait time. This method handles
|
|
3273
|
+
* both leading and trailing edge executions and invokes callbacks accordingly.
|
|
3274
|
+
*
|
|
3275
|
+
* @param {Function} resolve - The function to call when the operation is successfully resolved.
|
|
3276
|
+
* @param {Function} reject - The function to call with an error or reason if the operation fails.
|
|
3277
|
+
* @param {GraphContext} context - The execution context for the operation.
|
|
3278
|
+
* @param {NodeJS.Timeout} timeout - A timeout object for managing execution delays.
|
|
3279
|
+
* @param {Function} emit - A callback function to emit signals with a specific context.
|
|
3280
|
+
* @param {Function} progressCallback - A callback function to report progress during operation execution.
|
|
3281
|
+
* @return {void} Does not return a value but sets internal timers and invokes provided callbacks.
|
|
3282
|
+
*/
|
|
2669
3283
|
debouncedTrigger(resolve, reject, context, timeout, emit, progressCallback) {
|
|
2670
3284
|
const callNow = this.leading && this.timer === null;
|
|
2671
3285
|
const isNewBurst = this.timer === null;
|
|
@@ -2711,6 +3325,14 @@ var DebounceTask = class extends Task {
|
|
|
2711
3325
|
}, this.maxWait);
|
|
2712
3326
|
}
|
|
2713
3327
|
}
|
|
3328
|
+
/**
|
|
3329
|
+
* Executes a task with a debounced trigger mechanism.
|
|
3330
|
+
*
|
|
3331
|
+
* @param {GraphContext} context - The context containing relevant graph data for the execution.
|
|
3332
|
+
* @param {function(string, any): void} emit - A function used to emit signals with associated context.
|
|
3333
|
+
* @param {function(number): void} progressCallback - A callback function to report the progress of the task as a number between 0 and 1.
|
|
3334
|
+
* @return {Promise<TaskResult>} A promise that resolves with the task result upon completion or rejects on failure.
|
|
3335
|
+
*/
|
|
2714
3336
|
execute(context, emit, progressCallback) {
|
|
2715
3337
|
return new Promise((resolve, reject) => {
|
|
2716
3338
|
const timeout = setTimeout(() => {
|
|
@@ -2756,6 +3378,15 @@ var EphemeralTask = class extends Task {
|
|
|
2756
3378
|
this.once = once;
|
|
2757
3379
|
this.condition = condition;
|
|
2758
3380
|
}
|
|
3381
|
+
/**
|
|
3382
|
+
* Executes the process logic with the provided context, emit function, progress callback, and node data.
|
|
3383
|
+
*
|
|
3384
|
+
* @param {any} context - The execution context, carrying necessary parameters or states for the operation.
|
|
3385
|
+
* @param {function(string, AnyObject): void} emit - A function to emit signals with a string identifier and associated context.
|
|
3386
|
+
* @param {function(number): void} progressCallback - A callback function to report the progress of the execution as a numerical value.
|
|
3387
|
+
* @param {{ nodeId: string, routineExecId: string }} nodeData - An object containing details about the node ID and routine execution ID.
|
|
3388
|
+
* @return {any} The result of the execution, returned from the base implementation or processed internally.
|
|
3389
|
+
*/
|
|
2759
3390
|
execute(context, emit, progressCallback, nodeData) {
|
|
2760
3391
|
const result = super.execute(context, emit, progressCallback, nodeData);
|
|
2761
3392
|
if (this.once || this.condition(result)) {
|
|
@@ -2849,26 +3480,56 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2849
3480
|
this.debug = false;
|
|
2850
3481
|
this.index = index;
|
|
2851
3482
|
}
|
|
3483
|
+
/**
|
|
3484
|
+
* Sets the debug mode for the current instance and all associated nodes.
|
|
3485
|
+
*
|
|
3486
|
+
* @param {boolean} value - A boolean value to enable (true) or disable (false) debug mode.
|
|
3487
|
+
* @return {void} No return value.
|
|
3488
|
+
*/
|
|
2852
3489
|
setDebug(value) {
|
|
2853
3490
|
this.debug = value;
|
|
2854
3491
|
for (const node of this.nodes) {
|
|
2855
3492
|
node.setDebug(value);
|
|
2856
3493
|
}
|
|
2857
3494
|
}
|
|
3495
|
+
/**
|
|
3496
|
+
* Checks if the current layer has a preceding layer.
|
|
3497
|
+
*
|
|
3498
|
+
* @return {boolean} True if the current layer has a preceding layer that is an instance of GraphLayer; otherwise, false.
|
|
3499
|
+
*/
|
|
2858
3500
|
get hasPreceding() {
|
|
2859
3501
|
return !!this.previous && this.previous instanceof _GraphLayer;
|
|
2860
3502
|
}
|
|
2861
3503
|
getNumberOfNodes() {
|
|
2862
3504
|
return this.nodes.length;
|
|
2863
3505
|
}
|
|
3506
|
+
/**
|
|
3507
|
+
* Retrieves a list of nodes that match the given routine execution ID.
|
|
3508
|
+
*
|
|
3509
|
+
* @param {string} routineExecId - The ID of the routine execution to filter nodes by.
|
|
3510
|
+
* @return {Array} An array of nodes that have the specified routine execution ID.
|
|
3511
|
+
*/
|
|
2864
3512
|
getNodesByRoutineExecId(routineExecId) {
|
|
2865
3513
|
return this.nodes.filter((node) => node.routineExecId === routineExecId);
|
|
2866
3514
|
}
|
|
3515
|
+
/**
|
|
3516
|
+
* Finds and returns all nodes in the graph that are identical to the given node.
|
|
3517
|
+
* Two nodes are considered identical if they share the same routine execution ID
|
|
3518
|
+
* and share a task with each other.
|
|
3519
|
+
*
|
|
3520
|
+
* @param {GraphNode} node - The reference node to compare against other nodes in the graph.
|
|
3521
|
+
* @return {GraphNode[]} An array of nodes that are identical to the given node.
|
|
3522
|
+
*/
|
|
2867
3523
|
getIdenticalNodes(node) {
|
|
2868
3524
|
return this.nodes.filter(
|
|
2869
3525
|
(n) => node.routineExecId === n.routineExecId && node.sharesTaskWith(n)
|
|
2870
3526
|
);
|
|
2871
3527
|
}
|
|
3528
|
+
/**
|
|
3529
|
+
* Checks whether all nodes in the collection have been processed.
|
|
3530
|
+
*
|
|
3531
|
+
* @return {boolean} Returns true if all nodes are processed, otherwise false.
|
|
3532
|
+
*/
|
|
2872
3533
|
isProcessed() {
|
|
2873
3534
|
for (const node of this.nodes) {
|
|
2874
3535
|
if (!node.isProcessed()) {
|
|
@@ -2877,6 +3538,11 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2877
3538
|
}
|
|
2878
3539
|
return true;
|
|
2879
3540
|
}
|
|
3541
|
+
/**
|
|
3542
|
+
* Checks whether all layers in the graph have been processed.
|
|
3543
|
+
*
|
|
3544
|
+
* @return {boolean} Returns true if all graph layers are processed; otherwise, returns false.
|
|
3545
|
+
*/
|
|
2880
3546
|
graphDone() {
|
|
2881
3547
|
let done = true;
|
|
2882
3548
|
let layer = this;
|
|
@@ -2889,6 +3555,13 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2889
3555
|
}
|
|
2890
3556
|
return done;
|
|
2891
3557
|
}
|
|
3558
|
+
/**
|
|
3559
|
+
* Sets the next GraphLayer in the sequence if it has a higher index than the current layer.
|
|
3560
|
+
* Updates the previous property if the given next layer has an existing previous layer.
|
|
3561
|
+
*
|
|
3562
|
+
* @param {GraphLayer} next - The next GraphLayer to be linked in the sequence.
|
|
3563
|
+
* @return {void} Does not return a value. Modifies the current layer's state.
|
|
3564
|
+
*/
|
|
2892
3565
|
setNext(next) {
|
|
2893
3566
|
if (next.index <= this.index) {
|
|
2894
3567
|
return;
|
|
@@ -2898,15 +3571,32 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2898
3571
|
}
|
|
2899
3572
|
super.setNext(next);
|
|
2900
3573
|
}
|
|
3574
|
+
/**
|
|
3575
|
+
* Adds a node to the graph.
|
|
3576
|
+
*
|
|
3577
|
+
* @param {GraphNode} node - The node to be added to the graph.
|
|
3578
|
+
* @return {void}
|
|
3579
|
+
*/
|
|
2901
3580
|
add(node) {
|
|
2902
3581
|
this.nodes.push(node);
|
|
2903
3582
|
}
|
|
3583
|
+
/**
|
|
3584
|
+
* Starts the execution timer if it has not been started already.
|
|
3585
|
+
* Records the current timestamp as the start time.
|
|
3586
|
+
*
|
|
3587
|
+
* @return {number} The timestamp representing the start time in milliseconds.
|
|
3588
|
+
*/
|
|
2904
3589
|
start() {
|
|
2905
3590
|
if (!this.executionStart) {
|
|
2906
3591
|
this.executionStart = Date.now();
|
|
2907
3592
|
}
|
|
2908
3593
|
return this.executionStart;
|
|
2909
3594
|
}
|
|
3595
|
+
/**
|
|
3596
|
+
* Marks the end of a process by capturing the current timestamp and calculating the execution time if a start time exists.
|
|
3597
|
+
*
|
|
3598
|
+
* @return {number} The timestamp at which the process ended, or 0 if the start time is not defined.
|
|
3599
|
+
*/
|
|
2910
3600
|
end() {
|
|
2911
3601
|
if (!this.executionStart) {
|
|
2912
3602
|
return 0;
|
|
@@ -2915,6 +3605,14 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2915
3605
|
this.executionTime = end - this.executionStart;
|
|
2916
3606
|
return end;
|
|
2917
3607
|
}
|
|
3608
|
+
/**
|
|
3609
|
+
* Destroys the current graph layer and its associated resources.
|
|
3610
|
+
* This method recursively destroys all nodes in the current layer, clears the node list,
|
|
3611
|
+
* and ensures that any connected subsequent graph layers are also destroyed.
|
|
3612
|
+
* Additionally, it calls the decoupling logic to disconnect the current layer from its dependencies.
|
|
3613
|
+
*
|
|
3614
|
+
* @return {void} Does not return any value.
|
|
3615
|
+
*/
|
|
2918
3616
|
destroy() {
|
|
2919
3617
|
for (const node of this.nodes) {
|
|
2920
3618
|
node.destroy();
|
|
@@ -2926,9 +3624,20 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2926
3624
|
}
|
|
2927
3625
|
this.decouple();
|
|
2928
3626
|
}
|
|
3627
|
+
/**
|
|
3628
|
+
* Returns an iterator for traversing through the graph layers.
|
|
3629
|
+
*
|
|
3630
|
+
* @return {GraphLayerIterator} An instance of GraphLayerIterator to traverse graph layers.
|
|
3631
|
+
*/
|
|
2929
3632
|
getIterator() {
|
|
2930
3633
|
return new GraphLayerIterator(this);
|
|
2931
3634
|
}
|
|
3635
|
+
/**
|
|
3636
|
+
* Accepts a visitor object to traverse or perform operations on the current graph layer and its nodes.
|
|
3637
|
+
*
|
|
3638
|
+
* @param {GraphVisitor} visitor - The visitor instance implementing the visitLayer and visitNode behavior.
|
|
3639
|
+
* @return {void} Returns nothing.
|
|
3640
|
+
*/
|
|
2932
3641
|
accept(visitor) {
|
|
2933
3642
|
visitor.visitLayer(this);
|
|
2934
3643
|
for (const node of this.nodes) {
|
|
@@ -2965,6 +3674,14 @@ var GraphLayer = class _GraphLayer extends ExecutionChain {
|
|
|
2965
3674
|
|
|
2966
3675
|
// src/graph/execution/SyncGraphLayer.ts
|
|
2967
3676
|
var SyncGraphLayer = class extends GraphLayer {
|
|
3677
|
+
/**
|
|
3678
|
+
* Executes the processing logic of the current set of graph nodes. Iterates through all
|
|
3679
|
+
* nodes, skipping any that have already been processed, and executes their respective
|
|
3680
|
+
* logic to generate new nodes. Asynchronous functions are not supported and will
|
|
3681
|
+
* trigger an error log.
|
|
3682
|
+
*
|
|
3683
|
+
* @return {GraphNode[]} An array of newly generated graph nodes after executing the logic of each unprocessed node.
|
|
3684
|
+
*/
|
|
2968
3685
|
execute() {
|
|
2969
3686
|
this.start();
|
|
2970
3687
|
const result = [];
|
|
@@ -2997,20 +3714,49 @@ var GraphBuilder = class {
|
|
|
2997
3714
|
getResult() {
|
|
2998
3715
|
return this.graph;
|
|
2999
3716
|
}
|
|
3717
|
+
/**
|
|
3718
|
+
* Composes a series of functions or operations.
|
|
3719
|
+
* This method should be implemented in the child class
|
|
3720
|
+
* to define custom composition logic.
|
|
3721
|
+
*
|
|
3722
|
+
* @return {any} The result of the composed operations or functions
|
|
3723
|
+
* when implemented in the child class.
|
|
3724
|
+
*/
|
|
3000
3725
|
compose() {
|
|
3001
3726
|
throw "Implement this in child class...";
|
|
3002
3727
|
}
|
|
3728
|
+
/**
|
|
3729
|
+
* Adds a node to the appropriate layer of the graph.
|
|
3730
|
+
*
|
|
3731
|
+
* @param {GraphNode} node - The node to be added to the graph. The node contains
|
|
3732
|
+
* layer information that determines which layer it belongs to.
|
|
3733
|
+
* @return {void} Does not return a value.
|
|
3734
|
+
*/
|
|
3003
3735
|
addNode(node) {
|
|
3004
3736
|
const index = node.getLayerIndex();
|
|
3005
3737
|
this.addLayer(index);
|
|
3006
3738
|
const layer = this.getLayer(index);
|
|
3007
3739
|
node.scheduleOn(layer);
|
|
3008
3740
|
}
|
|
3741
|
+
/**
|
|
3742
|
+
* Adds multiple nodes to the graph.
|
|
3743
|
+
*
|
|
3744
|
+
* @param {GraphNode[]} nodes - An array of nodes to be added to the graph.
|
|
3745
|
+
* @return {void} This method does not return a value.
|
|
3746
|
+
*/
|
|
3009
3747
|
addNodes(nodes) {
|
|
3010
3748
|
for (const node of nodes) {
|
|
3011
3749
|
this.addNode(node);
|
|
3012
3750
|
}
|
|
3013
3751
|
}
|
|
3752
|
+
/**
|
|
3753
|
+
* Adds a new layer to the graph at the specified index. If the graph does not exist,
|
|
3754
|
+
* it creates the graph using the specified index. Updates the graph's top layer index
|
|
3755
|
+
* and maintains the order of layers.
|
|
3756
|
+
*
|
|
3757
|
+
* @param {number} index - The index at which the new layer should be added to the graph.
|
|
3758
|
+
* @return {void} This method does not return a value.
|
|
3759
|
+
*/
|
|
3014
3760
|
addLayer(index) {
|
|
3015
3761
|
if (!this.graph) {
|
|
3016
3762
|
const layer = this.createLayer(index);
|
|
@@ -3037,11 +3783,23 @@ var GraphBuilder = class {
|
|
|
3037
3783
|
this.addLayer(index);
|
|
3038
3784
|
}
|
|
3039
3785
|
}
|
|
3786
|
+
/**
|
|
3787
|
+
* Creates a new layer for the graph at the specified index.
|
|
3788
|
+
*
|
|
3789
|
+
* @param {number} index - The index of the layer to be created.
|
|
3790
|
+
* @return {GraphLayer} A new instance of the graph layer corresponding to the provided index.
|
|
3791
|
+
*/
|
|
3040
3792
|
createLayer(index) {
|
|
3041
3793
|
const layer = new SyncGraphLayer(index);
|
|
3042
3794
|
layer.setDebug(this.debug);
|
|
3043
3795
|
return layer;
|
|
3044
3796
|
}
|
|
3797
|
+
/**
|
|
3798
|
+
* Retrieves a specific layer from the current set of layers.
|
|
3799
|
+
*
|
|
3800
|
+
* @param {number} layerIndex - The index of the layer to retrieve.
|
|
3801
|
+
* @return {*} The layer corresponding to the given index.
|
|
3802
|
+
*/
|
|
3045
3803
|
getLayer(layerIndex) {
|
|
3046
3804
|
return this.layers[layerIndex - this.topLayerIndex];
|
|
3047
3805
|
}
|
|
@@ -3054,6 +3812,12 @@ var GraphBuilder = class {
|
|
|
3054
3812
|
|
|
3055
3813
|
// src/engine/builders/GraphBreadthFirstBuilder.ts
|
|
3056
3814
|
var GraphBreadthFirstBuilder = class extends GraphBuilder {
|
|
3815
|
+
/**
|
|
3816
|
+
* Composes layers of a graph by iterating through them, executing their logic,
|
|
3817
|
+
* and adding the resulting nodes to the current graph.
|
|
3818
|
+
*
|
|
3819
|
+
* @return {void} This method does not return a value.
|
|
3820
|
+
*/
|
|
3057
3821
|
compose() {
|
|
3058
3822
|
if (!this.graph) {
|
|
3059
3823
|
return;
|
|
@@ -3109,6 +3873,15 @@ var ThrottleEngine = class _ThrottleEngine {
|
|
|
3109
3873
|
setConcurrencyLimit(tag, limit) {
|
|
3110
3874
|
this.maxConcurrencyPerTag[tag] = limit;
|
|
3111
3875
|
}
|
|
3876
|
+
/**
|
|
3877
|
+
* Manages the execution of a function `fn` applied on a specified node `node` with controlled concurrency for a given tag.
|
|
3878
|
+
* The method ensures that processes are executed in a throttled manner, respecting the maximum concurrency for each tag.
|
|
3879
|
+
*
|
|
3880
|
+
* @param {ProcessFunction} fn - The function to be executed on the provided node.
|
|
3881
|
+
* @param {GraphNode} node - The graph node on which the function `fn` will be applied.
|
|
3882
|
+
* @param {string} [tag="default"] - The concurrency grouping tag used to control and group the throttling behavior.
|
|
3883
|
+
* @return {Promise<GraphNode[]>} A promise resolving to an array of GraphNode objects once the throttled function execution completes.
|
|
3884
|
+
*/
|
|
3112
3885
|
throttle(fn, node, tag = "default") {
|
|
3113
3886
|
var _a, _b;
|
|
3114
3887
|
const functionPromise = new Promise((resolve) => {
|
|
@@ -3120,6 +3893,12 @@ var ThrottleEngine = class _ThrottleEngine {
|
|
|
3120
3893
|
this.processQueue(tag);
|
|
3121
3894
|
return functionPromise;
|
|
3122
3895
|
}
|
|
3896
|
+
/**
|
|
3897
|
+
* Processes the tasks in the queue for a given tag while respecting concurrency limits.
|
|
3898
|
+
*
|
|
3899
|
+
* @param {string} tag - The identifier for the queue to be processed, used to group tasks and manage concurrency controls.
|
|
3900
|
+
* @return {void} Does not return a value; it processes tasks asynchronously and manages state internally.
|
|
3901
|
+
*/
|
|
3123
3902
|
processQueue(tag) {
|
|
3124
3903
|
const maxAllowed = this.maxConcurrencyPerTag[tag];
|
|
3125
3904
|
while ((this.queues[tag]?.length ?? 0) > 0 && (this.runningCounts[tag] ?? 0) < maxAllowed) {
|
|
@@ -3135,6 +3914,14 @@ var ThrottleEngine = class _ThrottleEngine {
|
|
|
3135
3914
|
delete this.runningCounts[tag];
|
|
3136
3915
|
}
|
|
3137
3916
|
}
|
|
3917
|
+
/**
|
|
3918
|
+
* Processes a given item consisting of a function and a graph node.
|
|
3919
|
+
*
|
|
3920
|
+
* @param {Array} item - An array where the first element is a processing function and the second element is a graph node.
|
|
3921
|
+
* @param {Function} item[0] - The function to process the graph node.
|
|
3922
|
+
* @param {GraphNode} item[1] - The graph node to be processed.
|
|
3923
|
+
* @return {Promise<void>} A promise that resolves when the processing and cleanup are complete.
|
|
3924
|
+
*/
|
|
3138
3925
|
async process(item) {
|
|
3139
3926
|
const fn = item[0];
|
|
3140
3927
|
const node = item[1];
|
|
@@ -3151,10 +3938,24 @@ var AsyncGraphLayer = class extends GraphLayer {
|
|
|
3151
3938
|
this.waitingNodes = [];
|
|
3152
3939
|
this.processingNodes = /* @__PURE__ */ new Set();
|
|
3153
3940
|
}
|
|
3941
|
+
/**
|
|
3942
|
+
* Adds a node to the graph and tracks it as a waiting node.
|
|
3943
|
+
*
|
|
3944
|
+
* @param {GraphNode} node - The graph node to be added.
|
|
3945
|
+
* @return {void}
|
|
3946
|
+
*/
|
|
3154
3947
|
add(node) {
|
|
3155
3948
|
this.nodes.push(node);
|
|
3156
3949
|
this.waitingNodes.push(node);
|
|
3157
3950
|
}
|
|
3951
|
+
/**
|
|
3952
|
+
* Executes the processing of nodes by iterating over the queued `waitingNodes`,
|
|
3953
|
+
* processing each node, and managing concurrency limits where applicable.
|
|
3954
|
+
* The method returns a mapping of routine execution IDs to arrays of processed nodes or promises.
|
|
3955
|
+
*
|
|
3956
|
+
* @return {Object} An object where the keys are routine execution IDs and the values
|
|
3957
|
+
* represent arrays of processed nodes or promises resolving to processed nodes.
|
|
3958
|
+
*/
|
|
3158
3959
|
execute() {
|
|
3159
3960
|
var _a;
|
|
3160
3961
|
if (this.waitingNodes.length === 0) {
|
|
@@ -3188,6 +3989,13 @@ var AsyncGraphLayer = class extends GraphLayer {
|
|
|
3188
3989
|
}
|
|
3189
3990
|
return result;
|
|
3190
3991
|
}
|
|
3992
|
+
/**
|
|
3993
|
+
* Processes the given graph node, executes its logic, and handles synchronous or asynchronous outcomes.
|
|
3994
|
+
*
|
|
3995
|
+
* @param {GraphNode} node - The graph node to be processed.
|
|
3996
|
+
* @return {Promise<GraphNode[]> | GraphNode[]} A promise that resolves to an array of next graph nodes if asynchronous,
|
|
3997
|
+
* or an array of next graph nodes if synchronous.
|
|
3998
|
+
*/
|
|
3191
3999
|
processNode(node) {
|
|
3192
4000
|
node.start();
|
|
3193
4001
|
const nextNodes = node.execute();
|
|
@@ -3197,11 +4005,23 @@ var AsyncGraphLayer = class extends GraphLayer {
|
|
|
3197
4005
|
this.processingNodes.delete(node);
|
|
3198
4006
|
return nextNodes;
|
|
3199
4007
|
}
|
|
4008
|
+
/**
|
|
4009
|
+
* Processes the given graph node asynchronously and removes it from the processing nodes set.
|
|
4010
|
+
*
|
|
4011
|
+
* @param {GraphNode} node - The current graph node being processed.
|
|
4012
|
+
* @param {Promise<GraphNode[]>} nextNodes - A promise that resolves to an array of the next graph nodes to process.
|
|
4013
|
+
* @return {Promise<GraphNode[]>} A promise that resolves to an array of the next graph nodes.
|
|
4014
|
+
*/
|
|
3200
4015
|
async processAsync(node, nextNodes) {
|
|
3201
4016
|
const result = await nextNodes;
|
|
3202
4017
|
this.processingNodes.delete(node);
|
|
3203
4018
|
return result;
|
|
3204
4019
|
}
|
|
4020
|
+
/**
|
|
4021
|
+
* Cleans up resources used by the instance by resetting relevant properties and invoking the parent class's destroy method.
|
|
4022
|
+
*
|
|
4023
|
+
* @return {void} No value is returned as the method performs cleanup operations.
|
|
4024
|
+
*/
|
|
3205
4025
|
destroy() {
|
|
3206
4026
|
super.destroy();
|
|
3207
4027
|
this.waitingNodes = [];
|
|
@@ -3211,6 +4031,14 @@ var AsyncGraphLayer = class extends GraphLayer {
|
|
|
3211
4031
|
|
|
3212
4032
|
// src/engine/builders/GraphAsyncQueueBuilder.ts
|
|
3213
4033
|
var GraphAsyncQueueBuilder = class extends GraphBuilder {
|
|
4034
|
+
/**
|
|
4035
|
+
* This method iterates over a graph structure and processes its layers sequentially.
|
|
4036
|
+
* It continues processing each layer until all layers in the graph are completed.
|
|
4037
|
+
* The asynchronous behavior ensures the operation provides breathing room for other
|
|
4038
|
+
* tasks/processes to execute during its operation.
|
|
4039
|
+
*
|
|
4040
|
+
* @return {Promise<void>} A promise that resolves when all layers of the graph are processed or rejects if an error occurs.
|
|
4041
|
+
*/
|
|
3214
4042
|
async compose() {
|
|
3215
4043
|
if (!this.graph) {
|
|
3216
4044
|
return;
|
|
@@ -3229,6 +4057,14 @@ var GraphAsyncQueueBuilder = class extends GraphBuilder {
|
|
|
3229
4057
|
await sleep(0);
|
|
3230
4058
|
}
|
|
3231
4059
|
}
|
|
4060
|
+
/**
|
|
4061
|
+
* Processes a given asynchronous graph layer and executes its nodes.
|
|
4062
|
+
* Handles promises within the nodes and adds the resolved or processed
|
|
4063
|
+
* nodes to the graph.
|
|
4064
|
+
*
|
|
4065
|
+
* @param {AsyncGraphLayer} layer - The asynchronous graph layer to be processed.
|
|
4066
|
+
* @return {void} - This method does not return a value.
|
|
4067
|
+
*/
|
|
3232
4068
|
processLayer(layer) {
|
|
3233
4069
|
const nextNodes = layer.execute();
|
|
3234
4070
|
for (const routineExecId of Object.keys(nextNodes)) {
|
|
@@ -3242,6 +4078,13 @@ var GraphAsyncQueueBuilder = class extends GraphBuilder {
|
|
|
3242
4078
|
}
|
|
3243
4079
|
}
|
|
3244
4080
|
}
|
|
4081
|
+
/**
|
|
4082
|
+
* Creates a new instance of AsyncGraphLayer, sets its debug configuration,
|
|
4083
|
+
* and returns the created layer.
|
|
4084
|
+
*
|
|
4085
|
+
* @param {number} index - The index to associate with the new AsyncGraphLayer.
|
|
4086
|
+
* @return {AsyncGraphLayer} A new instance of AsyncGraphLayer with the specified index and debug configuration.
|
|
4087
|
+
*/
|
|
3245
4088
|
createLayer(index) {
|
|
3246
4089
|
const layer = new AsyncGraphLayer(index);
|
|
3247
4090
|
layer.setDebug(this.debug);
|
|
@@ -3255,6 +4098,12 @@ var GraphAsyncRun = class extends GraphRunStrategy {
|
|
|
3255
4098
|
super();
|
|
3256
4099
|
this.graphBuilder = new GraphAsyncQueueBuilder();
|
|
3257
4100
|
}
|
|
4101
|
+
/**
|
|
4102
|
+
* Executes the run operation, which involves composing the graph builder,
|
|
4103
|
+
* updating the run instance, and resetting the state.
|
|
4104
|
+
*
|
|
4105
|
+
* @return {Promise<void>} A promise that resolves when the operation completes.
|
|
4106
|
+
*/
|
|
3258
4107
|
async run() {
|
|
3259
4108
|
await this.graphBuilder.compose();
|
|
3260
4109
|
this.updateRunInstance();
|
|
@@ -3270,6 +4119,12 @@ var GraphAsyncRun = class extends GraphRunStrategy {
|
|
|
3270
4119
|
|
|
3271
4120
|
// src/engine/strategy/GraphStandardRun.ts
|
|
3272
4121
|
var GraphStandardRun = class extends GraphRunStrategy {
|
|
4122
|
+
/**
|
|
4123
|
+
* Executes the sequence of operations involving graph composition,
|
|
4124
|
+
* instance updating, and reset mechanisms.
|
|
4125
|
+
*
|
|
4126
|
+
* @return {void} Does not return a value.
|
|
4127
|
+
*/
|
|
3273
4128
|
run() {
|
|
3274
4129
|
this.graphBuilder.compose();
|
|
3275
4130
|
this.updateRunInstance();
|
|
@@ -3282,6 +4137,13 @@ var GraphStandardRun = class extends GraphRunStrategy {
|
|
|
3282
4137
|
|
|
3283
4138
|
// src/Cadenza.ts
|
|
3284
4139
|
var Cadenza = class {
|
|
4140
|
+
/**
|
|
4141
|
+
* Initializes the system by setting up the required components such as the
|
|
4142
|
+
* signal broker, runners, and graph registry. Ensures the initialization
|
|
4143
|
+
* happens only once. Configures debug settings if applicable.
|
|
4144
|
+
*
|
|
4145
|
+
* @return {void} No value is returned.
|
|
4146
|
+
*/
|
|
3285
4147
|
static bootstrap() {
|
|
3286
4148
|
if (this.isBootstrapped) return;
|
|
3287
4149
|
this.isBootstrapped = true;
|
|
@@ -3299,12 +4161,27 @@ var Cadenza = class {
|
|
|
3299
4161
|
this.runner.init();
|
|
3300
4162
|
this.metaRunner.init();
|
|
3301
4163
|
}
|
|
4164
|
+
/**
|
|
4165
|
+
* Retrieves the available strategies for running graphs.
|
|
4166
|
+
*
|
|
4167
|
+
* @return {Object} An object containing the available run strategies, where:
|
|
4168
|
+
* - PARALLEL: Executes graph runs asynchronously.
|
|
4169
|
+
* - SEQUENTIAL: Executes graph runs in a sequential order.
|
|
4170
|
+
*/
|
|
3302
4171
|
static get runStrategy() {
|
|
3303
4172
|
return {
|
|
3304
4173
|
PARALLEL: new GraphAsyncRun(),
|
|
3305
4174
|
SEQUENTIAL: new GraphStandardRun()
|
|
3306
4175
|
};
|
|
3307
4176
|
}
|
|
4177
|
+
/**
|
|
4178
|
+
* Sets the mode for the application and configures the broker and runner settings accordingly.
|
|
4179
|
+
*
|
|
4180
|
+
* @param {CadenzaMode} mode - The mode to set. It can be one of the following:
|
|
4181
|
+
* "debug", "dev", "verbose", or "production".
|
|
4182
|
+
* Each mode adjusts debug and verbosity settings.
|
|
4183
|
+
* @return {void} This method does not return a value.
|
|
4184
|
+
*/
|
|
3308
4185
|
static setMode(mode) {
|
|
3309
4186
|
this.mode = mode;
|
|
3310
4187
|
this.bootstrap();
|
|
@@ -3326,29 +4203,129 @@ var Cadenza = class {
|
|
|
3326
4203
|
}
|
|
3327
4204
|
}
|
|
3328
4205
|
/**
|
|
3329
|
-
* Validates
|
|
3330
|
-
*
|
|
3331
|
-
*
|
|
4206
|
+
* Validates the given name to ensure it is a non-empty string.
|
|
4207
|
+
* Throws an error if the validation fails.
|
|
4208
|
+
*
|
|
4209
|
+
* @param {string} name - The name to validate.
|
|
4210
|
+
* @return {void} This method does not return anything.
|
|
4211
|
+
* @throws {Error} If the name is not a non-empty string.
|
|
3332
4212
|
*/
|
|
3333
4213
|
static validateName(name) {
|
|
3334
4214
|
if (!name || typeof name !== "string") {
|
|
3335
4215
|
throw new Error("Task or Routine name must be a non-empty string.");
|
|
3336
4216
|
}
|
|
3337
4217
|
}
|
|
4218
|
+
/**
|
|
4219
|
+
* Executes the specified task or GraphRoutine with the given context using an internal runner.
|
|
4220
|
+
*
|
|
4221
|
+
* @param {Task | GraphRoutine} task - The task or GraphRoutine to be executed.
|
|
4222
|
+
* @param {AnyObject} context - The context in which the task or GraphRoutine should be executed.
|
|
4223
|
+
* @return {void}
|
|
4224
|
+
*
|
|
4225
|
+
* @example
|
|
4226
|
+
* ```ts
|
|
4227
|
+
* const task = Cadenza.createTask('My task', (ctx) => {
|
|
4228
|
+
* console.log('My task executed with context:', ctx);
|
|
4229
|
+
* });
|
|
4230
|
+
*
|
|
4231
|
+
* Cadenza.run(task, { foo: 'bar' });
|
|
4232
|
+
*
|
|
4233
|
+
* const routine = Cadenza.createRoutine('My routine', [task], 'My routine description');
|
|
4234
|
+
*
|
|
4235
|
+
* Cadenza.run(routine, { foo: 'bar' });
|
|
4236
|
+
* ```
|
|
4237
|
+
*/
|
|
3338
4238
|
static run(task, context) {
|
|
3339
4239
|
this.runner?.run(task, context);
|
|
3340
4240
|
}
|
|
4241
|
+
/**
|
|
4242
|
+
* Emits an event with the specified name and data payload to the broker.
|
|
4243
|
+
*
|
|
4244
|
+
* @param {string} event - The name of the event to emit.
|
|
4245
|
+
* @param {AnyObject} [data={}] - The data payload associated with the event.
|
|
4246
|
+
* @return {void} - No return value.
|
|
4247
|
+
*
|
|
4248
|
+
* @example
|
|
4249
|
+
* This is meant to be used as a global event emitter.
|
|
4250
|
+
* If you want to emit an event from within a task, you can use the `emit` method provided to the task function. See {@link TaskFunction}.
|
|
4251
|
+
* ```ts
|
|
4252
|
+
* Cadenza.emit('main.my_event', { foo: 'bar' });
|
|
4253
|
+
* ```
|
|
4254
|
+
*/
|
|
3341
4255
|
static emit(event, data = {}) {
|
|
3342
4256
|
this.broker?.emit(event, data);
|
|
3343
4257
|
}
|
|
3344
4258
|
/**
|
|
3345
|
-
* Creates a
|
|
3346
|
-
*
|
|
3347
|
-
* @
|
|
3348
|
-
*
|
|
3349
|
-
* @param
|
|
3350
|
-
* @
|
|
3351
|
-
* @
|
|
4259
|
+
* Creates and registers a new task with the specified parameters and options.
|
|
4260
|
+
* Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.
|
|
4261
|
+
* See {@link Task} for more information.
|
|
4262
|
+
*
|
|
4263
|
+
* @param {string} name - The unique name of the task.
|
|
4264
|
+
* @param {TaskFunction} func - The function to be executed by the task.
|
|
4265
|
+
* @param {string} [description] - An optional description for the task.
|
|
4266
|
+
* @param {TaskOptions} [options={}] - Configuration options for the task, such as concurrency, timeout, and retry settings.
|
|
4267
|
+
* @return {Task} The created task instance.
|
|
4268
|
+
*
|
|
4269
|
+
* @example
|
|
4270
|
+
* You can use arrow functions to create tasks.
|
|
4271
|
+
* ```ts
|
|
4272
|
+
* const task = Cadenza.createTask('My task', (ctx) => {
|
|
4273
|
+
* console.log('My task executed with context:', ctx);
|
|
4274
|
+
* }, 'My task description');
|
|
4275
|
+
* ```
|
|
4276
|
+
*
|
|
4277
|
+
* You can also use named functions to create tasks.
|
|
4278
|
+
* This is the preferred way to create tasks since it allows for code inspection in the CadenzaUI.
|
|
4279
|
+
* ```ts
|
|
4280
|
+
* function myTask(ctx) {
|
|
4281
|
+
* console.log('My task executed with context:', ctx);
|
|
4282
|
+
* }
|
|
4283
|
+
*
|
|
4284
|
+
* const task = Cadenza.createTask('My task', myTask);
|
|
4285
|
+
* ```
|
|
4286
|
+
*
|
|
4287
|
+
* ** Use the TaskOptions object to configure the task. **
|
|
4288
|
+
*
|
|
4289
|
+
* With concurrency limit, timeout limit and retry settings.
|
|
4290
|
+
* ```ts
|
|
4291
|
+
* Cadenza.createTask('My task', (ctx) => {
|
|
4292
|
+
* console.log('My task executed with context:', ctx);
|
|
4293
|
+
* }, 'My task description', {
|
|
4294
|
+
* concurrency: 10,
|
|
4295
|
+
* timeout: 10000,
|
|
4296
|
+
* retryCount: 3,
|
|
4297
|
+
* retryDelay: 1000,
|
|
4298
|
+
* retryDelayFactor: 1.5,
|
|
4299
|
+
* });
|
|
4300
|
+
* ```
|
|
4301
|
+
*
|
|
4302
|
+
* You can specify the input and output context schemas for the task.
|
|
4303
|
+
* ```ts
|
|
4304
|
+
* Cadenza.createTask('My task', (ctx) => {
|
|
4305
|
+
* return { bar: 'foo' + ctx.foo };
|
|
4306
|
+
* }, 'My task description', {
|
|
4307
|
+
* inputContextSchema: {
|
|
4308
|
+
* type: 'object',
|
|
4309
|
+
* properties: {
|
|
4310
|
+
* foo: {
|
|
4311
|
+
* type: 'string',
|
|
4312
|
+
* },
|
|
4313
|
+
* },
|
|
4314
|
+
* required: ['foo'],
|
|
4315
|
+
* },
|
|
4316
|
+
* validateInputContext: true, // default is false
|
|
4317
|
+
* outputContextSchema: {
|
|
4318
|
+
* type: 'object',
|
|
4319
|
+
* properties: {
|
|
4320
|
+
* bar: {
|
|
4321
|
+
* type: 'string',
|
|
4322
|
+
* },
|
|
4323
|
+
* },
|
|
4324
|
+
* required: ['bar'],
|
|
4325
|
+
* },
|
|
4326
|
+
* validateOutputContext: true, // default is false
|
|
4327
|
+
* });
|
|
4328
|
+
* ```
|
|
3352
4329
|
*/
|
|
3353
4330
|
static createTask(name, func, description, options = {}) {
|
|
3354
4331
|
this.bootstrap();
|
|
@@ -3395,40 +4372,82 @@ var Cadenza = class {
|
|
|
3395
4372
|
);
|
|
3396
4373
|
}
|
|
3397
4374
|
/**
|
|
3398
|
-
* Creates a
|
|
3399
|
-
*
|
|
3400
|
-
*
|
|
3401
|
-
* @
|
|
3402
|
-
*
|
|
3403
|
-
* @param
|
|
3404
|
-
* @
|
|
3405
|
-
* @
|
|
4375
|
+
* Creates a meta task with the specified name, functionality, description, and options.
|
|
4376
|
+
* This is used for creating tasks that lives on the meta layer.
|
|
4377
|
+
* The meta layer is a special layer that is executed separately from the business logic layer and is used for extending Cadenzas core functionality.
|
|
4378
|
+
* See {@link Task} or {@link createTask} for more information.
|
|
4379
|
+
*
|
|
4380
|
+
* @param {string} name - The name of the meta task.
|
|
4381
|
+
* @param {TaskFunction} func - The function to be executed by the meta task.
|
|
4382
|
+
* @param {string} [description] - An optional description of the meta task.
|
|
4383
|
+
* @param {TaskOptions} [options={}] - Additional optional task configuration. Automatically sets `isMeta` to true.
|
|
4384
|
+
* @return {Task} A task instance configured as a meta task.
|
|
3406
4385
|
*/
|
|
3407
4386
|
static createMetaTask(name, func, description, options = {}) {
|
|
3408
4387
|
options.isMeta = true;
|
|
3409
4388
|
return this.createTask(name, func, description, options);
|
|
3410
4389
|
}
|
|
3411
4390
|
/**
|
|
3412
|
-
* Creates a
|
|
3413
|
-
*
|
|
3414
|
-
*
|
|
3415
|
-
* @
|
|
3416
|
-
*
|
|
3417
|
-
* @param
|
|
3418
|
-
* @
|
|
3419
|
-
* @
|
|
4391
|
+
* Creates a unique task by wrapping the provided task function with a uniqueness constraint.
|
|
4392
|
+
* Unique tasks are designed to execute once per execution ID, merging parents. This is useful for
|
|
4393
|
+
* tasks that require fan-in/joins after parallel branches.
|
|
4394
|
+
* See {@link Task} for more information.
|
|
4395
|
+
*
|
|
4396
|
+
* @param {string} name - The name of the task to be created.
|
|
4397
|
+
* @param {TaskFunction} func - The function that contains the logic for the task. It receives joinedContexts as a list in the context (context.joinedContexts).
|
|
4398
|
+
* @param {string} [description] - An optional description of the task.
|
|
4399
|
+
* @param {TaskOptions} [options={}] - Optional configuration for the task, such as additional metadata or task options.
|
|
4400
|
+
* @return {Task} The task instance that was created with a uniqueness constraint.
|
|
4401
|
+
*
|
|
4402
|
+
* @example
|
|
4403
|
+
* ```ts
|
|
4404
|
+
* const splitTask = Cadenza.createTask('Split foos', function* (ctx) {
|
|
4405
|
+
* for (const foo of ctx.foos) {
|
|
4406
|
+
* yield { foo };
|
|
4407
|
+
* }
|
|
4408
|
+
* }, 'Splits a list of foos into multiple sub-branches');
|
|
4409
|
+
*
|
|
4410
|
+
* const processTask = Cadenza.createTask('Process foo', (ctx) => {
|
|
4411
|
+
* return { bar: 'foo' + ctx.foo };
|
|
4412
|
+
* }, 'Process a foo');
|
|
4413
|
+
*
|
|
4414
|
+
* const uniqueTask = Cadenza.createUniqueTask('Gather processed foos', (ctx) => {
|
|
4415
|
+
* // A unique task will always be provided with a list of contexts (ctx.joinedContexts) from its predecessors.
|
|
4416
|
+
* const processedFoos = ctx.joinedContexts.map((c) => c.bar);
|
|
4417
|
+
* return { foos: processedFoos };
|
|
4418
|
+
* }, 'Gathers together the processed foos.');
|
|
4419
|
+
*
|
|
4420
|
+
* splitTask.then(
|
|
4421
|
+
* processTask.then(
|
|
4422
|
+
* uniqueTask,
|
|
4423
|
+
* ),
|
|
4424
|
+
* );
|
|
4425
|
+
*
|
|
4426
|
+
* // Give the flow a name using a routine
|
|
4427
|
+
* Cadenza.createRoutine(
|
|
4428
|
+
* 'Process foos',
|
|
4429
|
+
* [splitTask],
|
|
4430
|
+
* 'Processes a list of foos'
|
|
4431
|
+
* ).doOn('main.received_foos'); // Subscribe to a signal
|
|
4432
|
+
*
|
|
4433
|
+
* // Trigger the flow from anywhere
|
|
4434
|
+
* Cadenza.emit('main.received_foos', { foos: ['foo1', 'foo2', 'foo3'] });
|
|
4435
|
+
* ```
|
|
4436
|
+
*
|
|
3420
4437
|
*/
|
|
3421
4438
|
static createUniqueTask(name, func, description, options = {}) {
|
|
3422
4439
|
options.isUnique = true;
|
|
3423
4440
|
return this.createTask(name, func, description, options);
|
|
3424
4441
|
}
|
|
3425
4442
|
/**
|
|
3426
|
-
* Creates a
|
|
3427
|
-
* @
|
|
3428
|
-
*
|
|
3429
|
-
* @param
|
|
3430
|
-
* @param
|
|
3431
|
-
* @
|
|
4443
|
+
* Creates a unique meta task with the specified name, function, description, and options.
|
|
4444
|
+
* See {@link createUniqueTask} and {@link createMetaTask} for more information.
|
|
4445
|
+
*
|
|
4446
|
+
* @param {string} name - The name of the task to create.
|
|
4447
|
+
* @param {TaskFunction} func - The function to execute when the task is run.
|
|
4448
|
+
* @param {string} [description] - An optional description of the task.
|
|
4449
|
+
* @param {TaskOptions} [options={}] - Optional settings for the task. Defaults to an empty object. Automatically sets `isMeta` and `isUnique` to true.
|
|
4450
|
+
* @return {Task} The created unique meta task.
|
|
3432
4451
|
*/
|
|
3433
4452
|
static createUniqueMetaTask(name, func, description, options = {}) {
|
|
3434
4453
|
options.isMeta = true;
|
|
@@ -3436,14 +4455,33 @@ var Cadenza = class {
|
|
|
3436
4455
|
return this.createUniqueTask(name, func, description, options);
|
|
3437
4456
|
}
|
|
3438
4457
|
/**
|
|
3439
|
-
* Creates a
|
|
3440
|
-
*
|
|
3441
|
-
* @
|
|
3442
|
-
*
|
|
3443
|
-
* @param
|
|
3444
|
-
* @param
|
|
3445
|
-
* @
|
|
3446
|
-
* @
|
|
4458
|
+
* Creates a throttled task with a concurrency limit of 1, ensuring that only one instance of the task can run at a time for a specific throttle tag.
|
|
4459
|
+
* This is useful for ensuring execution order and preventing race conditions.
|
|
4460
|
+
* See {@link Task} for more information.
|
|
4461
|
+
*
|
|
4462
|
+
* @param {string} name - The name of the task.
|
|
4463
|
+
* @param {TaskFunction} func - The function to be executed when the task runs.
|
|
4464
|
+
* @param {ThrottleTagGetter} [throttledIdGetter=() => "default"] - A function that generates a throttle tag identifier to group tasks for throttling.
|
|
4465
|
+
* @param {string} [description] - An optional description of the task.
|
|
4466
|
+
* @param {TaskOptions} [options={}] - Additional options to customize the task behavior.
|
|
4467
|
+
* @return {Task} The created throttled task.
|
|
4468
|
+
*
|
|
4469
|
+
* @example
|
|
4470
|
+
* ```ts
|
|
4471
|
+
* const task = Cadenza.createThrottledTask(
|
|
4472
|
+
* 'My task',
|
|
4473
|
+
* async (ctx) => {
|
|
4474
|
+
* await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
4475
|
+
* console.log('My task executed with context:', ctx);
|
|
4476
|
+
* },
|
|
4477
|
+
* // Will throttle by the value of ctx.foo to make sure tasks with the same value are executed sequentially
|
|
4478
|
+
* (ctx) => ctx.foo,
|
|
4479
|
+
* );
|
|
4480
|
+
*
|
|
4481
|
+
* Cadenza.run(task, { foo: 'bar' }); // (First execution)
|
|
4482
|
+
* Cadenza.run(task, { foo: 'bar' }); // This will be executed after the first execution is finished
|
|
4483
|
+
* Cadenza.run(task, { foo: 'baz' }); // This will be executed in parallel with the first execution
|
|
4484
|
+
* ```
|
|
3447
4485
|
*/
|
|
3448
4486
|
static createThrottledTask(name, func, throttledIdGetter = () => "default", description, options = {}) {
|
|
3449
4487
|
options.concurrency = 1;
|
|
@@ -3451,13 +4489,15 @@ var Cadenza = class {
|
|
|
3451
4489
|
return this.createTask(name, func, description, options);
|
|
3452
4490
|
}
|
|
3453
4491
|
/**
|
|
3454
|
-
* Creates a
|
|
3455
|
-
* @
|
|
3456
|
-
*
|
|
3457
|
-
* @param
|
|
3458
|
-
* @param
|
|
3459
|
-
* @param
|
|
3460
|
-
* @
|
|
4492
|
+
* Creates a throttled meta task with the specified configuration.
|
|
4493
|
+
* See {@link createThrottledTask} and {@link createMetaTask} for more information.
|
|
4494
|
+
*
|
|
4495
|
+
* @param {string} name - The name of the throttled meta task.
|
|
4496
|
+
* @param {TaskFunction} func - The task function to be executed.
|
|
4497
|
+
* @param {ThrottleTagGetter} throttledIdGetter - A function to retrieve the throttling identifier.
|
|
4498
|
+
* @param {string} [description] - An optional description of the task.
|
|
4499
|
+
* @param {TaskOptions} [options={}] - Additional options for configuring the task.
|
|
4500
|
+
* @return {Task} The created throttled meta task.
|
|
3461
4501
|
*/
|
|
3462
4502
|
static createThrottledMetaTask(name, func, throttledIdGetter, description, options = {}) {
|
|
3463
4503
|
options.isMeta = true;
|
|
@@ -3470,14 +4510,37 @@ var Cadenza = class {
|
|
|
3470
4510
|
);
|
|
3471
4511
|
}
|
|
3472
4512
|
/**
|
|
3473
|
-
* Creates
|
|
3474
|
-
*
|
|
3475
|
-
* @
|
|
3476
|
-
*
|
|
3477
|
-
* @param
|
|
3478
|
-
* @param
|
|
3479
|
-
* @
|
|
3480
|
-
* @
|
|
4513
|
+
* Creates and returns a new debounced task with the specified parameters.
|
|
4514
|
+
* This is useful to prevent rapid execution of tasks that may be triggered by multiple events within a certain time frame.
|
|
4515
|
+
* See {@link DebounceTask} for more information.
|
|
4516
|
+
*
|
|
4517
|
+
* @param {string} name - The unique name of the task to be created.
|
|
4518
|
+
* @param {TaskFunction} func - The function to be executed by the task.
|
|
4519
|
+
* @param {string} [description] - An optional description of the task.
|
|
4520
|
+
* @param {number} [debounceTime=1000] - The debounce time in milliseconds to delay the execution of the task.
|
|
4521
|
+
* @param {TaskOptions & DebounceOptions} [options={}] - Additional configuration options for the task, including debounce behavior and other task properties.
|
|
4522
|
+
* @return {DebounceTask} A new instance of the DebounceTask with the specified configuration.
|
|
4523
|
+
*
|
|
4524
|
+
* @example
|
|
4525
|
+
* ```ts
|
|
4526
|
+
* const task = Cadenza.createDebounceTask(
|
|
4527
|
+
* 'My debounced task',
|
|
4528
|
+
* (ctx) => {
|
|
4529
|
+
* console.log('My task executed with context:', ctx);
|
|
4530
|
+
* },
|
|
4531
|
+
* 'My debounced task description',
|
|
4532
|
+
* 100, // Debounce time in milliseconds. Default is 1000
|
|
4533
|
+
* {
|
|
4534
|
+
* leading: false, // Should the first execution of a burst be executed immediately? Default is false
|
|
4535
|
+
* trailing: true, // Should the last execution of a burst be executed? Default is true
|
|
4536
|
+
* maxWait: 1000, // Maximum time in milliseconds to wait for the next execution. Default is 0
|
|
4537
|
+
* },
|
|
4538
|
+
* );
|
|
4539
|
+
*
|
|
4540
|
+
* Cadenza.run(task, { foo: 'bar' }); // This will not be executed
|
|
4541
|
+
* Cadenza.run(task, { foo: 'bar' }); // This will not be executed
|
|
4542
|
+
* Cadenza.run(task, { foo: 'baz' }); // This execution will be delayed by 100ms
|
|
4543
|
+
* ```
|
|
3481
4544
|
*/
|
|
3482
4545
|
static createDebounceTask(name, func, description, debounceTime = 1e3, options = {}) {
|
|
3483
4546
|
this.bootstrap();
|
|
@@ -3521,13 +4584,15 @@ var Cadenza = class {
|
|
|
3521
4584
|
);
|
|
3522
4585
|
}
|
|
3523
4586
|
/**
|
|
3524
|
-
* Creates a
|
|
3525
|
-
* @
|
|
3526
|
-
*
|
|
3527
|
-
* @param
|
|
3528
|
-
* @param
|
|
3529
|
-
* @param
|
|
3530
|
-
* @
|
|
4587
|
+
* Creates a debounced meta task with the specified parameters.
|
|
4588
|
+
* See {@link createDebounceTask} and {@link createMetaTask} for more information.
|
|
4589
|
+
*
|
|
4590
|
+
* @param {string} name - The name of the task.
|
|
4591
|
+
* @param {TaskFunction} func - The function to be executed by the task.
|
|
4592
|
+
* @param {string} [description] - Optional description of the task.
|
|
4593
|
+
* @param {number} [debounceTime=1000] - The debounce delay in milliseconds.
|
|
4594
|
+
* @param {TaskOptions & DebounceOptions} [options={}] - Additional configuration options for the task.
|
|
4595
|
+
* @return {DebounceTask} Returns an instance of the debounced meta task.
|
|
3531
4596
|
*/
|
|
3532
4597
|
static createDebounceMetaTask(name, func, description, debounceTime = 1e3, options = {}) {
|
|
3533
4598
|
options.isMeta = true;
|
|
@@ -3540,14 +4605,64 @@ var Cadenza = class {
|
|
|
3540
4605
|
);
|
|
3541
4606
|
}
|
|
3542
4607
|
/**
|
|
3543
|
-
* Creates an
|
|
3544
|
-
*
|
|
3545
|
-
*
|
|
3546
|
-
*
|
|
3547
|
-
* @
|
|
3548
|
-
*
|
|
3549
|
-
* @
|
|
3550
|
-
* @
|
|
4608
|
+
* Creates an ephemeral task with the specified configuration.
|
|
4609
|
+
* Ephemeral tasks are designed to self-destruct after execution or a certain condition is met.
|
|
4610
|
+
* This is useful for transient tasks such as resolving promises or performing cleanup operations.
|
|
4611
|
+
* They are not registered by default.
|
|
4612
|
+
* See {@link EphemeralTask} for more information.
|
|
4613
|
+
*
|
|
4614
|
+
* @param {string} name - The name of the task to be created.
|
|
4615
|
+
* @param {TaskFunction} func - The function that defines the logic of the task.
|
|
4616
|
+
* @param {string} [description] - An optional description of the task.
|
|
4617
|
+
* @param {TaskOptions & EphemeralTaskOptions} [options={}] - The configuration options for the task, including concurrency, timeouts, and retry policies.
|
|
4618
|
+
* @return {EphemeralTask} The created ephemeral task instance.
|
|
4619
|
+
*
|
|
4620
|
+
* @example
|
|
4621
|
+
* By default, ephemeral tasks are executed once and destroyed after execution.
|
|
4622
|
+
* ```ts
|
|
4623
|
+
* const task = Cadenza.createEphemeralTask('My ephemeral task', (ctx) => {
|
|
4624
|
+
* console.log('My task executed with context:', ctx);
|
|
4625
|
+
* });
|
|
4626
|
+
*
|
|
4627
|
+
* Cadenza.run(task); // Executes the task once and destroys it after execution
|
|
4628
|
+
* Cadenza.run(task); // Does nothing, since the task is destroyed
|
|
4629
|
+
* ```
|
|
4630
|
+
*
|
|
4631
|
+
* Use destroy condition to conditionally destroy the task
|
|
4632
|
+
* ```ts
|
|
4633
|
+
* const task = Cadenza.createEphemeralTask(
|
|
4634
|
+
* 'My ephemeral task',
|
|
4635
|
+
* (ctx) => {
|
|
4636
|
+
* console.log('My task executed with context:', ctx);
|
|
4637
|
+
* },
|
|
4638
|
+
* 'My ephemeral task description',
|
|
4639
|
+
* {
|
|
4640
|
+
* once: false, // Should the task be executed only once? Default is true
|
|
4641
|
+
* destroyCondition: (ctx) => ctx.foo > 10, // Should the task be destroyed after execution? Default is undefined
|
|
4642
|
+
* },
|
|
4643
|
+
* );
|
|
4644
|
+
*
|
|
4645
|
+
* Cadenza.run(task, { foo: 5 }); // The task will not be destroyed and can still be executed
|
|
4646
|
+
* Cadenza.run(task, { foo: 10 }); // The task will not be destroyed and can still be executed
|
|
4647
|
+
* Cadenza.run(task, { foo: 20 }); // The task will be destroyed after execution and cannot be executed anymore
|
|
4648
|
+
* Cadenza.run(task, { foo: 30 }); // This will not be executed
|
|
4649
|
+
* ```
|
|
4650
|
+
*
|
|
4651
|
+
* A practical use case for ephemeral tasks is to resolve a promise upon some external event.
|
|
4652
|
+
* ```ts
|
|
4653
|
+
* const task = Cadenza.createTask('Confirm something', (ctx, emit) => {
|
|
4654
|
+
* return new Promise((resolve) => {
|
|
4655
|
+
* ctx.foo = uuid();
|
|
4656
|
+
*
|
|
4657
|
+
* Cadenza.createEphemeralTask(`Resolve promise of ${ctx.foo}`, (c) => {
|
|
4658
|
+
* console.log('My task executed with context:', ctx);
|
|
4659
|
+
* resolve(c);
|
|
4660
|
+
* }).doOn(`socket.confirmation_received:${ctx.foo}`);
|
|
4661
|
+
*
|
|
4662
|
+
* emit('this_domain.confirmation_requested', ctx);
|
|
4663
|
+
* });
|
|
4664
|
+
* });
|
|
4665
|
+
* ```
|
|
3551
4666
|
*/
|
|
3552
4667
|
static createEphemeralTask(name, func, description, options = {}) {
|
|
3553
4668
|
this.bootstrap();
|
|
@@ -3598,45 +4713,72 @@ var Cadenza = class {
|
|
|
3598
4713
|
);
|
|
3599
4714
|
}
|
|
3600
4715
|
/**
|
|
3601
|
-
* Creates an
|
|
3602
|
-
* @
|
|
3603
|
-
*
|
|
3604
|
-
* @param
|
|
3605
|
-
* @param
|
|
3606
|
-
* @
|
|
4716
|
+
* Creates an ephemeral meta task with the specified name, function, description, and options.
|
|
4717
|
+
* See {@link createEphemeralTask} and {@link createMetaTask} for more details.
|
|
4718
|
+
*
|
|
4719
|
+
* @param {string} name - The name of the task to be created.
|
|
4720
|
+
* @param {TaskFunction} func - The function to be executed as part of the task.
|
|
4721
|
+
* @param {string} [description] - An optional description of the task.
|
|
4722
|
+
* @param {TaskOptions & EphemeralTaskOptions} [options={}] - Additional options for configuring the task.
|
|
4723
|
+
* @return {EphemeralTask} The created ephemeral meta task.
|
|
3607
4724
|
*/
|
|
3608
4725
|
static createEphemeralMetaTask(name, func, description, options = {}) {
|
|
3609
4726
|
options.isMeta = true;
|
|
3610
4727
|
return this.createEphemeralTask(name, func, description, options);
|
|
3611
4728
|
}
|
|
3612
4729
|
/**
|
|
3613
|
-
* Creates a
|
|
3614
|
-
*
|
|
3615
|
-
*
|
|
3616
|
-
* @
|
|
3617
|
-
*
|
|
3618
|
-
* @
|
|
4730
|
+
* Creates a new routine with the specified name, tasks, and an optional description.
|
|
4731
|
+
* Routines are named entry points to starting tasks and are registered in the GraphRegistry.
|
|
4732
|
+
* They are used to group tasks together and provide a high-level structure for organizing and managing the execution of a set of tasks.
|
|
4733
|
+
* See {@link GraphRoutine} for more information.
|
|
4734
|
+
*
|
|
4735
|
+
* @param {string} name - The name of the routine to create.
|
|
4736
|
+
* @param {Task[]} tasks - A list of tasks to include in the routine.
|
|
4737
|
+
* @param {string} [description=""] - An optional description for the routine.
|
|
4738
|
+
* @return {GraphRoutine} A new instance of the GraphRoutine containing the specified tasks and description.
|
|
4739
|
+
*
|
|
4740
|
+
* @example
|
|
4741
|
+
* ```ts
|
|
4742
|
+
* const task1 = Cadenza.createTask("Task 1", () => {});
|
|
4743
|
+
* const task2 = Cadenza.createTask("Task 2", () => {});
|
|
4744
|
+
*
|
|
4745
|
+
* task1.then(task2);
|
|
4746
|
+
*
|
|
4747
|
+
* const routine = Cadenza.createRoutine("Some routine", [task1]);
|
|
4748
|
+
*
|
|
4749
|
+
* Cadenza.run(routine);
|
|
4750
|
+
*
|
|
4751
|
+
* // Or, routines can be triggered by signals
|
|
4752
|
+
* routine.doOn("some.signal");
|
|
4753
|
+
*
|
|
4754
|
+
* Cadenza.emit("some.signal", {});
|
|
4755
|
+
* ```
|
|
3619
4756
|
*/
|
|
3620
4757
|
static createRoutine(name, tasks, description = "") {
|
|
3621
4758
|
this.bootstrap();
|
|
3622
4759
|
this.validateName(name);
|
|
3623
4760
|
if (tasks.length === 0) {
|
|
3624
|
-
|
|
4761
|
+
throw new Error(`Routine '${name}' created with no starting tasks.`);
|
|
3625
4762
|
}
|
|
3626
4763
|
return new GraphRoutine(name, tasks, description);
|
|
3627
4764
|
}
|
|
3628
4765
|
/**
|
|
3629
|
-
* Creates a
|
|
3630
|
-
*
|
|
3631
|
-
*
|
|
3632
|
-
* @
|
|
3633
|
-
*
|
|
4766
|
+
* Creates a meta routine with a given name, tasks, and optional description.
|
|
4767
|
+
* Routines are named entry points to starting tasks and are registered in the GraphRegistry.
|
|
4768
|
+
* They are used to group tasks together and provide a high-level structure for organizing and managing the execution of a set of tasks.
|
|
4769
|
+
* See {@link GraphRoutine} and {@link createRoutine} for more information.
|
|
4770
|
+
*
|
|
4771
|
+
* @param {string} name - The name of the routine to be created.
|
|
4772
|
+
* @param {Task[]} tasks - An array of tasks that the routine will consist of.
|
|
4773
|
+
* @param {string} [description=""] - An optional description for the routine.
|
|
4774
|
+
* @return {GraphRoutine} A new instance of the `GraphRoutine` representing the created routine.
|
|
4775
|
+
* @throws {Error} If no starting tasks are provided.
|
|
3634
4776
|
*/
|
|
3635
4777
|
static createMetaRoutine(name, tasks, description = "") {
|
|
3636
4778
|
this.bootstrap();
|
|
3637
4779
|
this.validateName(name);
|
|
3638
4780
|
if (tasks.length === 0) {
|
|
3639
|
-
|
|
4781
|
+
throw new Error(`Routine '${name}' created with no starting tasks.`);
|
|
3640
4782
|
}
|
|
3641
4783
|
return new GraphRoutine(name, tasks, description, true);
|
|
3642
4784
|
}
|