private def eventSourcedBehavior()

in pekko-sample-persistence-dc-scala/src/main/scala/sample/persistence/res/movielist/MovieWatchList.scala [32:52]


  private def eventSourcedBehavior(
      replicaId: ReplicaId, persistenceId: PersistenceId): EventSourcedBehavior[Command, ORSet.DeltaOp, ORSet[String]] =
    EventSourcedBehavior[Command, ORSet.DeltaOp, ORSet[String]](
      persistenceId,
      ORSet.empty(replicaId),
      (state, cmd) => commandHandler(state, cmd),
      (state, event) => state.applyOperation(event))

  private def commandHandler(state: ORSet[String], cmd: Command): Effect[ORSet.DeltaOp, ORSet[String]] = {
    // operations on an ORSet don't but instead create events describing the change that
    // are then persisted
    cmd match {
      case AddMovie(movieId) =>
        Effect.persist(state.add(movieId))
      case RemoveMovie(movieId) =>
        Effect.persist(state.remove(movieId))
      case GetMovieList(replyTo) =>
        replyTo ! MovieList(state.elements)
        Effect.none
    }
  }