@@ -21,6 +21,7 @@ import { Action as ConditionAction } from '@hatchet/protoc/v1/shared/condition';
2121import { HatchetClient } from '@hatchet/v1' ;
2222import { ContextWorker , NextStep } from '@hatchet/step' ;
2323import { applyNamespace } from '@hatchet/util/apply-namespace' ;
24+ import { createAbortError } from '@hatchet/util/abort-error' ;
2425import { V1Worker } from './worker-internal' ;
2526import { Duration } from '../duration' ;
2627
@@ -90,6 +91,12 @@ export class Context<T, K = {}> {
9091 return this . controller . signal . aborted ;
9192 }
9293
94+ protected throwIfCancelled ( ) : void {
95+ if ( this . abortController . signal . aborted ) {
96+ throw createAbortError ( 'Operation cancelled by AbortSignal' ) ;
97+ }
98+ }
99+
93100 async cancel ( ) {
94101 await this . v1 . runs . cancel ( {
95102 ids : [ this . action . taskRunExternalId ] ,
@@ -359,6 +366,8 @@ export class Context<T, K = {}> {
359366 }
360367
361368 private spawnOptions ( workflow : string | Workflow | WorkflowV1 < any , any > , options ?: ChildRunOpts ) {
369+ this . throwIfCancelled ( ) ;
370+
362371 let workflowName : string ;
363372
364373 if ( typeof workflow === 'string' ) {
@@ -410,6 +419,7 @@ export class Context<T, K = {}> {
410419 options ?: ChildRunOpts ;
411420 } >
412421 ) {
422+ this . throwIfCancelled ( ) ;
413423 const workflows : Parameters < typeof this . v1 . admin . runWorkflows < Q , P > > [ 0 ] = children . map (
414424 ( child ) => {
415425 const { workflowName, opts } = this . spawnOptions ( child . workflow , child . options ) ;
@@ -595,6 +605,7 @@ export class Context<T, K = {}> {
595605 options ?: ChildRunOpts ;
596606 } >
597607 ) : Promise < WorkflowRunRef < P > [ ] > {
608+ this . throwIfCancelled ( ) ;
598609 const { workflowRunId, taskRunExternalId } = this . action ;
599610
600611 const workflowRuns = workflows . map ( ( { workflow, input, options } ) => {
@@ -677,6 +688,7 @@ export class Context<T, K = {}> {
677688 input : Q ,
678689 options ?: ChildRunOpts
679690 ) : Promise < WorkflowRunRef < P > > {
691+ this . throwIfCancelled ( ) ;
680692 const { workflowRunId, taskRunExternalId } = this . action ;
681693
682694 let workflowName : string = '' ;
@@ -747,6 +759,7 @@ export class DurableContext<T, K = {}> extends Context<T, K> {
747759 * @returns A promise that resolves with the event that satisfied the conditions.
748760 */
749761 async waitFor ( conditions : Conditions | Conditions [ ] ) : Promise < Record < string , any > > {
762+ this . throwIfCancelled ( ) ;
750763 const pbConditions = conditionsToPb ( Render ( ConditionAction . CREATE , conditions ) ) ;
751764
752765 // eslint-disable-next-line no-plusplus
0 commit comments