Skip to content

Commit b978085

Browse files
committed
Allow skipping cancelation of some child jobs
1 parent d561c2c commit b978085

File tree

1 file changed

+18
-9
lines changed
  • packages/action-listener-middleware/src

1 file changed

+18
-9
lines changed

packages/action-listener-middleware/src/job.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type JobFunc<T> = (job: JobHandle) => Promise<Outcome<T>>
1313
* A handle for the current job used in [JobFunc]. This interface is equivalent to [Job]'s interface with the exception
1414
* of [run] and [runWithTimeout] to prevent recursive running of the [Job] inside its [JobFunc].
1515
*/
16-
interface JobHandle {
16+
export interface JobHandle {
1717
isActive: boolean
1818
isCompleted: boolean
1919
isCancelled: boolean
@@ -24,7 +24,10 @@ interface JobHandle {
2424
pause<R>(func: Promise<R>): Promise<R>
2525
delay(milliseconds: number): Promise<void>
2626
cancel(reason?: JobCancellationException): void
27-
cancelChildren(reason?: JobCancellationException): void
27+
cancelChildren(
28+
reason?: JobCancellationException,
29+
skipChildren?: JobHandle[]
30+
): void
2831
}
2932

3033
/**
@@ -281,14 +284,20 @@ export class Job<T> implements JobHandle {
281284
/**
282285
* Cancels all children jobs without cancelling the current job.
283286
*/
284-
cancelChildren(reason?: JobCancellationException) {
287+
cancelChildren(
288+
reason?: JobCancellationException,
289+
skipChildren: JobHandle[] = []
290+
) {
285291
const childrenCopy = [...this._children]
286-
childrenCopy.forEach((job) =>
287-
job.cancel(
288-
reason ??
289-
new JobCancellationException(JobCancellationReason.JobCancelled)
290-
)
291-
)
292+
const skipSet = new Set(skipChildren)
293+
childrenCopy.forEach((job) => {
294+
if (!skipSet.has(job)) {
295+
job.cancel(
296+
reason ??
297+
new JobCancellationException(JobCancellationReason.JobCancelled)
298+
)
299+
}
300+
})
292301
this._children = []
293302
}
294303

0 commit comments

Comments
 (0)