fix(trackedfutures): removes usage of `then` from tracked futures (#1032)

- removes usage of `then`, simplifying the logic, and allowing `then` to be removed completely
- updates annotations to reflect that all procs (sync and async) raise no exceptions
This commit is contained in:
Eric 2024-12-12 19:31:51 +07:00 committed by GitHub
parent 855b973811
commit da234d503b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 9 deletions

View File

@ -1,9 +1,9 @@
import std/sugar
import std/tables
import pkg/chronos
import ../logutils
import ../utils/then
{.push raises: [].}
type
TrackedFutures* = ref object
@ -25,10 +25,10 @@ proc track*[T](self: TrackedFutures, fut: Future[T]): Future[T] =
self.futures[fut.id] = FutureBase(fut)
fut
.then((val: T) => self.removeFuture(fut))
.cancelled(() => self.removeFuture(fut))
.catch((e: ref CatchableError) => self.removeFuture(fut))
proc cb(udata: pointer) =
self.removeFuture(fut)
fut.addCallback(cb)
return fut
@ -38,15 +38,17 @@ proc track*[T, U](future: Future[T], self: U): Future[T] =
## `trackedFutures` property.
self.trackedFutures.track(future)
proc cancelTracked*(self: TrackedFutures) {.async.} =
proc cancelTracked*(self: TrackedFutures) {.async: (raises: []).} =
self.cancelling = true
trace "cancelling tracked futures"
var cancellations: seq[FutureBase]
for future in self.futures.values:
if not future.isNil and not future.finished:
trace "cancelling tracked future", id = future.id
await future.cancelAndWait()
cancellations.add future.cancelAndWait()
await noCancel allFutures cancellations
self.futures.clear()
self.cancelling = false