Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add section on using Scala 3 Union types to eliminate msg adapters
- Use InteractionPatternsSpec as an example for applying Scala 3's Union types to symplify actor code - Remove response message wrapper & adapter - Use the union of the actor's public protocol (the `Translate` message (the only member of the `Command` ADT) and the possible responses from the backend (messages `JobStarted`, `JobProgress`, and `JobCompleted`): `private type CommandAndResponse = Command | Backend.Response` - Instead of utilising the message adaptor `ActorRef` in the `replyTo` field of the message sent to the backend, `context.self` is used instead - The internal (extended) `Behavior[CommandAndResponse]` is narrowed to `Behavior[Command]` at creation time The diffs between the original and the new version: ```bash 136d139 < private final case class WrappedBackendResponse(response: Backend.Response) extends Command 137a141,142 > private type CommandAndResponse = Command | Backend.Response > 139,141c144 < Behaviors.setup[Command] { context => < val backendResponseMapper: ActorRef[Backend.Response] = < context.messageAdapter(rsp => WrappedBackendResponse(rsp)) --- > Behaviors.setup[CommandAndResponse] { context => 143,144c146,147 < def active(inProgress: Map[Int, ActorRef[URI]], count: Int): Behavior[Command] = { < Behaviors.receiveMessage[Command] { --- > def active(inProgress: Map[Int, ActorRef[URI]], count: Int): Behavior[CommandAndResponse] = { > Behaviors.receiveMessage[CommandAndResponse] { 147c150 < backend ! Backend.StartTranslationJob(taskId, site, backendResponseMapper) --- > backend ! Backend.StartTranslationJob(taskId, site, context.self) 150,162c153,162 < case wrapped: WrappedBackendResponse => < wrapped.response match { < case Backend.JobStarted(taskId) => < context.log.info("Started {}", taskId) < Behaviors.same < case Backend.JobProgress(taskId, progress) => < context.log.info2("Progress {}: {}", taskId, progress) < Behaviors.same < case Backend.JobCompleted(taskId, result) => < context.log.info2("Completed {}: {}", taskId, result) < inProgress(taskId) ! result < active(inProgress - taskId, count) < } --- > case Backend.JobStarted(taskId) => > context.log.info("Started {}", taskId) > Behaviors.same > case Backend.JobProgress(taskId, progress) => > context.log.info2("Progress {}: {}", taskId, progress) > Behaviors.same > case Backend.JobCompleted(taskId, result) => > context.log.info2("Completed {}: {}", taskId, result) > inProgress(taskId) ! result > active(inProgress - taskId, count) 167c167 < } --- > }.narrow ```
- Loading branch information