Skip to content

Commit

Permalink
[gui] make grpc client check for updates on specific rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Sep 12, 2024
1 parent 342584e commit 6a3f021
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/client/gui/lib/grpc_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import 'package:grpc/grpc.dart';
import 'package:protobuf/protobuf.dart' hide RpcClient;
import 'package:rxdart/rxdart.dart';

import 'generated/multipass.pbgrpc.dart';
import 'logger.dart';
import 'providers.dart';
import 'update_available.dart';

export 'generated/multipass.pbgrpc.dart';

Expand All @@ -21,6 +22,23 @@ extension on RpcMessage {
String get repr => '$runtimeType${toProto3Json()}';
}

T checkForUpdate<T extends RpcMessage>(T t) {
final updateInfo = switch (t) {
LaunchReply launchReply => launchReply.updateInfo,
InfoReply infoReply => infoReply.updateInfo,
ListReply listReply => listReply.updateInfo,
NetworksReply networksReply => networksReply.updateInfo,
StartReply startReply => startReply.updateInfo,
RestartReply restartReply => restartReply.updateInfo,
VersionReply versionReply => versionReply.updateInfo,
_ => UpdateInfo(),
};

providerContainer.read(updateProvider.notifier).set(updateInfo);

return t;
}

void Function(StreamNotification<RpcMessage>) logGrpc(RpcMessage request) {
return (notification) {
switch (notification.kind) {
Expand Down Expand Up @@ -62,6 +80,7 @@ class GrpcClient {
logger.i('Sent ${request.repr}');
yield* _client
.launch(Stream.value(request))
.map(checkForUpdate)
.doOnEach(logGrpc(request))
.map(Either.left);
for (final mountRequest in mountRequests) {
Expand All @@ -84,6 +103,7 @@ class GrpcClient {
logger.i('Sent ${request.repr}');
return _client
.start(Stream.value(request))
.map(checkForUpdate)
.doOnEach(logGrpc(request))
.firstOrNull;
}
Expand Down Expand Up @@ -117,6 +137,7 @@ class GrpcClient {
logger.i('Sent ${request.repr}');
return _client
.restart(Stream.value(request))
.map(checkForUpdate)
.doOnEach(logGrpc(request))
.firstOrNull;
}
Expand Down Expand Up @@ -167,6 +188,7 @@ class GrpcClient {
);
return _client
.info(Stream.value(request))
.map(checkForUpdate)
.last
.then((r) => r.details.toList());
}
Expand Down Expand Up @@ -204,6 +226,7 @@ class GrpcClient {
logger.i('Sent ${request.repr}');
return _client
.networks(Stream.value(request))
.map(checkForUpdate)
.doOnEach(logGrpc(request))
.last
.then((r) => r.interfaces);
Expand All @@ -214,21 +237,12 @@ class GrpcClient {
logger.i('Sent ${request.repr}');
return _client
.version(Stream.value(request))
.map(checkForUpdate)
.doOnEach(logGrpc(request))
.last
.then((reply) => reply.version);
}

Future<UpdateInfo> updateInfo() {
final request = VersionRequest();
logger.i('Sent ${request.repr}');
return _client
.version(Stream.value(request))
.doOnEach(logGrpc(request))
.last
.then((reply) => reply.updateInfo);
}

Future<String> get(String key) {
final request = GetRequest(key: key);
logger.i('Sent ${request.repr}');
Expand Down

0 comments on commit 6a3f021

Please sign in to comment.