From 416d85dbf5b8f8345b55282ea83e4530ef84683c Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 25 Jul 2018 19:36:16 +0300 Subject: [PATCH] More safe version of sleepAsync(). --- asyncdispatch2/asyncloop.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/asyncdispatch2/asyncloop.nim b/asyncdispatch2/asyncloop.nim index 30857e9..5e79ed2 100644 --- a/asyncdispatch2/asyncloop.nim +++ b/asyncdispatch2/asyncloop.nim @@ -612,16 +612,19 @@ proc removeTimer*(at: uint64, cb: CallbackFunc, udata: pointer = nil) = if index != -1: loop.timers.del(index) -proc completeProxy*[T](data: pointer) = - var future = cast[Future[T]](data) - future.complete() +# proc completeProxy*[T](data: pointer) = +# var future = cast[Future[T]](data) +# future.complete() proc sleepAsync*(ms: int): Future[void] = ## Suspends the execution of the current async procedure for the next ## ``ms`` milliseconds. var retFuture = newFuture[void]("sleepAsync") + proc completion(data: pointer) = + if not retFuture.finished: + retFuture.complete() addTimer(fastEpochTime() + uint64(ms), - completeProxy[void], cast[pointer](retFuture)) + completion, cast[pointer](retFuture)) return retFuture proc withTimeout*[T](fut: Future[T], timeout: int): Future[bool] =