Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reopen #72 #76 #86

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cassiozen/usestatemachine",
"version": "1.0.1",
"version": "2.0.0",
"license": "MIT",
"author": "Cassio Zen",
"main": "dist/index.js",
Expand Down
56 changes: 30 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useReducer } from "react";
import { UseStateMachine, Machine, $$t } from "./types";
import { UseStateMachine, Machine, $$t, O } from "./types";
import { assertNever, R, useConstant } from "./extras";


const useStateMachineImpl = (definition: Machine.Definition.Impl) => {
const [state, dispatch] = useReducer(createReducer(definition), createInitialState(definition));
const [machineInstant, dispatch] = useReducer(createReducer(definition), createInitialState(definition));

const send = useConstant(() => (sendable: Machine.Sendable.Impl) => dispatch({ type: "SEND", sendable }));

Expand All @@ -13,30 +14,33 @@ const useStateMachineImpl = (definition: Machine.Definition.Impl) => {
};

useEffect(() => {
const entry = R.get(definition.states, state.value)!.effect;
const entry = R.get(definition.states, machineInstant.state)!.effect;
let exit = entry?.({
send,
setContext,
event: state.event,
context: state.context,
event: machineInstant.event,
context: machineInstant.context,
});

return typeof exit === "function"
? () => exit?.({ send, setContext, event: state.event, context: state.context })
? () => exit?.({ send, setContext, event: machineInstant.event, context: machineInstant.context })
: undefined;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.value, state.event]);
}, [machineInstant.state, machineInstant.event]);

return [state, send];
return { ...machineInstant, send };
};

const createInitialState = (definition: Machine.Definition.Impl): Machine.State.Impl => {
type MachineInstant =
O.OmitKey<Machine.Impl, "send">

const createInitialState = (definition: Machine.Definition.Impl): MachineInstant => {
let nextEvents = R.keys(R.concat(
R.fromMaybe(R.get(definition.states, definition.initial)!.on),
R.fromMaybe(definition.on)
))
return {
value: definition.initial,
state: definition.initial,
context: definition.context as Machine.Context.Impl,
event: { type: "$$initial" } as Machine.Event.Impl,
nextEvents: nextEvents,
Expand All @@ -46,56 +50,56 @@ const createInitialState = (definition: Machine.Definition.Impl): Machine.State.

const createReducer = (definition: Machine.Definition.Impl) => {
let log = createLogger(definition);
return (machineState: Machine.State.Impl, internalEvent: InternalEvent): Machine.State.Impl => {
return (machineInstant: MachineInstant, internalEvent: InternalEvent): MachineInstant => {
if (internalEvent.type === "SET_CONTEXT") {
let nextContext = internalEvent.updater(machineState.context);
log("Context update", ["Previous Context", machineState.context], ["Next Context", nextContext]);
let nextContext = internalEvent.updater(machineInstant.context);
log("Context update", ["Previous Context", machineInstant.context], ["Next Context", nextContext]);

return { ...machineState, context: nextContext };
return { ...machineInstant, context: nextContext };
}

if (internalEvent.type === "SEND") {
let sendable = internalEvent.sendable;
let event = typeof sendable === "string" ? { type: sendable } : sendable;
let context = machineState.context;
let stateNode = R.get(definition.states, machineState.value)!;
let context = machineInstant.context;
let stateNode = R.get(definition.states, machineInstant.state)!;
let resolvedTransition =
R.get(R.fromMaybe(stateNode.on), event.type) ?? R.get(R.fromMaybe(definition.on), event.type);

if (!resolvedTransition) {
log(
`Current state doesn't listen to event type "${event.type}".`,
["Current State", machineState],
["Current State", machineInstant],
["Event", event]
);
return machineState;
return machineInstant;
}

let [nextStateValue, didGuardDeny = false] = (() => {
let [nextState, didGuardDeny = false] = (() => {
if (typeof resolvedTransition === "string") return [resolvedTransition];
if (resolvedTransition.guard === undefined) return [resolvedTransition.target];
if (resolvedTransition.guard({ context, event })) return [resolvedTransition.target];
return [resolvedTransition.target, true]
})() as [Machine.StateValue.Impl, true?]
})() as [Machine.State.Impl, true?]

if (didGuardDeny) {
log(
`Transition from "${machineState.value}" to "${nextStateValue}" denied by guard`,
`Transition from "${machineInstant.state}" to "${nextState}" denied by guard`,
["Event", event],
["Context", context]
);
return machineState;
return machineInstant;
}
log(`Transition from "${machineState.value}" to "${nextStateValue}"`, ["Event", event]);
log(`Transition from "${machineInstant.state}" to "${nextState}"`, ["Event", event]);

let resolvedStateNode = R.get(definition.states, nextStateValue)!;
let resolvedStateNode = R.get(definition.states, nextState)!;

let nextEvents = R.keys(R.concat(
R.fromMaybe(resolvedStateNode.on),
R.fromMaybe(definition.on)
));
return {
value: nextStateValue,
state: nextState,
context,
event,
nextEvents,
Expand Down Expand Up @@ -144,4 +148,4 @@ const createLogger = (definition: Machine.Definition.Impl) => (groupLabel: strin
const useStateMachine = useStateMachineImpl as unknown as UseStateMachine;
export default useStateMachine;

export const t = <T extends unknown>() => ({ [$$t]: undefined as T })
export const t = <T>() => ({ [$$t]: undefined as unknown as T })
Loading