Created
January 14, 2014 13:11
-
-
Save fwbrasil/8418072 to your computer and use it in GitHub Desktop.
Scala <=> Twitter
Future Bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.ExecutionContext | |
| import scala.concurrent.{ Future => ScalaFuture } | |
| import scala.concurrent.{ Promise => ScalaPromise } | |
| import scala.util.Failure | |
| import scala.util.Success | |
| import com.twitter.util.{ Future => TwitterFuture } | |
| import com.twitter.util.{ Promise => TwitterPromise } | |
| object FutureBridge { | |
| implicit def twitterToScala[T](future: TwitterFuture[T]): ScalaFuture[T] = { | |
| val promise = ScalaPromise[T]() | |
| future.onSuccess(promise.success) | |
| future.onFailure(promise.failure) | |
| promise.future | |
| } | |
| implicit def scalaToTwitter[T](future: ScalaFuture[T])(implicit ctx: ExecutionContext): TwitterFuture[T] = { | |
| val promise = TwitterPromise[T]() | |
| future.onComplete { | |
| case Success(result) => | |
| promise.setValue(result) | |
| case Failure(exception) => | |
| promise.setException(exception) | |
| } | |
| promise | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment