From f27f5914e15980e6263fa18e01114a5def253a0f Mon Sep 17 00:00:00 2001 From: fuwa Date: Tue, 25 Jun 2019 16:54:44 +0000 Subject: [PATCH] copy re-sync logic to sync --- cyberwow/lib/state.dart | 48 +++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index 7469996..9f187fd 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -134,6 +134,7 @@ class LoadingState extends HookedState { class SyncingState extends HookedState { String stdout; + bool synced = false; SyncingState(f, s, this.stdout) : super (f, s); @@ -145,24 +146,39 @@ class SyncingState extends HookedState { Future next(Stream processOutput) async { print("Syncing next"); - await for (var line in processOutput) { - append(line); - print(line); - - final _targetHeight = await rpc.targetHeight(); - final _height = await rpc.height(); - // print('syncing: target height ${_targetHeight}'); - // print('syncing: height ${_height}'); - - // final _offline = await rpc.offline(); - // print('syncing: offline ${_offline}'); - - final _out_peers = await rpc.outgoing_connections_count(); - // print('syncing: out_peers ${_out_peers}'); - - if (_targetHeight == 0 && _out_peers > 0) break; + Future printStdout() async { + await for (var line in processOutput) { + if (synced) break; + // print('syncing: print stdout loop'); + append(line); + print(line); + } } + Future checkSync() async { + await for (var _targetHeight in refresh.targetHeight(getNotification)) { + print('syncing: target height ${_targetHeight}'); + + final _height = await rpc.height(); + print('syncing: height ${_height}'); + + // final _offline = await rpc.offline(); + // print('syncing: offline ${_offline}'); + + final _out_peers = await rpc.outgoing_connections_count(); + print('syncing: out_peers ${_out_peers}'); + + if (_targetHeight == 0 && _out_peers > 0) { + synced = true; + break; + } + // print('syncing: checkSync loop'); + } + } + + printStdout(); + await checkSync(); + print('syncing: loop exit'); SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput);