Skip to content

Commit

Permalink
make it green, make it green
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 25, 2024
1 parent 71324ad commit ca2977f
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 252 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import type {
StateMachineDefinition,
StateValue,
TransitionDefinition,
UnknownActionObject,
ResolvedStateMachineTypes,
StateSchema,
SnapshotStatus,
Expand Down Expand Up @@ -389,7 +388,7 @@ export class StateMachine<
preInitial,
initEvent,
actorScope,
[assign(assignment) as unknown as UnknownActionObject],
[assign(assignment)],
internalQueue,
undefined
) as SnapshotFrom<this>;
Expand Down
69 changes: 28 additions & 41 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NULL_EVENT, STATE_DELIMITER } from './constants.ts';
import { evaluateGuard } from './guards.ts';
import { memo } from './memo.ts';
import {
convertAction,
BuiltinAction,
formatInitialTransition,
formatTransition,
formatTransitions,
Expand All @@ -31,8 +31,7 @@ import type {
AnyStateNodeConfig,
ProvidedActor,
NonReducibleUnknown,
EventDescriptor,
UnknownActionObject
EventDescriptor
} from './types.ts';
import {
createInvokeId,
Expand All @@ -43,6 +42,21 @@ import {

const EMPTY_OBJECT = {};

const toSerializableAction = (action: UnknownAction) => {
if (typeof action === 'string') {
return { type: action };
}
if (typeof action === 'function') {
if ('resolve' in action) {
return { type: (action as BuiltinAction).type };
}
return {
type: action.name
};
}
return action;
};

interface StateNodeOptions<
TContext extends MachineContext,
TEvent extends EventObject
Expand Down Expand Up @@ -85,9 +99,9 @@ export class StateNode<
*/
public history: false | 'shallow' | 'deep';
/** The action(s) to be executed upon entering the state node. */
public entry: UnknownActionObject[];
public entry: UnknownAction[];
/** The action(s) to be executed upon exiting the state node. */
public exit: UnknownActionObject[];
public exit: UnknownAction[];
/** The parent state node. */
public parent?: StateNode<TContext, TEvent>;
/** The root machine node. */
Expand Down Expand Up @@ -196,32 +210,8 @@ export class StateNode<
this.history =
this.config.history === true ? 'shallow' : this.config.history || false;

this.entry = toArray(this.config.entry as UnknownAction).map(
(action, actionIndex) => {
const actionObject = convertAction(
action,
this,
undefined,
'entry',
0,
actionIndex
);
return actionObject;
}
);
this.exit = toArray(this.config.exit as UnknownAction).map(
(action, actionIndex) => {
const actionObject = convertAction(
action,
this,
undefined,
'exit',
0,
actionIndex
);
return actionObject;
}
);
this.entry = toArray(this.config.entry).slice();
this.exit = toArray(this.config.exit).slice();

this.meta = this.config.meta;
this.output =
Expand All @@ -233,8 +223,8 @@ export class StateNode<
public _initialize() {
this.transitions = formatTransitions(this);
if (this.config.always) {
this.always = toTransitionConfigArray(this.config.always).map((t, i) =>
formatTransition(this, NULL_EVENT, t, i)
this.always = toTransitionConfigArray(this.config.always).map((t) =>
formatTransition(this, NULL_EVENT, t)
);
}

Expand All @@ -254,13 +244,13 @@ export class StateNode<
? {
target: this.initial.target,
source: this,
actions: this.initial.actions,
actions: this.initial.actions.map(toSerializableAction),
eventType: null as any,
reenter: false,
toJSON: () => ({
target: this.initial.target.map((t) => `#${t.id}`),
source: `#${this.id}`,
actions: this.initial.actions,
actions: this.initial.actions.map(toSerializableAction),
eventType: null as any
})
}
Expand All @@ -274,8 +264,8 @@ export class StateNode<
...t,
actions: t.actions
})),
entry: this.entry,
exit: this.exit,
entry: this.entry.map(toSerializableAction),
exit: this.exit.map(toSerializableAction),
meta: this.meta,
order: this.order || -1,
output: this.output,
Expand Down Expand Up @@ -311,9 +301,6 @@ export class StateNode<
typeof src === 'string'
? src
: `xstate.invoke.${createInvokeId(this.id, i)}`;
if (typeof src !== 'string') {
this.machine.implementations.actors[sourceName] = src;
}

return {
...invokeConfig,
Expand Down Expand Up @@ -390,7 +377,7 @@ export class StateNode<
event: TEvent
): TransitionDefinition<TContext, TEvent>[] | undefined {
const eventType = event.type;
const actions: UnknownActionObject[] = [];
const actions: UnknownAction[] = [];

let selectedTransition: TransitionDefinition<TContext, TEvent> | undefined;

Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/actions/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,5 @@ export function assign<

assign.resolve = resolveAssign;

assign.toJSON = () => ({
...assign
});

return assign;
}
4 changes: 0 additions & 4 deletions packages/core/src/actions/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,5 @@ export function cancel<
cancel.resolve = resolveCancel;
cancel.execute = executeCancel;

cancel.toJSON = () => ({
...cancel
});

return cancel;
}
4 changes: 0 additions & 4 deletions packages/core/src/actions/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,5 @@ export function emit<
emit.resolve = resolveEmit;
emit.execute = executeEmit;

emit.toJSON = () => ({
...emit
});

return emit;
}
16 changes: 1 addition & 15 deletions packages/core/src/actions/enqueueActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import isDevelopment from '#is-development';
import { Guard, evaluateGuard } from '../guards.ts';
import { convertAction } from '../stateUtils.ts';
import {
Action,
ActionArgs,
Expand Down Expand Up @@ -136,16 +135,7 @@ function resolveEnqueueActions(
const enqueue: Parameters<typeof collect>[0]['enqueue'] = function enqueue(
action
) {
actions.push(
convertAction(
action as any,
snapshot.machine.root,
'enqueue' + Math.random(), // TODO: this should come from state node ID which isn't provided
undefined,
0,
actions.length
)
);
actions.push(action);
};
enqueue.assign = (...args) => {
actions.push(assign(...args));
Expand Down Expand Up @@ -333,9 +323,5 @@ export function enqueueActions<
enqueueActions.collect = collect;
enqueueActions.resolve = resolveEnqueueActions;

enqueueActions.toJSON = () => ({
...enqueueActions
});

return enqueueActions;
}
4 changes: 0 additions & 4 deletions packages/core/src/actions/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,5 @@ export function log<
log.resolve = resolveLog;
log.execute = executeLog;

log.toJSON = () => ({
...log
});

return log;
}
6 changes: 1 addition & 5 deletions packages/core/src/actions/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function resolveRaise(
];
}

function executeRaise(
export function executeRaise(
actorScope: AnyActorScope,
params: {
event: EventObject;
Expand Down Expand Up @@ -174,10 +174,6 @@ export function raise<
raise.resolve = resolveRaise;
raise.execute = executeRaise;

raise.toJSON = () => ({
...raise
});

return raise;
}

Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function retryResolveSendTo(
}
}

function executeSendTo(
export function executeSendTo(
actorScope: AnyActorScope,
params: {
to: AnyActorRef;
Expand Down Expand Up @@ -270,10 +270,6 @@ export function sendTo<
sendTo.retryResolve = retryResolveSendTo;
sendTo.execute = executeSendTo;

sendTo.toJSON = () => ({
...sendTo
});

return sendTo;
}

Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/actions/spawnChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ function executeSpawn(
}

actorScope.defer(() => {
actorRef._parent = actorScope.self;
actorRef.system = actorScope.system;
if (actorRef._processingStatus === ProcessingStatus.Stopped) {
return;
}
Expand Down Expand Up @@ -222,7 +220,7 @@ export function spawnChild<
}
}

spawnChild.type = 'xstate.spawn';
spawnChild.type = 'xstate.spawnChild';
spawnChild.id = id;
spawnChild.systemId = systemId;
spawnChild.src = src;
Expand All @@ -232,9 +230,5 @@ export function spawnChild<
spawnChild.resolve = resolveSpawn;
spawnChild.execute = executeSpawn;

spawnChild.toJSON = () => ({
...spawnChild
});

return spawnChild;
}
4 changes: 0 additions & 4 deletions packages/core/src/actions/stopChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ export function stopChild<
stop.resolve = resolveStop;
stop.execute = executeStop;

stop.toJSON = () => ({
...stop
});

return stop;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export {
type Interpreter,
type RequiredActorOptionsKeys as RequiredActorOptionsKeys
} from './createActor.ts';
// TODO: decide from where those should be exported
export { createMachine } from './createMachine.ts';
export { getInitialSnapshot, getNextSnapshot } from './getNextSnapshot.ts';
export { and, not, or, stateIn } from './guards.ts';
Expand All @@ -34,7 +33,7 @@ export {
pathToStateValue,
toObserver
} from './utils.ts';
export { transition } from './transition.ts';
export { transition, initialTransition } from './transition.ts';
export { waitFor } from './waitFor.ts';

declare global {
Expand Down
Loading

0 comments on commit ca2977f

Please sign in to comment.