2012-04-25 18:34:16 +02:00
|
|
|
package akka.pattern
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2012-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.Duration
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.ExecutionContext
|
2012-04-25 18:34:16 +02:00
|
|
|
import akka.actor._
|
2012-06-29 16:06:26 +02:00
|
|
|
import akka.dispatch.{ Promise, Future }
|
2012-04-25 18:34:16 +02:00
|
|
|
|
|
|
|
|
trait FutureTimeoutSupport {
|
|
|
|
|
/**
|
|
|
|
|
* Returns a [[akka.dispatch.Future]] that will be completed with the success or failure of the provided value
|
|
|
|
|
* after the specified duration.
|
|
|
|
|
*/
|
|
|
|
|
def after[T](duration: Duration, using: Scheduler)(value: ⇒ Future[T])(implicit ec: ExecutionContext): Future[T] =
|
|
|
|
|
if (duration.isFinite() && duration.length < 1) value else {
|
|
|
|
|
val p = Promise[T]()
|
|
|
|
|
val c = using.scheduleOnce(duration) { p completeWith value }
|
|
|
|
|
p onComplete { _ ⇒ c.cancel() }
|
|
|
|
|
p
|
|
|
|
|
}
|
|
|
|
|
}
|