2024-02-02 08:56:41 +00:00
|
|
|
import chronos
|
2023-10-12 18:59:21 +00:00
|
|
|
|
2024-07-05 22:03:38 +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
|
2024-05-13 15:25:44 +00:00
|
|
|
FUTURE_TIMEOUT_MEDIUM* = 5.seconds
|
2024-02-02 08:56:41 +00:00
|
|
|
FUTURE_TIMEOUT_LONG* = 10.seconds
|
2024-03-14 16:48:09 +00:00
|
|
|
FUTURE_TIMEOUT_SHORT* = 100.milliseconds
|
2024-08-02 14:43:22 +00:00
|
|
|
FUTURE_TIMEOUT_SCORING* = 13.seconds # Scoring is 12s, so we need to wait more
|
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()
|
2024-05-13 15:43:14 +00:00
|
|
|
|
|
|
|
proc reset*[T](future: Future[T]): void =
|
|
|
|
# Likely an incomplete reset, but good enough for testing purposes (for now)
|
|
|
|
future.internalError = nil
|
|
|
|
future.internalState = FutureState.Pending
|