remove unneeded mutex and clean up unneeded async

This commit is contained in:
julian 2023-05-25 15:40:30 -06:00
parent e72e59a50a
commit e9309bc281

View file

@ -3,7 +3,6 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:mutex/mutex.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
// hacky fix to receive large jsonrpc responses // hacky fix to receive large jsonrpc responses
@ -61,17 +60,23 @@ class JsonRPC {
"completing pending requests with errors", "completing pending requests with errors",
level: LogLevel.Error, level: LogLevel.Error,
); );
_requestQueue.clear(
errorMessage: "JsonRPC doneHandler: socket closed " for (final req in _requestQueue.queue) {
if (!req.isComplete) {
req.completer.completeError(
"JsonRPC doneHandler: socket closed "
"before request could complete", "before request could complete",
); );
} }
}
_requestQueue.clear();
}
disconnect(); disconnect();
} }
Future<void> _onReqCompleted(_JsonRPCRequest req) async { void _onReqCompleted(_JsonRPCRequest req) {
await _requestQueue.remove(req); _requestQueue.remove(req);
if (_requestQueue.isNotEmpty) { if (_requestQueue.isNotEmpty) {
_sendNextAvailableRequest(); _sendNextAvailableRequest();
} }
@ -109,7 +114,7 @@ class JsonRPC {
completer: Completer<dynamic>(), completer: Completer<dynamic>(),
); );
await _requestQueue.add(req); _requestQueue.add(req);
// if this is the only/first request then send it right away // if this is the only/first request then send it right away
if (_requestQueue.length == 1) { if (_requestQueue.length == 1) {
@ -156,35 +161,20 @@ class JsonRPC {
} }
} }
// mutex *may* not be needed as the protected functions are not async
class _JsonRPCRequestQueue { class _JsonRPCRequestQueue {
final _m = Mutex();
final List<_JsonRPCRequest> _rq = []; final List<_JsonRPCRequest> _rq = [];
Future<void> add(_JsonRPCRequest req) async { void add(_JsonRPCRequest req) => _rq.add(req);
await _m.protect(() async => _rq.add(req));
}
Future<void> remove(_JsonRPCRequest req) async { bool remove(_JsonRPCRequest req) => _rq.remove(req);
await _m.protect(() async => _rq.remove(req));
}
Future<void> clear({required String errorMessage}) async { void clear() => _rq.clear();
await _m.protect(() async {
for (final req in _rq) {
if (!req.isComplete) {
req.completer.completeError(errorMessage);
}
}
_rq.clear();
});
}
bool get isEmpty => _rq.isEmpty; bool get isEmpty => _rq.isEmpty;
bool get isNotEmpty => _rq.isNotEmpty; bool get isNotEmpty => _rq.isNotEmpty;
int get length => _rq.length; int get length => _rq.length;
_JsonRPCRequest get next => _rq.first; _JsonRPCRequest get next => _rq.first;
List<_JsonRPCRequest> get queue => _rq.toList(growable: false);
} }
class _JsonRPCRequest { class _JsonRPCRequest {