Skip to content

Commit

Permalink
[v5] Behavior to logic (#4041)
Browse files Browse the repository at this point in the history
* behavior -> logic

# Conflicts:
#	packages/core/src/State.ts
#	packages/core/src/StateMachine.ts
#	packages/core/src/StateNode.ts
#	packages/core/src/actors/callback.ts
#	packages/core/src/actors/observable.ts
#	packages/core/src/types.ts
#	packages/core/src/utils.ts
#	packages/core/test/invoke.test.ts
#	packages/xstate-test/test/paths.test.ts

* Update packages/xstate-solid/test/createSpawn.test.tsx

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Rename

* createActorContext machine -> logic

* Promise test for createActorContext

* rename leftover type helper

* use ActorLogic in test+graph

* Rename remaining references to behaviors

* Changeset

---------

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
davidkpiano and Andarist committed Jun 1, 2023
1 parent c59bb6a commit 50fe8cd
Show file tree
Hide file tree
Showing 41 changed files with 378 additions and 383 deletions.
8 changes: 8 additions & 0 deletions .changeset/poor-bottles-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'xstate': major
'@xstate/fsm': major
'@xstate/react': major
'@xstate/vue': major
---

Instances of "behavior" in the codebase have been replaced with "actor logic".
12 changes: 6 additions & 6 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import type {
} from './typegenTypes.ts';
import type {
ActorContext,
ActorLogic,
ActorMap,
ActorBehavior,
EventObject,
InternalMachineImplementations,
InvokeActionObject,
Expand Down Expand Up @@ -72,7 +72,7 @@ export class StateMachine<
TActorMap
>
> implements
ActorBehavior<
ActorLogic<
TEvent,
State<TContext, TEvent, TResolvedTypesMeta>,
State<TContext, TEvent, TResolvedTypesMeta>,
Expand Down Expand Up @@ -418,17 +418,17 @@ export class StateMachine<
const childState = actorData.state;
const src = actorData.src;

const behavior = src
const logic = src
? resolveReferencedActor(this.options.actors[src])?.src
: undefined;

if (!behavior) {
if (!logic) {
return;
}

const actorState = behavior.restoreState?.(childState, _actorCtx);
const actorState = logic.restoreState?.(childState, _actorCtx);

const actorRef = interpret(behavior, {
const actorRef = interpret(logic, {
id: actorId,
state: actorState
});
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import type {
TransitionDefinitionMap,
InitialTransitionDefinition,
MachineContext,
BaseActionObject
BaseActionObject,
AnyActorLogic
} from './types.ts';
import type { State } from './State.ts';
import * as actionTypes from './actionTypes.ts';
Expand Down Expand Up @@ -250,15 +251,16 @@ export class StateNode<
}

/**
* The behaviors invoked as actors by this state node.
* The logic invoked as actors by this state node.
*/
public get invoke(): Array<InvokeDefinition<TContext, TEvent>> {
return memo(this, 'invoke', () =>
toArray(this.config.invoke).map((invocable, i) => {
const generatedId = createInvokeId(this.id, i);
const invokeConfig = toInvokeConfig(invocable, generatedId);
const resolvedId = invokeConfig.id || generatedId;
const { src, systemId } = invokeConfig;
const src = invokeConfig.src as string | AnyActorLogic;
const { systemId } = invokeConfig;

const resolvedSrc = isString(src)
? src
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/actors/callback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
InvokeCallback,
Receiver,
ActorBehavior,
ActorLogic,
EventObject,
AnyEventObject
} from '../types';
Expand All @@ -18,8 +18,8 @@ export interface CallbackInternalState {

export function fromCallback<TEvent extends EventObject>(
invokeCallback: InvokeCallback
): ActorBehavior<TEvent, undefined, CallbackInternalState> {
const behavior: ActorBehavior<TEvent, undefined, CallbackInternalState> = {
): ActorLogic<TEvent, undefined, CallbackInternalState> {
const logic: ActorLogic<TEvent, undefined, CallbackInternalState> = {
config: invokeCallback,
start: (_state, { self }) => {
self.send({ type: startSignalType } as TEvent);
Expand Down Expand Up @@ -89,5 +89,5 @@ export function fromCallback<TEvent extends EventObject>(
getPersistedState: ({ input }) => input
};

return behavior;
return logic;
}
8 changes: 4 additions & 4 deletions packages/core/src/actors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type LifecycleSignalType =
| typeof stopSignalType;

/**
* An object that expresses the behavior of an actor in reaction to received events,
* An object that expresses the actor logic in reaction to received events,
* as well as an optionally emitted stream of values.
*
* @template TReceived The received event
Expand All @@ -48,7 +48,7 @@ export function isActorRef(item: any): item is ActorRef<any> {

// TODO: refactor the return type, this could be written in a better way
// but it's best to avoid unneccessary breaking changes now
// @deprecated use `interpret(behavior)` instead
// @deprecated use `interpret(actorLogic)` instead
export function toActorRef<
TEvent extends EventObject,
TSnapshot = any,
Expand All @@ -70,8 +70,8 @@ export function toActorRef<
};
}

const emptyBehavior = fromTransition((_) => undefined, undefined);
const emptyLogic = fromTransition((_) => undefined, undefined);

export function createEmptyActor(): ActorRef<AnyEventObject, undefined> {
return interpret(emptyBehavior);
return interpret(emptyLogic);
}
23 changes: 9 additions & 14 deletions packages/core/src/actors/observable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Subscribable,
ActorBehavior,
EventObject,
Subscription
} from '../types';
import { Subscribable, ActorLogic, EventObject, Subscription } from '../types';
import { stopSignalType } from '../actors';

export interface ObservableInternalState<T> {
Expand All @@ -21,7 +16,7 @@ export type ObservablePersistedState<T> = Omit<
// TODO: this likely shouldn't accept TEvent, observable actor doesn't accept external events
export function fromObservable<T, TEvent extends EventObject>(
observableCreator: ({ input }: { input: any }) => Subscribable<T>
): ActorBehavior<
): ActorLogic<
TEvent,
T | undefined,
ObservableInternalState<T>,
Expand All @@ -32,7 +27,7 @@ export function fromObservable<T, TEvent extends EventObject>(
const completeEventType = '$$xstate.complete';

// TODO: add event types
const behavior: ActorBehavior<
const logic: ActorLogic<
any,
T | undefined,
ObservableInternalState<T>,
Expand Down Expand Up @@ -123,21 +118,21 @@ export function fromObservable<T, TEvent extends EventObject>(
})
};

return behavior;
return logic;
}

/**
* Creates an event observable behavior that listens to an observable
* Creates event observable logic that listens to an observable
* that delivers event objects.
*
*
* @param lazyObservable A function that creates an observable
* @returns An event observable behavior
* @returns Event observable logic
*/

export function fromEventObservable<T extends EventObject>(
lazyObservable: ({ input }: { input: any }) => Subscribable<T>
): ActorBehavior<
): ActorLogic<
EventObject,
T | undefined,
ObservableInternalState<T>,
Expand All @@ -147,7 +142,7 @@ export function fromEventObservable<T extends EventObject>(
const completeEventType = '$$xstate.complete';

// TODO: event types
const behavior: ActorBehavior<
const logic: ActorLogic<
any,
T | undefined,
ObservableInternalState<T>,
Expand Down Expand Up @@ -226,5 +221,5 @@ export function fromEventObservable<T extends EventObject>(
})
};

return behavior;
return logic;
}
8 changes: 4 additions & 4 deletions packages/core/src/actors/promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActorBehavior } from '../types';
import { ActorLogic } from '../types';
import { stopSignalType } from '../actors';

export interface PromiseInternalState<T> {
Expand All @@ -10,12 +10,12 @@ export interface PromiseInternalState<T> {
export function fromPromise<T>(
// TODO: add types
promiseCreator: ({ input }: { input: any }) => PromiseLike<T>
): ActorBehavior<{ type: string }, T | undefined, PromiseInternalState<T>> {
): ActorLogic<{ type: string }, T | undefined, PromiseInternalState<T>> {
const resolveEventType = '$$xstate.resolve';
const rejectEventType = '$$xstate.reject';

// TODO: add event types
const behavior: ActorBehavior<
const logic: ActorLogic<
| {
type: typeof resolveEventType;
data: T;
Expand Down Expand Up @@ -99,5 +99,5 @@ export function fromPromise<T>(
restoreState: (state) => state
};

return behavior;
return logic;
}
17 changes: 6 additions & 11 deletions packages/core/src/actors/transition.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import {
ActorBehavior,
ActorContext,
ActorSystem,
EventObject
} from '../types';
import { ActorLogic, ActorContext, ActorSystem, EventObject } from '../types';

/**
* Returns an actor behavior from a transition function and its initial state.
* Returns actor logic from a transition function and its initial state.
*
* A transition function is a function that takes the current state and an event and returns the next state.
*
* @param transition The transition function that returns the next state given the current state and event.
* @param initialState The initial state of the transition function.
* @returns An actor behavior
* @returns Actor logic
*/
export function fromTransition<
TState,
Expand All @@ -25,8 +20,8 @@ export function fromTransition<
actorContext: ActorContext<TEvent, TState, TSystem>
) => TState,
initialState: TState | (({ input }: { input: any }) => TState) // TODO: type
): ActorBehavior<TEvent, TState, TState> {
const behavior: ActorBehavior<TEvent, TState, TState, TState> = {
): ActorLogic<TEvent, TState, TState> {
const logic: ActorLogic<TEvent, TState, TState, TState> = {
config: transition,
transition: (state, event, actorContext) => {
return transition(state, event as TEvent, actorContext as any);
Expand All @@ -41,5 +36,5 @@ export function fromTransition<
restoreState: (state) => state
};

return behavior;
return logic;
}
2 changes: 1 addition & 1 deletion packages/core/src/dev/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createReduxDevTools = (
...(options ? options.features : undefined)
}
},
service.behavior
service.logic
);

service.subscribe((state) => {
Expand Down
Loading

0 comments on commit 50fe8cd

Please sign in to comment.