2024-02-02 08:56:41 +00:00
|
|
|
import chronos
|
2023-10-12 18:59:21 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
import ../../../waku/[waku_core/message, waku_store, waku_store_legacy]
|
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
|
2024-03-14 16:48:09 +00:00
|
|
|
FUTURE_TIMEOUT_SHORT* = 100.milliseconds
|
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
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
proc newHistoryFuture*(): Future[StoreQueryRequest] =
|
|
|
|
newFuture[StoreQueryRequest]()
|
|
|
|
|
|
|
|
proc newLegacyHistoryFuture*(): Future[waku_store_legacy.HistoryQuery] =
|
|
|
|
newFuture[waku_store_legacy.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.")
|
|
|
|
|
2024-03-14 16:48:09 +00:00
|
|
|
proc waitForResult*[T](
|
|
|
|
future: Future[T], timeout = FUTURE_TIMEOUT
|
|
|
|
): Future[Result[T, string]] {.async.} =
|
2024-02-06 16:37:42 +00:00
|
|
|
discard await future.withTimeout(timeout)
|
|
|
|
return future.toResult()
|