Skip to content

Commit

Permalink
Fix cancel action
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Nov 2, 2024
1 parent 94d44f0 commit 6242d5d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/actions/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
EventObject,
MachineContext,
ActionArgs,
ParameterizedObject
ParameterizedObject,
SpecialActionResolution
} from '../types.ts';

type ResolvableSendId<
Expand All @@ -26,18 +27,18 @@ function resolveCancel(
actionArgs: ActionArgs<any, any, any>,
actionParams: ParameterizedObject['params'] | undefined,
{ sendId }: { sendId: ResolvableSendId<any, any, any, any> }
) {
): SpecialActionResolution {
const resolvedSendId =
typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
return [snapshot, resolvedSendId];
return [snapshot, { sendId: resolvedSendId }];
}

export function executeCancel(
actorScope: AnyActorScope,
resolvedSendId: string
params: { sendId: string }
) {
actorScope.defer(() => {
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
actorScope.system.scheduler.cancel(actorScope.self, params.sendId);
});
}

Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2663,3 +2663,14 @@ export type ExecutableActionsFrom<T extends AnyActorLogic> =
: never;

export type ActionExecutor = (actionToExecute: ExecutableActionObject) => void;

export type SpecialActionResolution =
| [
AnyMachineSnapshot,
NonReducibleUnknown // params
]
| [
AnyMachineSnapshot,
NonReducibleUnknown, // params
UnknownAction[] | undefined
];
9 changes: 8 additions & 1 deletion packages/core/test/transition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,14 @@ describe('transition function', () => {
});

// TODO: tweak the assertion
expect(actions.map((a) => a.type)).toEqual({});
expect(actions).toContainEqual(
expect.objectContaining({
type: 'xstate.cancel',
params: expect.objectContaining({
sendId: 'myRaise'
})
})
);
});

// Copied from getSnapshot.test.ts
Expand Down

0 comments on commit 6242d5d

Please sign in to comment.