@cadenza.io/service 1.24.5 → 1.24.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -21814,6 +21814,13 @@ declare class CadenzaService {
21814
21814
  * @param {string} signal - The name of the event or signal to emit.
21815
21815
  * @param {AnyObject} [data={}] - The data to be emitted along with the signal.
21816
21816
  * @return {void} No return value.
21817
+ *
21818
+ * @example
21819
+ * This is meant to be used as a global event emitter.
21820
+ * 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}.
21821
+ * ```ts
21822
+ * Cadenza.emit('main.my_event', { foo: 'bar' });
21823
+ * ```
21817
21824
  */
21818
21825
  static emit(signal: string, data?: AnyObject): void;
21819
21826
  /**
@@ -21822,10 +21829,24 @@ declare class CadenzaService {
21822
21829
  * @param {Task | GraphRoutine} task - The task or graph routine to be executed.
21823
21830
  * @param {AnyObject} context - The context within which the task will be executed.
21824
21831
  * @return {void}
21832
+ *
21833
+ * @example
21834
+ * ```ts
21835
+ * const task = Cadenza.createTask('My task', (ctx) => {
21836
+ * console.log('My task executed with context:', ctx);
21837
+ * });
21838
+ *
21839
+ * Cadenza.run(task, { foo: 'bar' });
21840
+ *
21841
+ * const routine = Cadenza.createRoutine('My routine', [task], 'My routine description');
21842
+ *
21843
+ * Cadenza.run(routine, { foo: 'bar' });
21844
+ * ```
21825
21845
  */
21826
21846
  static run(task: Task | GraphRoutine, context: AnyObject): void;
21827
21847
  /**
21828
21848
  * Logs a message with a specified log level and additional contextual data.
21849
+ * Records in the CadenzaDB when available.
21829
21850
  *
21830
21851
  * @param {string} message - The main message to be logged.
21831
21852
  * @param {any} [data={}] - Additional data or metadata to include with the log.
@@ -21844,11 +21865,57 @@ declare class CadenzaService {
21844
21865
  * @param {string|undefined} [serviceName] - The name of the service that the routine belongs to. This is optional and defaults to undefined.
21845
21866
  * @param {TaskOptions} [options={}] - A configuration object for the task, allowing various properties such as concurrency, timeout, and retry settings to be customized.
21846
21867
  * @return {DeputyTask} - A new DeputyTask instance initialized with the specified parameters.
21868
+ *
21869
+ * @example
21870
+ * Let's say we are writing the code for a Service called "Service1".
21871
+ * We also have an additional service called "Service2" with a routine called "My Routine".
21872
+ * A flow on Service1 depends on the result of "My Routine" on Service2.
21873
+ * We can create a deputy task for the routine using the following code:
21874
+ * ```ts
21875
+ * Cadenza.createDeputyTask("My Routine", "Service2").then(
21876
+ * Cadenza.createTask("Handle result", (ctx) => {
21877
+ * console.log("'Handle result' executed with context:", ctx);
21878
+ * }),
21879
+ * );
21880
+ * ```
21881
+ * Internally, this will send a request to an available "Service2" instance to execute the "My Routine" routine.
21882
+ * The deputy task will wait for the response and then execute the next task(s) in the chain.
21883
+ *
21884
+ * You can visualize the execution of the deputy task as follows:
21885
+ * ```
21886
+ * Service1 flow = [Deputy tasks for "My Routine"] -> ["Handle result"]
21887
+ * || A
21888
+ * V ||
21889
+ * Service2 flow = [[My Routine]]
21890
+ * ```
21891
+ *
21892
+ * Deputy tasks are useful for delegating flows to other services, allowing for parallel execution and load balancing.
21893
+ * But it creates tight coupling between the services, which may not be desirable in some cases.
21894
+ * In cases where an event on one service should simply trigger a flow on another service, without the need for a result,
21895
+ * it is recommended to use signals instead. Like this:
21896
+ *
21897
+ * Service1
21898
+ * ```ts
21899
+ * Cadenza.createTask("Generate event", (ctx, emit) => {
21900
+ * // Do something
21901
+ * emit("some.event");
21902
+ * });
21903
+ * ```
21904
+ *
21905
+ * Service2
21906
+ * ```ts
21907
+ * Cadenza.createTask("Handle event", (ctx) => {
21908
+ * console.log("Handle event executed with context:", ctx);
21909
+ * }).doOn("Service1.some.event");
21910
+ * ```
21911
+ *
21912
+ * Every time the "Generate event" task is executed, it will emit a signal "Service1.some.event" to one Service2 instance and trigger the "Handle event" task.
21847
21913
  */
21848
21914
  static createDeputyTask(routineName: string, serviceName?: string | undefined, options?: TaskOptions): DeputyTask;
21849
21915
  /**
21850
21916
  * Creates a meta deputy task by setting the `isMeta` property in the options to true,
21851
21917
  * and delegating task creation to the `createDeputyTask` method.
21918
+ * See {@link createDeputyTask} and {@link createMetaTask} for more information.
21852
21919
  *
21853
21920
  * @param {string} routineName - The name of the routine associated with the task.
21854
21921
  * @param {string | undefined} [serviceName] - The optional name of the service associated with the task.
@@ -21856,39 +21923,20 @@ declare class CadenzaService {
21856
21923
  * @return {DeputyTask} - The created meta deputy task.
21857
21924
  */
21858
21925
  static createMetaDeputyTask(routineName: string, serviceName?: string | undefined, options?: TaskOptions): DeputyTask;
21859
- /**
21860
- * Creates a unique deputy task with the specified routine name, service name,
21861
- * and optional task options. The uniqueness is ensured by setting the
21862
- * `isUnique` property in the task options.
21863
- *
21864
- * @param {string} routineName - The name of the routine associated with the task.
21865
- * @param {string | undefined} [serviceName] - The name of the service associated with the task. If undefined, no service name is used.
21866
- * @param {TaskOptions} [options={}] - Additional configuration options for the task.
21867
- * @return {*} - Returns the result of creating a deputy task with the provided parameters and unique configuration.
21868
- */
21869
- static createUniqueDeputyTask(routineName: string, serviceName?: string | undefined, options?: TaskOptions): DeputyTask;
21870
- /**
21871
- * Creates a unique meta deputy task based on the provided routine name, service name, and options.
21872
- * This method sets the task as a meta task and delegates the creation to the `createUniqueDeputyTask` method.
21873
- *
21874
- * @param {string} routineName - The routine name to associate with the meta deputy task.
21875
- * @param {string | undefined} [serviceName] - The optional service name associated with the task.
21876
- * @param {TaskOptions} [options] - Additional options to configure the task. Defaults to an empty options object.
21877
- * @return {*} A unique meta deputy task created based on the given parameters.
21878
- */
21879
- static createUniqueMetaDeputyTask(routineName: string, serviceName?: string | undefined, options?: TaskOptions): DeputyTask;
21880
21926
  /**
21881
21927
  * Creates a throttled deputy task with the specified parameters.
21928
+ * See {@link createThrottledTask} and {@link createDeputyTask} for more information.
21882
21929
  *
21883
21930
  * @param {string} routineName - The name of the routine to be executed.
21884
21931
  * @param {string | undefined} [serviceName=undefined] - The name of the service, if applicable.
21885
21932
  * @param {ThrottleTagGetter} [throttledIdGetter=() => "default"] - A function to get the throttled tag for the task.
21886
21933
  * @param {TaskOptions} [options={}] - The options for task configuration, including concurrency and callbacks.
21887
- * @return {any} The created throttled deputy task.
21934
+ * @return {DeputyTask} The created throttled deputy task.
21888
21935
  */
21889
21936
  static createThrottledDeputyTask(routineName: string, serviceName?: string | undefined, throttledIdGetter?: ThrottleTagGetter, options?: TaskOptions): DeputyTask;
21890
21937
  /**
21891
21938
  * Creates a throttled deputy task with meta-task settings enabled.
21939
+ * See {@link createThrottledTask},{@link createDeputyTask} and {@link createMetaTask} for more information.
21892
21940
  *
21893
21941
  * @param {string} routineName - The name of the routine for which the task is being created.
21894
21942
  * @param {string|undefined} [serviceName=undefined] - The name of the service associated with the task, or undefined if not applicable.
@@ -21900,6 +21948,7 @@ declare class CadenzaService {
21900
21948
  /**
21901
21949
  * Creates and configures a signal transmission task that handles the transmission
21902
21950
  * of a specified signal to a target service with a set of customizable options.
21951
+ * This is only used for internal purposes and is not exposed to the business logic layer.
21903
21952
  *
21904
21953
  * @param {string} signalName - The name of the signal to be transmitted.
21905
21954
  * @param {string} serviceName - The name of the target service to transmit the signal to.
@@ -22013,114 +22062,339 @@ declare class CadenzaService {
22013
22062
  * @param {string} [description] - An optional description of what the task does.
22014
22063
  * @param {TaskOptions} [options={}] - An optional configuration object specifying additional task options.
22015
22064
  * @return {Task} - The created task instance.
22065
+ *
22066
+ * @example
22067
+ * You can use arrow functions to create tasks.
22068
+ * ```ts
22069
+ * const task = Cadenza.createTask('My task', (ctx) => {
22070
+ * console.log('My task executed with context:', ctx);
22071
+ * }, 'My task description');
22072
+ * ```
22073
+ *
22074
+ * You can also use named functions to create tasks.
22075
+ * This is the preferred way to create tasks since it allows for code inspection in the CadenzaUI.
22076
+ * ```ts
22077
+ * function myTask(ctx) {
22078
+ * console.log('My task executed with context:', ctx);
22079
+ * }
22080
+ *
22081
+ * const task = Cadenza.createTask('My task', myTask);
22082
+ * ```
22083
+ *
22084
+ * ** Use the TaskOptions object to configure the task. **
22085
+ *
22086
+ * With concurrency limit, timeout limit and retry settings.
22087
+ * ```ts
22088
+ * Cadenza.createTask('My task', (ctx) => {
22089
+ * console.log('My task executed with context:', ctx);
22090
+ * }, 'My task description', {
22091
+ * concurrency: 10,
22092
+ * timeout: 10000,
22093
+ * retryCount: 3,
22094
+ * retryDelay: 1000,
22095
+ * retryDelayFactor: 1.5,
22096
+ * });
22097
+ * ```
22098
+ *
22099
+ * You can specify the input and output context schemas for the task.
22100
+ * ```ts
22101
+ * Cadenza.createTask('My task', (ctx) => {
22102
+ * return { bar: 'foo' + ctx.foo };
22103
+ * }, 'My task description', {
22104
+ * inputContextSchema: {
22105
+ * type: 'object',
22106
+ * properties: {
22107
+ * foo: {
22108
+ * type: 'string',
22109
+ * },
22110
+ * },
22111
+ * required: ['foo'],
22112
+ * },
22113
+ * validateInputContext: true, // default is false
22114
+ * outputContextSchema: {
22115
+ * type: 'object',
22116
+ * properties: {
22117
+ * bar: {
22118
+ * type: 'string',
22119
+ * },
22120
+ * },
22121
+ * required: ['bar'],
22122
+ * },
22123
+ * validateOutputContext: true, // default is false
22124
+ * });
22125
+ * ```
22016
22126
  */
22017
22127
  static createTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
22018
22128
  /**
22019
- * Creates a new meta task with the specified name, function, description, and options.
22129
+ * Creates a meta task with the specified name, functionality, description, and options.
22130
+ * This is used for creating tasks that lives on the meta layer.
22131
+ * The meta layer is a special layer that is executed separately from the business logic layer and is used for extending Cadenzas core functionality.
22132
+ * See {@link Task} or {@link createTask} for more information.
22020
22133
  *
22021
22134
  * @param {string} name - The name of the meta task.
22022
22135
  * @param {TaskFunction} func - The function to be executed by the meta task.
22023
22136
  * @param {string} [description] - An optional description of the meta task.
22024
- * @param {TaskOptions} [options={}] - Additional options to configure the meta task.
22025
- * @return {Task} The created meta task instance.
22137
+ * @param {TaskOptions} [options={}] - Additional optional task configuration. Automatically sets `isMeta` to true.
22138
+ * @return {Task} A task instance configured as a meta task.
22026
22139
  */
22027
22140
  static createMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
22028
22141
  /**
22029
- * Creates a UniqueTask (executes once per execution ID, merging parents) and registers it.
22030
- * Use for fan-in/joins after parallel branches.
22142
+ * Creates a unique task by wrapping the provided task function with a uniqueness constraint.
22143
+ * Unique tasks are designed to execute once per execution ID, merging parents. This is useful for
22144
+ * tasks that require fan-in/joins after parallel branches.
22145
+ * See {@link Task} for more information.
22031
22146
  * @param {string} name Unique identifier.
22032
22147
  * @param {TaskFunction} func Function receiving joinedContexts as a list (context.joinedContexts).
22033
22148
  * @param {string} [description] Optional description.
22034
22149
  * @param {TaskOptions} [options={}] Optional task options.
22035
22150
  * @returns {Task} The created UniqueTask.
22151
+ *
22152
+ * @example
22153
+ * ```ts
22154
+ * const splitTask = Cadenza.createTask('Split foos', function* (ctx) {
22155
+ * for (const foo of ctx.foos) {
22156
+ * yield { foo };
22157
+ * }
22158
+ * }, 'Splits a list of foos into multiple sub-branches');
22159
+ *
22160
+ * const processTask = Cadenza.createTask('Process foo', (ctx) => {
22161
+ * return { bar: 'foo' + ctx.foo };
22162
+ * }, 'Process a foo');
22163
+ *
22164
+ * const uniqueTask = Cadenza.createUniqueTask('Gather processed foos', (ctx) => {
22165
+ * // A unique task will always be provided with a list of contexts (ctx.joinedContexts) from its predecessors.
22166
+ * const processedFoos = ctx.joinedContexts.map((c) => c.bar);
22167
+ * return { foos: processedFoos };
22168
+ * }, 'Gathers together the processed foos.');
22169
+ *
22170
+ * splitTask.then(
22171
+ * processTask.then(
22172
+ * uniqueTask,
22173
+ * ),
22174
+ * );
22175
+ *
22176
+ * // Give the flow a name using a routine
22177
+ * Cadenza.createRoutine(
22178
+ * 'Process foos',
22179
+ * [splitTask],
22180
+ * 'Processes a list of foos'
22181
+ * ).doOn('main.received_foos'); // Subscribe to a signal
22182
+ *
22183
+ * // Trigger the flow from anywhere
22184
+ * Cadenza.emit('main.received_foos', { foos: ['foo1', 'foo2', 'foo3'] });
22185
+ * ```
22036
22186
  */
22037
22187
  static createUniqueTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
22038
22188
  /**
22039
- * Creates a unique meta task with the provided name, function, description, and options.
22189
+ * Creates a unique meta task with the specified name, function, description, and options.
22190
+ * See {@link createUniqueTask} and {@link createMetaTask} for more information.
22040
22191
  *
22041
- * @param {string} name - The unique name for the meta task.
22042
- * @param {TaskFunction} func - The function to be executed by the task.
22043
- * @param {string} [description] - An optional description of the task's purpose.
22044
- * @param {TaskOptions} [options={}] - Additional optional configuration settings for the task.
22045
- * @return {Task} Returns the created unique meta task.
22192
+ * @param {string} name - The name of the task to create.
22193
+ * @param {TaskFunction} func - The function to execute when the task is run.
22194
+ * @param {string} [description] - An optional description of the task.
22195
+ * @param {TaskOptions} [options={}] - Optional settings for the task. Defaults to an empty object. Automatically sets `isMeta` and `isUnique` to true.
22196
+ * @return {Task} The created unique meta task.
22046
22197
  */
22047
22198
  static createUniqueMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
22048
22199
  /**
22049
- * Creates a ThrottledTask (rate-limited by concurrency or custom groups) and registers it.
22050
- * @param {string} name Unique identifier.
22051
- * @param {TaskFunction} func Function.
22052
- * @param {ThrottleTagGetter} [throttledIdGetter=() => "default"] Optional getter for dynamic grouping (e.g., per-user).
22053
- * @param {string} [description] Optional.
22054
- * @param {TaskOptions} [options={}] Optional task options.
22055
- * @returns {Task} The created ThrottledTask.
22200
+ * 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.
22201
+ * This is useful for ensuring execution order and preventing race conditions.
22202
+ * See {@link Task} for more information.
22203
+ *
22204
+ * @param {string} name - The name of the task.
22205
+ * @param {TaskFunction} func - The function to be executed when the task runs.
22206
+ * @param {ThrottleTagGetter} [throttledIdGetter=() => "default"] - A function that generates a throttle tag identifier to group tasks for throttling.
22207
+ * @param {string} [description] - An optional description of the task.
22208
+ * @param {TaskOptions} [options={}] - Additional options to customize the task behavior.
22209
+ * @return {Task} The created throttled task.
22210
+ *
22211
+ * @example
22212
+ * ```ts
22213
+ * const task = Cadenza.createThrottledTask(
22214
+ * 'My task',
22215
+ * async (ctx) => {
22216
+ * await new Promise((resolve) => setTimeout(resolve, 1000));
22217
+ * console.log('My task executed with context:', ctx);
22218
+ * },
22219
+ * // Will throttle by the value of ctx.foo to make sure tasks with the same value are executed sequentially
22220
+ * (ctx) => ctx.foo,
22221
+ * );
22222
+ *
22223
+ * Cadenza.run(task, { foo: 'bar' }); // (First execution)
22224
+ * Cadenza.run(task, { foo: 'bar' }); // This will be executed after the first execution is finished
22225
+ * Cadenza.run(task, { foo: 'baz' }); // This will be executed in parallel with the first execution
22226
+ * ```
22056
22227
  */
22057
22228
  static createThrottledTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
22058
22229
  /**
22059
- * Creates and returns a throttled meta task with the specified name, function,
22060
- * and additional options. Throttling is determined based on the throttled ID getter.
22230
+ * Creates a throttled meta task with the specified configuration.
22231
+ * See {@link createThrottledTask} and {@link createMetaTask} for more information.
22061
22232
  *
22062
- * @param {string} name - The unique name for the meta task.
22063
- * @param {TaskFunction} func - The function to be executed as the task.
22064
- * @param {ThrottleTagGetter} [throttledIdGetter=() => "default"] - A function that determines the throttle ID for the task.
22233
+ * @param {string} name - The name of the throttled meta task.
22234
+ * @param {TaskFunction} func - The task function to be executed.
22235
+ * @param {ThrottleTagGetter} throttledIdGetter - A function to retrieve the throttling identifier.
22065
22236
  * @param {string} [description] - An optional description of the task.
22066
- * @param {TaskOptions} [options={}] - Additional configuration options for the task.
22237
+ * @param {TaskOptions} [options={}] - Additional options for configuring the task.
22067
22238
  * @return {Task} The created throttled meta task.
22068
22239
  */
22069
22240
  static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
22070
22241
  /**
22071
- * Creates a DebounceTask (delays exec until quiet period) and registers it.
22072
- * @param {string} name Identifier.
22073
- * @param {TaskFunction} func Function.
22074
- * @param {string} [description] Optional.
22075
- * @param {number} [debounceTime=1000] Delay in ms.
22076
- * @param {TaskOptions & DebounceOptions} [options={}] Optional task options plus optional debounce config (e.g., leading/trailing).
22077
- * @returns {Task} The created DebounceTask.
22242
+ * Creates and returns a new debounced task with the specified parameters.
22243
+ * This is useful to prevent rapid execution of tasks that may be triggered by multiple events within a certain time frame.
22244
+ * See {@link DebounceTask} for more information.
22245
+ *
22246
+ * @param {string} name - The unique name of the task to be created.
22247
+ * @param {TaskFunction} func - The function to be executed by the task.
22248
+ * @param {string} [description] - An optional description of the task.
22249
+ * @param {number} [debounceTime=1000] - The debounce time in milliseconds to delay the execution of the task.
22250
+ * @param {TaskOptions & DebounceOptions} [options={}] - Additional configuration options for the task, including debounce behavior and other task properties.
22251
+ * @return {DebounceTask} A new instance of the DebounceTask with the specified configuration.
22252
+ *
22253
+ * @example
22254
+ * ```ts
22255
+ * const task = Cadenza.createDebounceTask(
22256
+ * 'My debounced task',
22257
+ * (ctx) => {
22258
+ * console.log('My task executed with context:', ctx);
22259
+ * },
22260
+ * 'My debounced task description',
22261
+ * 100, // Debounce time in milliseconds. Default is 1000
22262
+ * {
22263
+ * leading: false, // Should the first execution of a burst be executed immediately? Default is false
22264
+ * trailing: true, // Should the last execution of a burst be executed? Default is true
22265
+ * maxWait: 1000, // Maximum time in milliseconds to wait for the next execution. Default is 0
22266
+ * },
22267
+ * );
22268
+ *
22269
+ * Cadenza.run(task, { foo: 'bar' }); // This will not be executed
22270
+ * Cadenza.run(task, { foo: 'bar' }); // This will not be executed
22271
+ * Cadenza.run(task, { foo: 'baz' }); // This execution will be delayed by 100ms
22272
+ * ```
22078
22273
  */
22079
22274
  static createDebounceTask(name: string, func: TaskFunction, description?: string, debounceTime?: number, options?: TaskOptions & DebounceOptions): DebounceTask;
22080
22275
  /**
22081
- * Creates a DebounceTask for the meta layer (delays exec until quiet period) and registers it.
22082
- * @param {string} name Identifier.
22083
- * @param {TaskFunction} func Function.
22084
- * @param {string} [description] Optional.
22085
- * @param {number} [debounceTime=1000] Delay in ms.
22086
- * @param {TaskOptions & DebounceOptions} [options={}] Optional task options plus optional debounce config (e.g., leading/trailing).
22087
- * @returns {Task} The created DebounceMetaTask.
22276
+ * Creates a debounced meta task with the specified parameters.
22277
+ * See {@link createDebounceTask} and {@link createMetaTask} for more information.
22278
+ *
22279
+ * @param {string} name - The name of the task.
22280
+ * @param {TaskFunction} func - The function to be executed by the task.
22281
+ * @param {string} [description] - Optional description of the task.
22282
+ * @param {number} [debounceTime=1000] - The debounce delay in milliseconds.
22283
+ * @param {TaskOptions & DebounceOptions} [options={}] - Additional configuration options for the task.
22284
+ * @return {DebounceTask} Returns an instance of the debounced meta task.
22088
22285
  */
22089
22286
  static createDebounceMetaTask(name: string, func: TaskFunction, description?: string, debounceTime?: number, options?: TaskOptions & DebounceOptions): DebounceTask;
22090
22287
  /**
22091
- * Creates an EphemeralTask (self-destructs after exec or condition) without default registration.
22092
- * Useful for transients; optionally register if needed.
22093
- * @param {string} name Identifier (may not be unique if not registered).
22094
- * @param {TaskFunction} func Function.
22095
- * @param {string} [description] Optional.
22096
- * @param {TaskOptions & EphemeralTaskOptions} [options={}] Optional task options.
22097
- * @returns {Task} The created EphemeralTask.
22288
+ * Creates an ephemeral task with the specified configuration.
22289
+ * Ephemeral tasks are designed to self-destruct after execution or a certain condition is met.
22290
+ * This is useful for transient tasks such as resolving promises or performing cleanup operations.
22291
+ * They are not registered by default.
22292
+ * See {@link EphemeralTask} for more information.
22293
+ *
22294
+ * @param {string} name - The name of the task to be created.
22295
+ * @param {TaskFunction} func - The function that defines the logic of the task.
22296
+ * @param {string} [description] - An optional description of the task.
22297
+ * @param {TaskOptions & EphemeralTaskOptions} [options={}] - The configuration options for the task, including concurrency, timeouts, and retry policies.
22298
+ * @return {EphemeralTask} The created ephemeral task instance.
22299
+ *
22300
+ * @example
22301
+ * By default, ephemeral tasks are executed once and destroyed after execution.
22302
+ * ```ts
22303
+ * const task = Cadenza.createEphemeralTask('My ephemeral task', (ctx) => {
22304
+ * console.log('My task executed with context:', ctx);
22305
+ * });
22306
+ *
22307
+ * Cadenza.run(task); // Executes the task once and destroys it after execution
22308
+ * Cadenza.run(task); // Does nothing, since the task is destroyed
22309
+ * ```
22310
+ *
22311
+ * Use destroy condition to conditionally destroy the task
22312
+ * ```ts
22313
+ * const task = Cadenza.createEphemeralTask(
22314
+ * 'My ephemeral task',
22315
+ * (ctx) => {
22316
+ * console.log('My task executed with context:', ctx);
22317
+ * },
22318
+ * 'My ephemeral task description',
22319
+ * {
22320
+ * once: false, // Should the task be executed only once? Default is true
22321
+ * destroyCondition: (ctx) => ctx.foo > 10, // Should the task be destroyed after execution? Default is undefined
22322
+ * },
22323
+ * );
22324
+ *
22325
+ * Cadenza.run(task, { foo: 5 }); // The task will not be destroyed and can still be executed
22326
+ * Cadenza.run(task, { foo: 10 }); // The task will not be destroyed and can still be executed
22327
+ * Cadenza.run(task, { foo: 20 }); // The task will be destroyed after execution and cannot be executed anymore
22328
+ * Cadenza.run(task, { foo: 30 }); // This will not be executed
22329
+ * ```
22330
+ *
22331
+ * A practical use case for ephemeral tasks is to resolve a promise upon some external event.
22332
+ * ```ts
22333
+ * const task = Cadenza.createTask('Confirm something', (ctx, emit) => {
22334
+ * return new Promise((resolve) => {
22335
+ * ctx.foo = uuid();
22336
+ *
22337
+ * Cadenza.createEphemeralTask(`Resolve promise of ${ctx.foo}`, (c) => {
22338
+ * console.log('My task executed with context:', ctx);
22339
+ * resolve(c);
22340
+ * }).doOn(`socket.confirmation_received:${ctx.foo}`);
22341
+ *
22342
+ * emit('this_domain.confirmation_requested', ctx);
22343
+ * });
22344
+ * });
22345
+ * ```
22098
22346
  */
22099
22347
  static createEphemeralTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions & EphemeralTaskOptions): EphemeralTask;
22100
22348
  /**
22101
- * Creates an EphemeralTask for the meta layer (self-destructs after exec or condition) without default registration.
22102
- * Useful for transients; optionally register if needed.
22103
- * @param {string} name Identifier (may not be unique if not registered).
22104
- * @param {TaskFunction} func Function.
22105
- * @param {string} [description] Optional.
22106
- * @param {TaskOptions & EphemeralTaskOptions} [options={}] Optional task options.
22107
- * @returns {Task} The created EphemeralMetaTask.
22349
+ * Creates an ephemeral meta task with the specified name, function, description, and options.
22350
+ * See {@link createEphemeralTask} and {@link createMetaTask} for more details.
22351
+ *
22352
+ * @param {string} name - The name of the task to be created.
22353
+ * @param {TaskFunction} func - The function to be executed as part of the task.
22354
+ * @param {string} [description] - An optional description of the task.
22355
+ * @param {TaskOptions & EphemeralTaskOptions} [options={}] - Additional options for configuring the task.
22356
+ * @return {EphemeralTask} The created ephemeral meta task.
22108
22357
  */
22109
22358
  static createEphemeralMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions & EphemeralTaskOptions): EphemeralTask;
22110
22359
  /**
22111
- * Creates a GraphRoutine (named entry to starting tasks) and registers it.
22112
- * @param {string} name Unique identifier.
22113
- * @param {Task[]} tasks Starting tasks.
22114
- * @param {string} [description] Optional.
22115
- * @returns {GraphRoutine} The created GraphRoutine.
22360
+ * Creates a new routine with the specified name, tasks, and an optional description.
22361
+ * Routines are named entry points to starting tasks and are registered in the GraphRegistry.
22362
+ * They are used to group tasks together and provide a high-level structure for organizing and managing the execution of a set of tasks.
22363
+ * See {@link GraphRoutine} for more information.
22364
+ *
22365
+ * @param {string} name - The name of the routine to create.
22366
+ * @param {Task[]} tasks - A list of tasks to include in the routine.
22367
+ * @param {string} [description=""] - An optional description for the routine.
22368
+ * @return {GraphRoutine} A new instance of the GraphRoutine containing the specified tasks and description.
22369
+ *
22370
+ * @example
22371
+ * ```ts
22372
+ * const task1 = Cadenza.createTask("Task 1", () => {});
22373
+ * const task2 = Cadenza.createTask("Task 2", () => {});
22374
+ *
22375
+ * task1.then(task2);
22376
+ *
22377
+ * const routine = Cadenza.createRoutine("Some routine", [task1]);
22378
+ *
22379
+ * Cadenza.run(routine);
22380
+ *
22381
+ * // Or, routines can be triggered by signals
22382
+ * routine.doOn("some.signal");
22383
+ *
22384
+ * Cadenza.emit("some.signal", {});
22385
+ * ```
22116
22386
  */
22117
22387
  static createRoutine(name: string, tasks: Task[], description?: string): GraphRoutine;
22118
22388
  /**
22119
- * Creates a GraphRoutine for the meta layer (named entry to starting tasks) and registers it.
22120
- * @param {string} name Unique identifier.
22121
- * @param {Task[]} tasks Starting tasks.
22122
- * @param {string} [description] Optional.
22123
- * @returns {GraphRoutine} The created GraphMetaRoutine.
22389
+ * Creates a meta routine with a given name, tasks, and optional description.
22390
+ * Routines are named entry points to starting tasks and are registered in the GraphRegistry.
22391
+ * They are used to group tasks together and provide a high-level structure for organizing and managing the execution of a set of tasks.
22392
+ * See {@link GraphRoutine} and {@link createRoutine} for more information.
22393
+ *
22394
+ * @param {string} name - The name of the routine to be created.
22395
+ * @param {Task[]} tasks - An array of tasks that the routine will consist of.
22396
+ * @param {string} [description=""] - An optional description for the routine.
22397
+ * @return {GraphRoutine} A new instance of the `GraphRoutine` representing the created routine.
22124
22398
  */
22125
22399
  static createMetaRoutine(name: string, tasks: Task[], description?: string): GraphRoutine;
22126
22400
  static reset(): void;