From 73ecbf15d5659ff5a3e8863140de5468766f5410 Mon Sep 17 00:00:00 2001 From: Josh Babb Date: Wed, 24 May 2023 13:39:21 -0500 Subject: [PATCH] move dataHandler and errorHandler up in scope --- lib/electrumx_rpc/rpc.dart | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/electrumx_rpc/rpc.dart b/lib/electrumx_rpc/rpc.dart index 34a354c95..7b589e875 100644 --- a/lib/electrumx_rpc/rpc.dart +++ b/lib/electrumx_rpc/rpc.dart @@ -17,10 +17,13 @@ class JsonRPC { final String host; final int port; final Duration connectionTimeout; - Socket? socket; + Socket? socket; StreamSubscription? _subscription; + void Function(List)? _onData; + void Function(Object, StackTrace)? _onError; + Future request(String jsonRpcRequest) async { final completer = Completer(); final List responseData = []; @@ -49,6 +52,8 @@ class JsonRPC { } } + _onData = dataHandler; + void errorHandler(Object error, StackTrace trace) { Logging.instance .log("JsonRPC errorHandler: $error\n$trace", level: LogLevel.Error); @@ -61,6 +66,8 @@ class JsonRPC { // TODO do we need to recreate the socket? } + _onError = errorHandler; + void doneHandler() { Logging.instance.log( "JsonRPC doneHandler: not destroying socket ${socket?.address}:${socket?.port}", @@ -78,8 +85,8 @@ class JsonRPC { socket ??= await SecureSocket.connect(host, port, timeout: connectionTimeout, onBadCertificate: (_) => true); _subscription ??= socket!.listen( - dataHandler, - onError: errorHandler, + _onData, + onError: _onError, onDone: doneHandler, cancelOnError: true, ); @@ -90,8 +97,8 @@ class JsonRPC { timeout: connectionTimeout, ); _subscription ??= socket!.listen( - dataHandler, - onError: errorHandler, + _onData, + onError: _onError, onDone: doneHandler, cancelOnError: true, );