update timer for compliter

This commit is contained in:
Serhii 2023-03-21 22:11:22 +02:00
parent 24354b0864
commit a2af1e1ff7

View file

@ -124,6 +124,7 @@ class ElectrumClient {
await callWithTimeout(method: 'server.ping');
_setIsConnected(true);
} on RequestFailedTimeoutException catch (_) {
print('RequestFailedTimeoutException');
_setIsConnected(false);
}
}
@ -356,23 +357,27 @@ class ElectrumClient {
}
Future<dynamic> callWithTimeout(
{required String method,
List<Object> params = const [],
int timeout = 3000}) async {
{required String method, List<Object> params = const [], int timeout = 1000}) async {
try {
final completer = Completer<dynamic>();
_id += 1;
final id = _id;
_registryTask(id, completer);
socket!.write(jsonrpc(method: method, id: id, params: params));
Timer(Duration(milliseconds: timeout), () {
if (!completer.isCompleted) {
var counter = 0;
Timer.periodic(Duration(milliseconds: timeout), (timer) {
if (!completer.isCompleted && counter > 5) {
timer.cancel();
completer.completeError(RequestFailedTimeoutException(method, id));
} else if (completer.isCompleted) {
timer.cancel();
}
counter++;
});
return completer.future;
} catch(e) {
} catch (e) {
print(e.toString());
}
}