Skip to content

Commit 325711f

Browse files
committed
[manager] untilTaskRunDone
1 parent 9903805 commit 325711f

File tree

5 files changed

+633
-488
lines changed

5 files changed

+633
-488
lines changed

src/@types/docs.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,38 @@
11041104
* @since v1.0.0
11051105
*/
11061106
/// Manager.getTaskRunRunning
1107+
/**
1108+
* The untilTaskRunDone method returns a Promise that resolves when a task run
1109+
* has finished running, regardless of whether it was successful or not.
1110+
*
1111+
* This is useful if you want to have a parent task that will keep running
1112+
* until its child tasks have concluded.
1113+
*
1114+
* In this context, 'done' means that the test run succeeded, deleted or
1115+
* failed (and, if relevant, all retries have been attempted). If a task is
1116+
* configured to 'repeat', future iterations are not included and the promise
1117+
* will resolve after the first is done.
1118+
* @param taskRunId The Id of the task run to await.
1119+
* @returns A promise that will resolve when the task run finishes.
1120+
* @example
1121+
* This example demonstrates using untilTaskRunDone to wait for a task to
1122+
* complete before continuing execution.
1123+
*
1124+
* ```js
1125+
* import {createManager} from 'tinytick';
1126+
*
1127+
* const manager = createManager().start();
1128+
* manager.setTask('ping', async () => await fetch('https://example.org'));
1129+
*
1130+
* const taskRunId = manager.scheduleTaskRun('ping');
1131+
* await manager.untilTaskRunDone(taskRunId);
1132+
*
1133+
* // Continue execution
1134+
* ```
1135+
* @category TaskRun
1136+
* @since v1.2.4
1137+
*/
1138+
/// Manager.untilTaskRunDone
11071139
/**
11081140
* The delTaskRun method deletes a scheduled task run or aborts a running one.
11091141
* @param taskRunId The Id of the task run to delete or abort.

src/@types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ export interface Manager {
209209
/// Manager.getTaskRunRunning
210210
getTaskRunRunning(taskRunId: Id): boolean | undefined;
211211

212+
/// Manager.untilTaskRunDone
213+
untilTaskRunDone(taskRunId: Id): Promise<void>;
214+
212215
/// Manager.delTaskRun
213216
delTaskRun(taskRunId: Id): Manager;
214217

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,26 @@ export const createManager: typeof createManagerDecl = (): Manager => {
668668
const getTaskRunRunning = (taskRunId: Id): boolean | undefined =>
669669
mapGet(taskRunMap, id(taskRunId))?.[TaskRunPositions.Running];
670670

671+
const untilTaskRunDone = (taskRunId: Id): Promise<void> =>
672+
new Promise((resolve) =>
673+
ifNotUndefined(
674+
mapGet(taskRunMap, taskRunId),
675+
([taskId]) => {
676+
const listenerId = addTaskRunRunningListener(
677+
taskId,
678+
taskRunId,
679+
(_1, _2, _3, running) => {
680+
if (isUndefined(running)) {
681+
delListener(listenerId);
682+
resolve();
683+
}
684+
},
685+
);
686+
},
687+
resolve,
688+
),
689+
);
690+
671691
const delTaskRun = (taskRunId: Id): Manager =>
672692
fluent((taskRunId) => {
673693
delTaskRunImpl(taskRunId);
@@ -746,6 +766,7 @@ export const createManager: typeof createManagerDecl = (): Manager => {
746766
getTaskRunConfig,
747767
getTaskRunInfo,
748768
getTaskRunRunning,
769+
untilTaskRunDone,
749770
delTaskRun,
750771

751772
getScheduledTaskRunIds,

0 commit comments

Comments
 (0)