akkaのソースコード自体は、何をすべきかのヒントです。 akka.pattern.PipeToSupport
を見て:
def pipeTo(recipient: ActorRef)(implicit sender: ActorRef = Actor.noSender): Future[T] = {
future andThen {
case Success(r) ⇒ recipient ! r
case Failure(f) ⇒ recipient ! Status.Failure(f)
}
}
だから私たちは基本的にEither
の派遣と我々の場合のために、このアプローチを再利用することができます
val result: Future[Either[Int, Throwable]] = Future.successful(Left(5))
result andThen {
case Success(Left(value)) => leftActor ! value
case Success(Right(exception)) => rightActor ! exception
case Failure(exception) => println("Failure")
}
希望DSL達成:
を我々が達成しようとすることができますあなたのDSL(Pipe()とto(...))のように:
あなただけで
MyEitherPipeSupport
を混ぜて、あなたはこのように書くことができ、あなたの俳優で今
trait MyEitherPipeSupport extends PipeToSupport {
final class PipeableEitherFuture[L, R](val future: Future[Either[L, R]])(implicit executionContext: ExecutionContext) {
def to(leftRef: ActorRef, rightRef: ActorRef, exceptionRef: ActorRef) = future andThen {
case Success(Left(value)) ⇒ leftRef ! value
case Success(Right(exception)) ⇒ rightRef ! exception
case Failure(exception) ⇒ exceptionRef ! Status.Failure(exception)
}
}
implicit def eitherPipe[L, R](future: Future[Either[L, R]])(implicit executionContext: ExecutionContext): PipeableEitherFuture[L, R] = new PipeableEitherFuture(future)
}
:
val result: Future[Either[Int, Throwable]] = Future.successful(Left(5))
eitherPipe(result).to(left, right, anotherOne)
あなたはこの質問への答えで見たいのですが何か他のものがありますか? – tkachuko