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
object FutureOps extends FutureOps | |
trait FutureOps { | |
def takeFirstMatch[A](futures: Traversable[Future[A]], predicate: A => Boolean)(implicit ec:ExecutionContext): Future[Option[A]] = { | |
if(futures.nonEmpty) { | |
val promise = Promise[Option[A]]() | |
val completedCount = new java.util.concurrent.atomic.AtomicInteger(0) | |
val allDoneLatch = Promise[Unit] | |
def maybeAllDone() { | |
// Note: check and execute normally creates a race but only one of the futures can cause the final count to be reached | |
if(completedCount.incrementAndGet() == futures.size) { |
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
object FutureOps extends FutureOps | |
trait FutureOps { | |
def toTry[A](self: Future[A])(implicit ec: ExecutionContext): Future[Try[A]] = { | |
val p = Promise[Try[A]]() | |
self onComplete { case result => p.success(result) } | |
p.future | |
} | |
def takeFirstMatch[A](futures: Traversable[Future[A]], predicate: A => Boolean)(implicit ec:ExecutionContext): Future[Option[A]] = { | |
if(futures.nonEmpty) { | |
val promise = Promise[Option[A]]() |