From 3b0f94f2b52396161ddbadd47e6e3def94ee5b28 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 3 Sep 2025 16:12:27 +0200 Subject: [PATCH] refactor(subscriptions): remove unnecessary json-rpc method table --- ethers/providers/jsonrpc/subscriptions.nim | 31 ++++++---------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index 95c094e..096fcb9 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -18,7 +18,7 @@ type JsonRpcSubscriptions* = ref object of RootObj client: RpcClient callbacks: Table[JsonNode, SubscriptionCallback] - methodHandlers: Table[string, MethodHandler] + subscriptionHandler: MethodHandler # Used by both PollingSubscriptions and WebsocketSubscriptions to store # subscription filters so the subscriptions can be recreated. With # PollingSubscriptions, the RPC node might prune/forget about them, and with @@ -43,28 +43,13 @@ template `or`(a: JsonNode, b: typed): JsonNode = func start*(subscriptions: JsonRpcSubscriptions) = subscriptions.client.onProcessMessage = - proc(client: RpcClient, - line: string): Result[bool, string] {.gcsafe, raises: [].} = + proc(client: RpcClient, line: string): Result[bool, string] = if json =? JsonNode.fromJson(line): - if "method" in json: - let methodName = json{"method"}.getStr() - if methodName in subscriptions.methodHandlers: - let handler = subscriptions.methodHandlers.getOrDefault(methodName) - if not handler.isNil: - handler(json{"params"} or newJArray()) - # false = do not continue processing message using json_rpc's - # default processing handler - return ok false - - # true = continue processing message using json_rpc's default message handler - return ok true - -proc setMethodHandler( - subscriptions: JsonRpcSubscriptions, - `method`: string, - handler: MethodHandler -) = - subscriptions.methodHandlers[`method`] = handler + if "method" in json and json{"method"}.getStr() == "eth_subscription": + if handler =? subscriptions.subscriptionHandler: + handler(json{"params"} or newJArray()) + return ok false # do not process using json-rpc default handler + return ok true # continue processing using json-rpc default handler method subscribeBlocks*(subscriptions: JsonRpcSubscriptions, onBlock: BlockHandler): @@ -155,7 +140,7 @@ proc new*(_: type JsonRpcSubscriptions, let id = arguments{"subscription"} or newJString("") if callback =? subscriptions.getCallback(id): callback(id, success(arguments)) - subscriptions.setMethodHandler("eth_subscription", subscriptionHandler) + subscriptions.subscriptionHandler = subscriptionHandler if resubscribeInterval > 0: if resubscribeInterval >= 300: