diff --git a/cyberwow/lib/controller/refresh.dart b/cyberwow/lib/controller/refresh.dart new file mode 100644 index 0000000..98b1fd8 --- /dev/null +++ b/cyberwow/lib/controller/refresh.dart @@ -0,0 +1,40 @@ +/* + +Copyright 2019 fuwa + +This file is part of CyberWOW. + +CyberWOW is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +CyberWOW is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with CyberWOW. If not, see . + +*/ + +import 'dart:ui'; +import 'dart:async'; +import 'rpc.dart' as rpc; + +typedef GetNotificationFunc = AppLifecycleState Function(); + +Stream targetHeight(GetNotificationFunc getNotification) async* { + while (true) { + final _appState = getNotification(); + print('refresh targetHeight: app state: ${_appState}'); + + if (_appState == AppLifecycleState.resumed) { + final _targetHeight = await rpc.targetHeight(); + yield _targetHeight + } + + await Future.delayed(const Duration(seconds: 2), () => "1"); + } +} diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index dccc723..deee380 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -30,6 +30,7 @@ import 'package:path_provider/path_provider.dart'; import 'controller/helper.dart'; import 'controller/rpc.dart' as rpc; +import 'controller/refresh.dart' as refresh; import 'config.dart'; abstract class AppState { @@ -202,6 +203,7 @@ class SyncedState extends HookedState { class ReSyncingState extends HookedState { String stdout; Stream processOutput; + bool synced = false; ReSyncingState(f, s, this.stdout, this.processOutput) : super (f, s); @@ -213,14 +215,27 @@ class ReSyncingState extends HookedState { Future next() async { print("ReSyncing next"); - await for (var line in processOutput) { - append(line); - print(line); + Future printStdout() async { + await for (var line in processOutput) { + append(line); + print(line); - final _targetHeight = await rpc.targetHeight(); - if (_targetHeight == 0) break; + if (synced) break; + } } + Future checkSync() async { + await for (var _targetHeight in refresh.targetHeight(getNotification)) { + if (_targetHeight == 0) { + synced = true; + break; + } + } + } + + printStdout(); + await checkSync(); + print('resync: await exit'); SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput); _next.height = await rpc.height();