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, );