You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
823 B

  1. package nova.monadic_sfx.actors
  2. import akka.actor.typed._
  3. import akka.actor.typed.scaladsl._
  4. object Counter {
  5. sealed trait Command
  6. case object Increment extends Command
  7. final case class GetValue(replyTo: ActorRef[Value]) extends Command
  8. final case class Value(n: Int)
  9. def apply(): Behavior[Command] = {
  10. Behaviors.setup(context => new Counter(context))
  11. }
  12. }
  13. class Counter(context: ActorContext[Counter.Command])
  14. extends AbstractBehavior[Counter.Command](context) {
  15. import Counter._
  16. private var n = 0
  17. override def onMessage(msg: Command): Behavior[Counter.Command] = {
  18. msg match {
  19. case Increment =>
  20. n += 1
  21. context.log.debug("Incremented counter to [{}]", n)
  22. this
  23. case GetValue(replyTo) =>
  24. replyTo ! Value(n)
  25. Behaviors.stopped
  26. }
  27. }
  28. }