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