2024-02-02 08:56:41 +00:00
|
|
|
import chronos
|
2023-10-12 18:59:21 +00:00
|
|
|
|
2024-02-02 08:56:41 +00:00
|
|
|
import ../../../waku/[waku_core/message, waku_store]
|
2023-10-12 18:59:21 +00:00
|
|
|
|
2024-02-02 08:56:41 +00:00
|
|
|
const
|
|
|
|
FUTURE_TIMEOUT* = 1.seconds
|
|
|
|
FUTURE_TIMEOUT_LONG* = 10.seconds
|
2023-11-15 15:15:38 +00:00
|
|
|
|
2023-10-12 18:59:21 +00:00
|
|
|
proc newPushHandlerFuture*(): Future[(string, WakuMessage)] =
|
2024-02-02 08:56:41 +00:00
|
|
|
newFuture[(string, WakuMessage)]()
|
2023-11-15 15:11:36 +00:00
|
|
|
|
|
|
|
proc newBoolFuture*(): Future[bool] =
|
2024-02-02 08:56:41 +00:00
|
|
|
newFuture[bool]()
|
2023-12-19 14:38:43 +00:00
|
|
|
|
|
|
|
proc newHistoryFuture*(): Future[HistoryQuery] =
|
2024-02-02 08:56:41 +00:00
|
|
|
newFuture[HistoryQuery]()
|
2024-02-06 16:37:42 +00:00
|
|
|
|
|
|
|
proc toResult*[T](future: Future[T]): Result[T, string] =
|
|
|
|
if future.cancelled():
|
|
|
|
return chronos.err("Future timeouted before completing.")
|
|
|
|
elif future.finished() and not future.failed():
|
|
|
|
return chronos.ok(future.read())
|
|
|
|
else:
|
|
|
|
return chronos.err("Future finished but failed.")
|
|
|
|
|
|
|
|
proc toResult*(future: Future[void]): Result[void, string] =
|
|
|
|
if future.cancelled():
|
|
|
|
return chronos.err("Future timeouted before completing.")
|
|
|
|
elif future.finished() and not future.failed():
|
|
|
|
return chronos.ok()
|
|
|
|
else:
|
|
|
|
return chronos.err("Future finished but failed.")
|
|
|
|
|
|
|
|
proc waitForResult*[T](future: Future[T], timeout = FUTURE_TIMEOUT): Future[Result[T, string]] {.async.} =
|
|
|
|
discard await future.withTimeout(timeout)
|
|
|
|
return future.toResult()
|