Skip to content

Commit

Permalink
add a failing boilerplate for cancel execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 30, 2024
1 parent 3474c5c commit 94d44f0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isDevelopment from '#is-development';
import { assign } from './actions.ts';
import { executeCancel } from './actions/cancel.ts';
import { executeRaise } from './actions/raise.ts';
import { executeSendTo } from './actions/send.ts';
import { createEmptyActor } from './actors/index.ts';
Expand Down Expand Up @@ -661,6 +662,9 @@ export class StateMachine<
actorScope.defer = (fn) => fn();
try {
switch (action.type) {
case 'xstate.cancel':
executeCancel(actorScope, action.params as any);
return;
case 'xstate.raise':
if (typeof (action as any).params.delay !== 'number') {
return;
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/actions/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function resolveCancel(
return [snapshot, resolvedSendId];
}

function executeCancel(actorScope: AnyActorScope, resolvedSendId: string) {
export function executeCancel(
actorScope: AnyActorScope,
resolvedSendId: string
) {
actorScope.defer(() => {
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,7 @@ export interface ExecutableSpawnAction extends ExecutableActionObject {
};
}

// TODO: cover all that can be actually returned
export type SpecialExecutableAction =
| ExecutableSpawnAction
| ExecutableRaiseAction
Expand Down
32 changes: 32 additions & 0 deletions packages/core/test/transition.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sleep } from '@xstate-repo/jest-utils';
import {
assign,
cancel,
createActor,
createMachine,
enqueueActions,
Expand Down Expand Up @@ -244,6 +245,37 @@ describe('transition function', () => {
await waitFor(actor, (s) => s.matches('b'));
});

it('cancel action should be returned and can be executed', async () => {
const machine = createMachine({
initial: 'a',
states: {
a: {
entry: raise({ type: 'NEXT' }, { delay: 10, id: 'myRaise' }),
on: {
NEXT: {
target: 'b',
actions: cancel('myRaise')
}
}
},
b: {}
}
});

const [state] = initialTransition(machine);

expect(state.value).toEqual('a');

const [, actions] = transition(machine, state, { type: 'NEXT' });

actions.forEach((action) => {
machine.executeAction(action);
});

// TODO: tweak the assertion
expect(actions.map((a) => a.type)).toEqual({});
});

// Copied from getSnapshot.test.ts

it('should calculate the next snapshot for transition logic', () => {
Expand Down

0 comments on commit 94d44f0

Please sign in to comment.