hook resync state

master
fuwa 5 years ago
parent 905aecb6f2
commit 143e2c3c88

@ -59,7 +59,7 @@ Future<int> targetHeight() async {
body: body
);
print('Response status: ${response.statusCode}');
// print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
return -1;
} else {

@ -33,6 +33,7 @@ import 'widget/loading.dart';
import 'widget/blank.dart';
import 'widget/syncing.dart';
import 'widget/synced.dart';
import 'widget/resyncing.dart';
void main() => runApp(MyApp());
@ -73,6 +74,11 @@ class _MyHomePageState extends State<MyHomePage>
);
}
AppState _getState() {
return _state;
}
void _updateLoading(LoadingState state, String msg) {
print('updateLoading: ' + msg);
}
@ -88,10 +94,21 @@ class _MyHomePageState extends State<MyHomePage>
SyncingState _syncingState = await _loadingState.next(loading, '');
final syncing = runBinary(binName);
SyncedState _syncedState = await _syncingState.next(syncing);
final syncing = runBinary(binName).asBroadcastStream();
SyncedState _syncedState = await _syncingState.next(syncing);
await _syncedState.next();
while (true) {
await _getState().use
(
(s) => () => 0,
(s) => () => 0,
(s) => () => 0,
(s) => s.next(),
(s) => s.next(),
);
}
}
@override
@ -123,6 +140,7 @@ class _MyHomePageState extends State<MyHomePage>
(s) => buildLoading(context, s),
(s) => buildSyncing(context, s),
(s) => buildSynced(context, s),
(s) => buildReSyncing(context, s),
),
);
}

@ -38,6 +38,7 @@ abstract class AppState {
T Function(LoadingState) useLoadingState,
T Function(SyncingState) useSyncingState,
T Function(SyncedState) useSyncedState,
T Function(ReSyncingState) useReSyncingState,
)
{
if (this is BlankState) {
@ -52,6 +53,9 @@ abstract class AppState {
if (this is SyncedState) {
return useSyncedState(this);
}
if (this is ReSyncingState) {
return useReSyncingState(this);
}
throw Exception('Invalid state');
}
}
@ -103,7 +107,7 @@ class LoadingState extends HookedState {
}
Future<void> load() async {
print("LoadingState.next");
print("Loading next");
await for (var line in loadingProgress) {
// append(line);
print(line);
@ -125,48 +129,87 @@ class LoadingState extends HookedState {
}
class SyncingState extends HookedState {
String status;
String stdout;
SyncingState(f, this.status) : super (f);
SyncingState(f, this.stdout) : super (f);
void append(String msg) {
this.status += msg;
this.stdout += msg;
setState(this);
}
Future<SyncedState> next(Stream<String> processOutput) async {
print("SyncingState.next");
print("Syncing next");
await for (var line in processOutput) {
append(line);
print(line);
// print('hi');
final _targetHeight = await targetHeight();
print('target_height: ${_targetHeight}');
if (_targetHeight == 0) break;
}
SyncedState _next = SyncedState(setState, processOutput);
SyncedState _next = SyncedState(setState, stdout, processOutput);
setState(_next);
return _next;
}
}
class SyncedState extends HookedState {
String stdout;
Stream<String> processOutput;
SyncedState(f, this.processOutput) : super (f);
SyncedState(f, this.stdout, this.processOutput) : super (f);
Future<void> next() async {
print("SyncedState.next");
Future<ReSyncingState> next() async {
print("Synced next");
while (true) {
final _targetHeight = await targetHeight();
print('target_height: ${_targetHeight}');
if (_targetHeight != 0) break;
await Future.delayed(const Duration(seconds: 2), () => "1");
}
// print('synced: loop exit');
ReSyncingState _next = ReSyncingState(setState, stdout, processOutput);
setState(_next);
return _next;
}
}
class ReSyncingState extends HookedState {
String stdout;
Stream<String> processOutput;
ReSyncingState(f, this.stdout, this.processOutput) : super (f);
void append(String msg) {
this.stdout += msg;
setState(this);
}
Future<SyncedState> next() async {
print("ReSyncing next");
await for (var line in processOutput) {
append(line);
print(line);
final _targetHeight = await targetHeight();
if (_targetHeight == 0) break;
}
print('resync: await exit');
SyncedState _next = SyncedState(setState, stdout, processOutput);
setState(_next);
return _next;
}
}

@ -0,0 +1,71 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import '../state.dart';
import '../config.dart';
Widget buildReSyncing(BuildContext context, ReSyncingState state) {
return Scaffold
(
// appBar: AppBar
// (
// // title: Text(widget.title),
// title: Text('CyberWOW'),
// ),
body: new Container
(
// padding: const EdgeInsets.all(10.0),
color: config.backgroundColor,
child: Align
(
alignment: Alignment.topLeft,
child: Column
(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>
[
new Expanded
(
flex: 1,
child: new SingleChildScrollView
(
scrollDirection: Axis.vertical,
reverse: true,
child: Text
(
state.stdout,
style: TextStyle
(
fontFamily: 'RobotoMono',
fontSize: 11,
color: config.textColor,
),
)
)
)
],
),
),
),
);
}

@ -53,7 +53,7 @@ Widget buildSyncing(BuildContext context, SyncingState state) {
reverse: true,
child: Text
(
state.status,
state.stdout,
style: TextStyle
(
fontFamily: 'RobotoMono',

@ -13,7 +13,7 @@ int main()
i++;
sleep(1);
if (i > 3) break;
if (i > 20) break;
}
return 0;
}

Loading…
Cancel
Save