sttdlib: asyncdispatch: all: Add Future[void] support.

This commit is contained in:
Konstantin Molchanov 2016-05-31 22:33:51 +04:00
commit f44e065356

View file

@ -9,7 +9,7 @@
include "system/inclrtl" include "system/inclrtl"
import os, oids, tables, strutils, macros, times, heapqueue import os, oids, tables, strutils, macros, times, heapqueue, sequtils
import nativesockets, net, queues import nativesockets, net, queues
@ -355,30 +355,34 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
fut2.callback = cb fut2.callback = cb
return retFuture return retFuture
proc all*[T](futs: varargs[Future[T]]): Future[seq[T]] = proc all*[T](futs: varargs[Future[T]]): auto =
## Returns a future which will complete once all futures in ``futs`` ## Returns a future which will complete once all futures in ``futs``
## complete. ## complete.
## ##
## The resulting future will hold the values of all awaited futures, ## The resulting future will hold the values of all awaited futures,
## in the order they are passed. ## in the order they are passed.
var when T is void:
retFuture = newFuture[seq[T]]("asyncdispatch.all") return foldl(futs, a and b)
retValues = newSeq[T](len(futs))
completedFutures = 0
for i, fut in futs: else:
proc setCallback(i: int) = var
fut.callback = proc(f: Future[T]) = retFuture = newFuture[seq[T]]("asyncdispatch.all")
retValues[i] = f.read() retValues = newSeq[T](len(futs))
inc(completedFutures) completedFutures = 0
if completedFutures == len(retValues): for i, fut in futs:
retFuture.complete(retValues) proc setCallback(i: int) =
fut.callback = proc(f: Future[T]) =
retValues[i] = f.read()
inc(completedFutures)
setCallback(i) if completedFutures == len(retValues):
retFuture.complete(retValues)
return retFuture setCallback(i)
return retFuture
type type
PDispatcherBase = ref object of RootRef PDispatcherBase = ref object of RootRef