Skip to content

Commit

Permalink
Simplify passing of rootAbortSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
nibble-4bits committed Aug 28, 2023
1 parent a10b090 commit f82e74e
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 31 deletions.
10 changes: 0 additions & 10 deletions __tests__/StateExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -78,7 +77,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -118,7 +116,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const executorResult = stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -163,7 +160,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const executorResult = stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -203,7 +199,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -245,7 +240,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult, nextState } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -283,7 +277,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult, nextState } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -322,7 +315,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult, nextState } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -372,7 +364,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const executorResult = stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down Expand Up @@ -407,7 +398,6 @@ describe('State Executor', () => {
const stateExecutor = new StateExecutor('TaskState', stateDefinition);
const { stateResult, nextState } = await stateExecutor.execute(input, context, {
abortSignal: new AbortController().signal,
rootAbortSignal: new AbortController().signal,
stateMachineOptions: undefined,
runOptions: {
overrides: {
Expand Down
3 changes: 1 addition & 2 deletions __tests__/stateActions/MapStateAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ describe('Map State', () => {
const mapStateAction = new MapStateAction(definition);
const mapStateResult = mapStateAction.execute(input, context, {
stateMachineOptions: undefined,
runOptions: undefined,
rootAbortSignal: abortController.signal,
runOptions: { _rootAbortSignal: abortController.signal },
});

abortController.abort();
Expand Down
3 changes: 1 addition & 2 deletions __tests__/stateActions/ParallelStateAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ describe('Parallel State', () => {
const parallelStateAction = new ParallelStateAction(definition);
const parallelStateResult = parallelStateAction.execute(input, context, {
stateMachineOptions: undefined,
runOptions: undefined,
rootAbortSignal: abortController.signal,
runOptions: { _rootAbortSignal: abortController.signal },
});

abortController.abort();
Expand Down
6 changes: 2 additions & 4 deletions src/stateMachine/StateExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class StateExecutor {
// Handle `Retry` logic
const { shouldRetry, waitTimeBeforeRetry } = this.shouldRetry(error as RuntimeError);
if (shouldRetry && waitTimeBeforeRetry) {
await sleep(waitTimeBeforeRetry, options.abortSignal, options.rootAbortSignal);
await sleep(waitTimeBeforeRetry, options.abortSignal, options.runOptions?._rootAbortSignal);
return this.execute(input, context, options);
}

Expand Down Expand Up @@ -299,7 +299,6 @@ export class StateExecutor {
const executionResult = await parallelStateAction.execute(input, context, {
stateMachineOptions: options.stateMachineOptions,
runOptions: options.runOptions,
rootAbortSignal: options.rootAbortSignal,
});

return executionResult;
Expand All @@ -323,7 +322,6 @@ export class StateExecutor {
const executionResult = await mapStateAction.execute(input, context, {
stateMachineOptions: options.stateMachineOptions,
runOptions: options.runOptions,
rootAbortSignal: options.rootAbortSignal,
});

return executionResult;
Expand Down Expand Up @@ -369,7 +367,7 @@ export class StateExecutor {
const executionResult = await waitStateAction.execute(input, context, {
waitTimeOverrideOption,
abortSignal: options.abortSignal,
rootAbortSignal: options.rootAbortSignal,
rootAbortSignal: options.runOptions?._rootAbortSignal,
});

return executionResult;
Expand Down
3 changes: 1 addition & 2 deletions src/stateMachine/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ export class StateMachine {
input,
{
stateMachineOptions: this.stateMachineOptions,
runOptions: options,
runOptions: { ...options, _rootAbortSignal: options?._rootAbortSignal ?? abortController.signal },
abortSignal: abortController.signal,
rootAbortSignal: options?._rootAbortSignal ?? abortController.signal,
},
() => {
abortController.signal.removeEventListener('abort', onAbortHandler);
Expand Down
5 changes: 1 addition & 4 deletions src/stateMachine/stateActions/MapStateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ class MapStateAction extends BaseStateAction<MapState> {
}

// Pass the current parameter value if defined, otherwise pass the current item being iterated
const execution = stateMachine.run(paramValue ?? item, {
...options?.runOptions,
_rootAbortSignal: options?.rootAbortSignal,
});
const execution = stateMachine.run(paramValue ?? item, options?.runOptions);

this.executionAbortFuncs.push(execution.abort);

Expand Down
5 changes: 1 addition & 4 deletions src/stateMachine/stateActions/ParallelStateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ class ParallelStateAction extends BaseStateAction<ParallelState> {
...options?.stateMachineOptions,
validationOptions: { _noValidate: true },
});
const execution = stateMachine.run(input, {
...options?.runOptions,
_rootAbortSignal: options?.rootAbortSignal,
});
const execution = stateMachine.run(input, options?.runOptions);

this.executionAbortFuncs.push(execution.abort);

Expand Down
2 changes: 0 additions & 2 deletions src/typings/StateActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export type TaskStateActionOptions = {
export type ParallelStateActionOptions = {
stateMachineOptions: StateMachineOptions | undefined;
runOptions: RunOptions | undefined;
rootAbortSignal: AbortSignal | undefined;
};

export type MapStateActionOptions = {
stateMachineOptions: StateMachineOptions | undefined;
runOptions: RunOptions | undefined;
rootAbortSignal: AbortSignal | undefined;
};

export type PassStateActionOptions = Record<string, unknown>;
Expand Down
1 change: 0 additions & 1 deletion src/typings/StateMachineImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ export interface ExecuteOptions {
stateMachineOptions: StateMachineOptions | undefined;
runOptions: RunOptions | undefined;
abortSignal: AbortSignal;
rootAbortSignal: AbortSignal | undefined;
}

0 comments on commit f82e74e

Please sign in to comment.