Skip to content

Commit

Permalink
Clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Sep 28, 2024
1 parent 96f4ac4 commit 9c077d6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
21 changes: 13 additions & 8 deletions packages/core/src/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
EventFromLogic,
InputFrom,
SnapshotFrom,
ExecutableActionObject
ExecutableActionObject,
ExecutableActionsFrom,
AnyStateMachine
} from './types';

/**
Expand All @@ -17,12 +19,12 @@ export function transition<T extends AnyActorLogic>(
logic: T,
snapshot: SnapshotFrom<T>,
event: EventFromLogic<T>
): [nextSnapshot: SnapshotFrom<T>, actions: ExecutableActionObject[]] {
const executableActions = [] as ExecutableActionObject[];
): [nextSnapshot: SnapshotFrom<T>, actions: ExecutableActionsFrom<T>[]] {
const executableActions = [] as ExecutableActionsFrom<T>[];

const actorScope = createInertActorScope(logic);
actorScope.actionExecutor = (action) => {
executableActions.push(action);
executableActions.push(action as ExecutableActionsFrom<T>);
};

const nextSnapshot = logic.transition(snapshot, event, actorScope);
Expand All @@ -42,15 +44,18 @@ export function initialTransition<T extends AnyActorLogic>(
...[input]: undefined extends InputFrom<T>
? [input?: InputFrom<T>]
: [input: InputFrom<T>]
): [SnapshotFrom<T>, ExecutableActionObject[]] {
const executableActions = [] as ExecutableActionObject[];
): [SnapshotFrom<T>, ExecutableActionsFrom<T>[]] {
const executableActions = [] as ExecutableActionsFrom<T>[];

const actorScope = createInertActorScope(logic);
actorScope.actionExecutor = (action) => {
executableActions.push(action);
executableActions.push(action as ExecutableActionsFrom<T>);
};

const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
const nextSnapshot = logic.getInitialSnapshot(
actorScope,
input
) as SnapshotFrom<T>;

return [nextSnapshot, executableActions];
}
13 changes: 11 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,13 @@ export interface ExecutableActionObject {
| undefined;
}

export interface ToExecutableAction<T extends ParameterizedObject>
extends ExecutableActionObject {
type: T['type'];
params: T['params'];
exec: undefined;
}

export interface ExecutableSpawnAction extends ExecutableActionObject {
type: 'xstate.spawn';
info: ActionArgs<MachineContext, EventObject, EventObject>;
Expand All @@ -2647,7 +2654,7 @@ export type SpecialExecutableAction =
| ExecutableSpawnAction
| ExecutableRaiseAction;

export type ExecutableActionsFrom<T extends AnyStateMachine> =
export type ExecutableActionsFrom<T extends AnyActorLogic> =
T extends StateMachine<
infer _TContext,
infer _TEvent,
Expand All @@ -2664,7 +2671,9 @@ export type ExecutableActionsFrom<T extends AnyStateMachine> =
infer _TMeta,
infer _TConfig
>
? SpecialExecutableAction | { type: unknown; params: unknown }
?
| SpecialExecutableAction
| (string extends TAction['type'] ? never : ToExecutableAction<TAction>)
: never;

export type ActionExecutor = (
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/transition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('transition function', () => {

const calls: string[] = [];

async function execute(action: ExecutableActionObject) {
async function execute(action: ExecutableActionsFrom<typeof machine>) {
switch (action.type) {
case 'xstate.spawn': {
const spawnAction = action as ExecutableSpawnAction;
Expand Down

0 comments on commit 9c077d6

Please sign in to comment.