suprnation / cats-actors

Cats Actors framework for building apps which are reactive. Cats actors uses a conceptual actor model as a higher level abstraction for concurrency.
Apache License 2.0
104 stars 9 forks source link

Curried FSM StateFunction #11

Closed PetrosPapapa closed 3 months ago

PetrosPapapa commented 3 months ago

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()
      }
  }