Skip to content

Commit

Permalink
Remove switch statement in executeAction
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Nov 12, 2024
1 parent b47f77f commit c04c12c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 69 deletions.
46 changes: 11 additions & 35 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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';
import { $$ACTOR_TYPE, createActor } from './createActor.ts';
import { createInitEvent } from './eventUtils.ts';
Expand All @@ -22,7 +19,7 @@ import {
macrostep,
microstep,
resolveActionsAndContext,
resolveReferencedAction,
getAction,
resolveStateValue,
transitionNode
} from './stateUtils.ts';
Expand Down Expand Up @@ -657,37 +654,16 @@ export class StateMachine<
action: ExecutableActionObject,
actor: AnyActorRef = createEmptyActor()
) {
const actorScope = (actor as any)._actorScope as AnyActorScope;
const defer = actorScope.defer;
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;
}
executeRaise(actorScope, action.params as any);
return;
case 'xstate.sendTo':
executeSendTo(actorScope, action.params as any);
return;
}
const resolvedInfo = {
...action.info,
self: actor,
system: actor.system
};
if (action.exec) {
action.exec?.(resolvedInfo, action.params);
} else {
const resolvedAction = resolveReferencedAction(this, action.type)!;
resolvedAction(resolvedInfo, action.params);
}
} finally {
actorScope.defer = defer;
const resolvedInfo = {
...action.info,
self: actor,
system: actor.system
};
if (action.exec) {
action.exec?.(resolvedInfo, action.params);
} else {
const resolvedAction = getAction(this, action.type)!;
resolvedAction(resolvedInfo, action.params);
}
}
}
5 changes: 1 addition & 4 deletions packages/core/src/actions/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ function resolveCancel(
return [snapshot, { sendId: resolvedSendId }, undefined];
}

export function executeCancel(
actorScope: AnyActorScope,
params: { sendId: string }
) {
function executeCancel(actorScope: AnyActorScope, params: { sendId: string }) {
actorScope.defer(() => {
actorScope.system.scheduler.cancel(actorScope.self, params.sendId);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function resolveRaise(
];
}

export function executeRaise(
function executeRaise(
actorScope: AnyActorScope,
params: {
event: EventObject;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function retryResolveSendTo(
}
}

export function executeSendTo(
function executeSendTo(
actorScope: AnyActorScope,
params: {
to: AnyActorRef;
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/stateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,10 +1486,7 @@ export interface BuiltinAction {
execute: (actorScope: AnyActorScope, params: unknown) => void;
}

export function resolveReferencedAction(
machine: AnyStateMachine,
actionType: string
) {
export function getAction(machine: AnyStateMachine, actionType: string) {
return machine.implementations.actions[actionType];
}

Expand All @@ -1515,10 +1512,7 @@ function resolveAndExecuteActionsWithContext(
// it's fine to cast this here to get a common type and lack of errors in the rest of the code
// our logic below makes sure that we call those 2 "variants" correctly

resolveReferencedAction(
machine,
typeof action === 'string' ? action : action.type
);
getAction(machine, typeof action === 'string' ? action : action.type);
const actionArgs = {
context: intermediateSnapshot.context,
event,
Expand Down
20 changes: 0 additions & 20 deletions packages/core/test/transition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,6 @@ describe('transition function', () => {
})
})
);

const actor = createActor(machine, {
snapshot: state
}).start();

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

await waitFor(actor, (s) => s.matches('b'));
});

it('raise actions related to delayed transitions should be returned', async () => {
Expand All @@ -236,16 +226,6 @@ describe('transition function', () => {
})
})
);

const actor = createActor(machine, {
snapshot: state
}).start();

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

await waitFor(actor, (s) => s.matches('b'));
});

it('cancel action should be returned and can be executed', async () => {
Expand Down

0 comments on commit c04c12c

Please sign in to comment.