From 281a04654ceb440b46c0d0f29222da214c2a52c2 Mon Sep 17 00:00:00 2001 From: Josh Babb Date: Wed, 24 May 2023 13:14:56 -0500 Subject: [PATCH] reuse socket var where possible --- lib/electrumx_rpc/rpc.dart | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/electrumx_rpc/rpc.dart b/lib/electrumx_rpc/rpc.dart index 1bcf2bd84..b20f2f802 100644 --- a/lib/electrumx_rpc/rpc.dart +++ b/lib/electrumx_rpc/rpc.dart @@ -53,20 +53,15 @@ class JsonRPC { } if (useSSL) { - await SecureSocket.connect(host, port, + socket ??= await SecureSocket.connect(host, port, timeout: connectionTimeout, - onBadCertificate: (_) => true).then((Socket sock) { - socket = sock; - socket?.listen(dataHandler, - onError: errorHandler, onDone: doneHandler, cancelOnError: true); - }); + onBadCertificate: (_) => true); + socket!.listen(dataHandler, + onError: errorHandler, onDone: doneHandler, cancelOnError: true); } else { - await Socket.connect(host, port, timeout: connectionTimeout) - .then((Socket sock) { - socket = sock; - socket?.listen(dataHandler, - onError: errorHandler, onDone: doneHandler, cancelOnError: true); - }); + socket ??= await Socket.connect(host, port, timeout: connectionTimeout); + socket!.listen(dataHandler, + onError: errorHandler, onDone: doneHandler, cancelOnError: true); } socket?.write('$jsonRpcRequest\r\n');