Created
June 23, 2020 15:10
-
-
Save philip-wernersbach/9bc5eee1d68d4d0ef16e9202789bd7c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/main.dart b/lib/main.dart | |
index 5169bd5..0be3e6f 100644 | |
--- a/lib/main.dart | |
+++ b/lib/main.dart | |
@@ -96,102 +96,13 @@ class MainAppState extends State<MainApp> { | |
MainAppState(); | |
Future<void> initProcess() async { | |
- _process = await openProcess(); | |
_eventController = StreamController<AppEvent>(); | |
- // TODO: We use asBroadcastStream() to be able to read the first element. | |
- // There is possibly a cleaner way to do this. | |
- final fromProcess = _process.stdout | |
- .transform(utf8.decoder) | |
- .transform(const LineSplitter()) | |
- .map((String data) { | |
- myPrint('Message from process:\n$data'); | |
- // TODO: deserializeMsg might raise an exception. How to handle? | |
- return deserializeMsg<ServerToUserAck>(data); | |
- }).asBroadcastStream(); | |
- | |
- // The first message must contain the current state of all nodes: | |
- final ServerToUserAck serverToUserAck = await fromProcess.first; | |
- final serverToUser = serverToUserAck.match( | |
- serverToUser: (serverToUser) => serverToUser, | |
- ack: (_) => throw MainAppError('Invalid first incoming message')); | |
- | |
- final nodesStatus = serverToUser.match( | |
- nodeOpened: (_) => throw MainAppError('Invalid first incoming message'), | |
- nodesStatus: (nodesStatus) => nodesStatus, | |
- node: (_a, _b) => throw MainAppError('Invalid first incoming message')); | |
- | |
// Create initial appState (according to first incoming message from stcompact) | |
final preAppState = AppState((b) => b | |
..viewState = ViewState.view(AppView.home()) | |
..nodesStates = BuiltMap<NodeName, NodeState>().toBuilder()); | |
- _appState = handleNodesStatus(preAppState, nodesStatus); | |
- | |
- // Handle messages from stcompact: | |
- _streamSubs.add(fromProcess.listen((serverToUserAck) { | |
- // TODO; `deserializeMsg` could raise an exception. How to handle it? | |
- _eventController.add(AppEvent.serverToUserAck(serverToUserAck)); | |
- }, onError: (err) { | |
- throw MainAppError('fromProcess listen error: $err'); | |
- })); | |
- | |
- _process.exitCode.then((exitCode) { | |
- logger.e('Process exited with code: $exitCode'); | |
- // TODO: Is this a reasonable place to close the eventController? | |
- _eventController.close(); | |
- throw MainAppError('Remote process closed!'); | |
- // TODO: What to do in this case? | |
- // Ideas: | |
- // - Close the app? | |
- // - Show an error screen and then close the program? | |
- }); | |
- | |
- _streamSubs.add(_process.stderr.transform(utf8.decoder).listen((data) { | |
- logger.e('stderr:\n$data'); | |
- }, onError: (err) { | |
- throw MainAppError('stderr listen error: $err'); | |
- })); | |
- | |
- // Handle shared files: | |
- final handleSharedFiles = (List<String> filePaths) { | |
- if (filePaths == null) { | |
- logger.w('handleSharedFiles(): Received null filePaths. Aborting'); | |
- return; | |
- } | |
- if (filePaths.isEmpty) { | |
- logger.w('handleSharedFiles(): Received empty shared filePaths!'); | |
- return; | |
- } | |
- if (filePaths.length > 1) { | |
- logger.w( | |
- 'handleSharedFiles(): Received more than one file path! Aborting.'); | |
- return; | |
- } | |
- // We received exactly one filePath: | |
- final filePath = filePaths[0]; | |
- // Queue shared file event: | |
- _eventController.add(AppEvent.sharedFile(filePath)); | |
- }; | |
- | |
- // Listen to incoming shared files: | |
- // For sharing files coming from outside the app while the app is in the memory | |
- _streamSubs | |
- .add(ReceiveFileIntent.getFileStream().listen((List<String> filePaths) { | |
- handleSharedFiles(filePaths); | |
- }, onError: (err) { | |
- throw MainAppError('getIntentDataStreamError: $err'); | |
- })); | |
- | |
- // For sharing files coming from outside the app while the app is closed | |
- ReceiveFileIntent.getInitialFile().then((List<String> filePaths) { | |
- handleSharedFiles(filePaths); | |
- }); | |
- | |
- final sendUserToServerAck = (UserToServerAck userToServerAck) { | |
- final data = serializeMsg<UserToServerAck>(userToServerAck); | |
- myPrint('Sending to process:\n$data'); | |
- _process.stdin.writeln(data); | |
- }; | |
+ _appState = handleNodesStatus(preAppState, BuiltMap<NodeName, NodeStatus>()); | |
// Secure random generator: | |
final rand = Random.secure(); | |
@@ -199,8 +110,7 @@ class MainAppState extends State<MainApp> { | |
// Begin handling events: | |
_streamSubs.add(this._eventController.stream.listen((appEvent) { | |
final newAppState1 = handleAppEvent(this._appState, appEvent, rand); | |
- final newAppState2 = attemptSend(newAppState1, sendUserToServerAck); | |
- setState(() => this._appState = newAppState2); | |
+ setState(() => this._appState = newAppState1); | |
}, onError: (err) { | |
throw MainAppError('_eventController error: $err'); | |
})); | |
diff --git a/tool/presubmit b/tool/presubmit | |
index c391573..bc6c272 100755 | |
--- a/tool/presubmit | |
+++ b/tool/presubmit | |
@@ -6,7 +6,7 @@ | |
set -eux -o pipefail | |
echo "*** Formatting ..." | |
-dartfmt -w $(find bin lib test -name \*.dart 2>/dev/null) | |
+#dartfmt -w $(find bin lib test -name \*.dart 2>/dev/null) | |
echo "*** Get dependencies ..." | |
flutter pub get |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment