Skip to content

Instantly share code, notes, and snippets.

@soaxelbrooke
Last active July 30, 2016 06:35
Show Gist options
  • Save soaxelbrooke/042743148c9ec4521880f5f5328f8f49 to your computer and use it in GitHub Desktop.
Save soaxelbrooke/042743148c9ec4521880f5f5328f8f49 to your computer and use it in GitHub Desktop.
object implicits {
implicit class ESFuture[Response <: ActionResponse](future: ListenableActionFuture[Response])
extends Future[Response] {
override def onComplete[U](f: (Try[Response]) => U)(implicit executor: ExecutionContext): Unit = {
future.addListener(new ActionListener[Response] {
override def onFailure(e: Throwable): Unit = throw e
override def onResponse(response: Response): Unit = f(Try(response))
})
}
override def isCompleted: Boolean = future.isDone
override def value: Option[Try[Response]] =
future.isDone match {
case true => Some(Try(future.get()))
case false => None
}
@scala.throws[InterruptedException](classOf[InterruptedException])
@scala.throws[TimeoutException](classOf[TimeoutException])
override def ready(atMost: Duration)(implicit permit: CanAwait): ESFuture.this.type = {
future.wait(atMost.toMillis)
this
}
@scala.throws[Exception](classOf[Exception])
override def result(atMost: Duration)(implicit permit: CanAwait): Response =
future.get(atMost.toMillis, TimeUnit.MILLISECONDS)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment