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

Issue/8 fsm actor context #9

Closed Mitrug closed 3 months ago

Mitrug commented 3 months ago

Summary

This PR adds the MinimalActorContext to the FSM StateManager. Allows the creation of child actors and send messages to self in an FSM.

Changes:

  1. Exposed the MinimalActorContext in the FSM StateManager.
  2. Removed the ActorSystem from the MinimalActorContext as we want to restrict access to the scheduler in the FSMBuilder. Access to the Actor Scheduler would allow someone to transition states in the scheduleOnce functions.
  3. Switched the FaultHandler to use the ActorContext instead of the Minimal one since it requires access to the ActorSystem. Switched the Request and Response types to be wildcards since the FaultHandler should never actually make use of them. Before they were allowed to be [... , Nothing, Any] due to the MinimalActorContext allowing co/contra variance, however, the ActorContext only allows for invariance.

Usage of the FSM ActorContext:

FSM[IO, FsmParentState, Int, FsmRequest, Any]
  .when(FsmIdle) { 
    case (Event(FsmRun, _), sM) =>
      for {
        fsmChildActor <- sM.minimalContext.actorOf(FsmChild())
        result <- fsmChildActor ? FsmChildEcho
        state <- sM.goto(FsmRunning).replying(result)
      } yield state
  }
  .when(FsmRunning) {
    case (Event(FsmRun, _), sM) =>
      (sM.minimalContext.self ! FsmStop) *> sM.stay()
    case (Event(FsmStop, _), sM) =>
      stopped.complete(true) *> sM.stay()
  }
  .withConfig(FSMConfig.withConsoleInformation)
  .startWith(startWith, 0)
  .initialize