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