feat: Lint and format

This commit is contained in:
2023-06-17 21:14:17 +02:00
parent 7e55015948
commit 3d3b047097
7 changed files with 100 additions and 108 deletions

View File

@@ -13,7 +13,7 @@ class JsonImplementation {
factory JsonImplementation.fromJson(Map<String, dynamic> json) {
return JsonImplementation();
}
Map<String, dynamic> toJson() => {};
}
@@ -29,17 +29,17 @@ class DataWrapper<T extends JsonImplementation> {
/// The actual data.
final T data;
Map<String, dynamic> toJson() => {
'id': id,
'data': data.toJson()
};
static DataWrapper fromJson<T extends JsonImplementation>(Map<String, dynamic> json) => DataWrapper<T>(
json['id']! as String,
json['data']! as T,
);
Map<String, dynamic> toJson() => {'id': id, 'data': data.toJson()};
static DataWrapper fromJson<T extends JsonImplementation>(
Map<String, dynamic> json,
) =>
DataWrapper<T>(
json['id']! as String,
json['data']! as T,
);
DataWrapper reply(T newData) => DataWrapper(id, newData);
}
@@ -49,16 +49,14 @@ class DataWrapper<T extends JsonImplementation> {
///
/// awaiting [sendData] will return a [Future] that will resolve to the reresponse when
/// received via [onData].
abstract class AwaitableDataSender<
S extends JsonImplementation,
R extends JsonImplementation
> {
abstract class AwaitableDataSender<S extends JsonImplementation,
R extends JsonImplementation> {
@mustCallSuper
AwaitableDataSender();
/// A mapping of ID to completer for pending requests.
final Map<String, Completer<R>> _awaitables = {};
/// Critical section for accessing [AwaitableDataSender._awaitables].
final Lock _lock = Lock();
@@ -67,7 +65,7 @@ abstract class AwaitableDataSender<
/// A logger.
final Logger _log = Logger('AwaitableDataSender');
@visibleForTesting
Map<String, Completer<R>> getAwaitables() => _awaitables;
@@ -78,34 +76,38 @@ abstract class AwaitableDataSender<
/// NOTE: Must be overwritten by the actual implementation
@visibleForOverriding
Future<void> sendDataImpl(DataWrapper data);
/// Sends [data] using [sendDataImpl]. If [awaitable] is true, then a
/// Future will be returned that can be used to await a response. If it
/// is false, then null will be imediately resolved.
Future<R?> sendData(S data, { bool awaitable = true, @visibleForTesting String? id }) async {
Future<R?> sendData(
S data, {
bool awaitable = true,
@visibleForTesting String? id,
}) async {
// ignore: no_leading_underscores_for_local_identifiers
final _id = id ?? _uuid.v4();
var future = Future<R?>.value();
_log.fine('sendData: Waiting to acquire lock...');
await _lock.synchronized(() async {
_log.fine('sendData: Done');
if (awaitable) {
_awaitables[_id] = Completer();
onAdd();
}
await sendDataImpl(
DataWrapper<S>(
_id,
data,
),
);
_log.fine('sendData: Done');
if (awaitable) {
_awaitables[_id] = Completer();
onAdd();
}
if (awaitable) {
future = _awaitables[_id]!.future;
}
await sendDataImpl(
DataWrapper<S>(
_id,
data,
),
);
_log.fine('sendData: Releasing lock...');
if (awaitable) {
future = _awaitables[_id]!.future;
}
_log.fine('sendData: Releasing lock...');
});
return future;
@@ -128,7 +130,7 @@ abstract class AwaitableDataSender<
});
completer?.complete(data.data);
return completer != null;
}
}