Skip to content

Curried FSM StateFunction #11

Merged
cloudmark merged 1 commit intocloudmark:mainfrom
PetrosPapapa:feature/FSM-curried-state-function
Aug 23, 2024
Merged

Curried FSM StateFunction #11
cloudmark merged 1 commit intocloudmark:mainfrom
PetrosPapapa:feature/FSM-curried-state-function

Conversation

@PetrosPapapa
Copy link
Collaborator

This PR changes the type signature of state functions in FSMs to curry the state manager. This makes the syntax a bit cleaner, especially for multiple cases of events, and slighly closer to Akka (which doesn't need a state manager argument).

Made up example:

object Before {

    // Partial function gives the impression the state manager is also changing and something to match against!

    type StateFunction =
      PartialFunction[(FSM.Event[Any, Request], StateManager[IO, SomeState, Any, Request, Any]), IO[
        State[SomeState, Any, Request, Any]
      ]]

    def someStateFunction: StateFunction = {
      case (FSM.Event(Start, _), stateManager) =>
        stateManager.goto(Active)
      case (FSM.Event(Stop, _), stateManager) =>    // we have to repeat the stateManager in each case! 
        stateManager.stay()
    }
  }

  object After {
    type StateFunction = StateManager[IO, SomeState, Any, Request, Any] =>
      PartialFunction[FSM.Event[Any, Request], IO[
        State[SomeState, Any, Request, Any]
      ]]

    def someStateFunction: StateFunction = stateManager => {
        case FSM.Event(Start, _) =>
          stateManager.goto(Active)
        case FSM.Event(Stop, _) =>
          stateManager.stay()
      }
  }

@cloudmark cloudmark merged commit 33d39c2 into cloudmark:main Aug 23, 2024
PetrosPapapa referenced this pull request in PetrosPapapa/cats-actors Aug 23, 2024
cloudmark pushed a commit that referenced this pull request Aug 23, 2024
@PetrosPapapa PetrosPapapa deleted the feature/FSM-curried-state-function branch August 23, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants