#Move logic into state machine?

1 messages · Page 1 of 1 (latest)

normal fjord
#

I'm new to using state machines and im still getting used to them.
Should I move things like this into a some sort of toggle action?
Or is it fine like this?

    const togglePlay = () => {
        if (editorState.matches("Playing")) {
            editorActorRef.send({ type: "pause" })
            return
        }

        editorActorRef.send({ type: "play" })
    }
faint rapids
#

You can do that at first but IMO it's better to keep the logic centralized in the machine

#
Playing: {
  on: { toggle: { target: 'Paused' } }
},
Paused: {
  on: { toggle: { target: 'Playing' } }
}
normal fjord
# faint rapids ```ts Playing: { on: { toggle: { target: 'Paused' } } }, Paused: { on: { tog...

One question for that. Right now I have the actions running on the events, as shown in most examples aswell.
But that means if I have something like this for toggling I do something like:

Playing: {
  on: { toggle: { target: 'Paused' }, actions: {
type: "onPause"} }
},
Paused: {
  on: { toggle: { target: 'Playing' }, actions: {
type: "onPlay"} }
}

Would it be wrong from your point of view to put the actions on state, so I dont have to repeat myself with calling the actions?