From 038af999f9836d30df2cff15f9c26036ccced013 Mon Sep 17 00:00:00 2001 From: fuwa Date: Thu, 25 Jul 2019 01:24:08 +0000 Subject: [PATCH] add exiting func --- cyberwow/lib/state.dart | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index fde28ed..a43357b 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -68,11 +68,13 @@ abstract class AppState { typedef SetStateFunc = void Function(AppState); typedef GetNotificationFunc = AppLifecycleState Function(); +typedef IsExitingFunc = bool Function(); class HookedState extends AppState { final SetStateFunc setState; final GetNotificationFunc getNotification; - HookedState(this.setState, this.getNotification); + final GetExitingFunc isExiting; + HookedState(this.setState, this.getNotification, this.isExiting); syncState() { setState(this); @@ -313,3 +315,34 @@ class ReSyncingState extends HookedState { return moveState(_next); } } + +class ExitingState extends HookedState { + Queue stdout; + Stream processOutput; + + ExitingState(f, s, this.stdout, this.processOutput) : super (f, s); + + void append(String msg) { + stdout.addLast(msg); + while (stdout.length > config.stdoutLineBufferSize) { + stdout.removeFirst(); + } + syncState(); + } + + Future wait() async { + log.fine("Exiting wait"); + + Future printStdout() async { + await for (final line in processOutput) { + if (synced) break; + log.finest('exiting: print stdout loop'); + + append(line); + log.info(line); + } + } + + await printStdout(); + } +}