From 50cfd9d9dd82cb8dda954a06235d199fd20cf7be Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 27 Jun 2023 14:44:03 +0200 Subject: [PATCH] `untilCancelled` template --- ethers/providers/jsonrpc/looping.nim | 6 ++++++ ethers/providers/jsonrpc/subscriptions.nim | 12 +++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 ethers/providers/jsonrpc/looping.nim diff --git a/ethers/providers/jsonrpc/looping.nim b/ethers/providers/jsonrpc/looping.nim new file mode 100644 index 0000000..f0bb7a8 --- /dev/null +++ b/ethers/providers/jsonrpc/looping.nim @@ -0,0 +1,6 @@ +template untilCancelled*(body) = + try: + while true: + body + except CancelledError: + raise diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index e24ead5..c0e92c2 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -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()