Add more FutureBase helpers. (#304)

This commit is contained in:
Eugene Kabanov 2022-09-12 15:11:13 +03:00 committed by GitHub
parent 396ed554f6
commit 8e8263370b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 2 deletions

View File

@ -295,6 +295,10 @@ proc cancel(future: FutureBase, loc: ptr SrcLoc): bool =
future.mustCancel = true
return true
template cancel*(future: FutureBase) =
## Cancel ``future``.
discard cancel(future, getSrcLocation())
template cancel*[T](future: Future[T]) =
## Cancel ``future``.
discard cancel(FutureBase(future), getSrcLocation())
@ -823,7 +827,7 @@ proc oneValue*[T](futs: varargs[Future[T]]): Future[T] {.
return retFuture
proc cancelAndWait*[T](fut: Future[T]): Future[void] =
proc cancelAndWait*(fut: FutureBase): Future[void] =
## Initiate cancellation process for Future ``fut`` and wait until ``fut`` is
## done e.g. changes its state (become completed, failed or cancelled).
##
@ -845,7 +849,10 @@ proc cancelAndWait*[T](fut: Future[T]): Future[void] =
fut.cancel()
return retFuture
proc allFutures*[T](futs: varargs[Future[T]]): Future[void] =
proc cancelAndWait*[T](fut: Future[T]): Future[void] =
cancelAndWait(FutureBase(fut))
proc allFutures*(futs: varargs[FutureBase]): Future[void] =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.
##
@ -883,6 +890,19 @@ proc allFutures*[T](futs: varargs[Future[T]]): Future[void] =
return retFuture
proc allFutures*[T](futs: varargs[Future[T]]): Future[void] =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.
##
## If the argument is empty, the returned future COMPLETES immediately.
##
## On cancel all the awaited futures ``futs`` WILL NOT BE cancelled.
# Because we can't capture varargs[T] in closures we need to create copy.
var nfuts: seq[FutureBase]
for future in futs:
nfuts.add(FutureBase(future))
allFutures(nfuts)
proc allFinished*[T](futs: varargs[Future[T]]): Future[seq[Future[T]]] =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.