diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-02-08 22:59:40 +0000 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-02-08 22:59:40 +0000 |
commit | 76e8efe36eede6db4c79586919c479a62fdb52dc (patch) | |
tree | 3027401f510788e65ff8d9cc8718e99a003b1577 /lib | |
parent | eedf51e9d11e362fc14bc8b33aca58775420576e (diff) | |
download | Nim-76e8efe36eede6db4c79586919c479a62fdb52dc.tar.gz |
Added a generic-lacking version of PFuture.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/asyncio2.nim | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/pure/asyncio2.nim b/lib/pure/asyncio2.nim index 38fa93452..cdb4a6f49 100644 --- a/lib/pure/asyncio2.nim +++ b/lib/pure/asyncio2.nim @@ -23,9 +23,12 @@ import sockets2, net # -- Futures type - PFuture*[T] = ref object - value: T + PFutureVoid* = ref object of PObject + cbVoid: proc () {.closure.} finished: bool + + PFuture*[T] = ref object of PFutureVoid + value: T error: ref EBase cb: proc (future: PFuture[T]) {.closure.} @@ -42,6 +45,8 @@ proc complete*[T](future: PFuture[T], val: T) = future.finished = true if future.cb != nil: future.cb(future) + if future.cbVoid != nil: + future.cbVoid() proc fail*[T](future: PFuture[T], error: ref EBase) = ## Completes ``future`` with ``error``. @@ -60,6 +65,17 @@ proc `callback=`*[T](future: PFuture[T], if future.finished: future.cb(future) +proc `callbackVoid=`*(future: PFutureVoid, cb: proc () {.closure.}) = + ## Sets the **void** callback proc to be called when the future completes. + ## + ## If future has already completed then ``cb`` will be called immediately. + ## + ## **Note**: This is used for the ``await`` functionality, you most likely + ## want to use ``callback``. + future.cbVoid = cb + if future.finished: + future.cbVoid() + proc read*[T](future: PFuture[T]): T = ## Retrieves the value of ``future``. Future must be finished otherwise ## this function will fail with a ``EInvalidValue`` exception. |