`untilCancelled` template

This commit is contained in:
Mark Spanbroek 2023-06-27 14:44:03 +02:00 committed by markspanbroek
parent 3a76fa74f1
commit 50cfd9d9dd
2 changed files with 11 additions and 7 deletions

View File

@ -0,0 +1,6 @@
template untilCancelled*(body) =
try:
while true:
body
except CancelledError:
raise

View File

@ -6,6 +6,7 @@ import ../../basics
import ../../provider
import ./rpccalls
import ./conversions
import ./looping
type
JsonRpcSubscriptions* = ref object of RootObj
@ -110,13 +111,10 @@ proc new*(_: type JsonRpcSubscriptions,
callback(id, change)
proc poll {.async.} =
try:
while true:
for id in toSeq subscriptions.callbacks.keys:
await poll(id)
await sleepAsync(pollingInterval)
except CancelledError:
raise
untilCancelled:
for id in toSeq subscriptions.callbacks.keys:
await poll(id)
await sleepAsync(pollingInterval)
asyncSpawn poll()