BlueGit

ict rusty-teams

Files

rusty-teams / crates / webview / src / sleep.rs

sleep.rs

sleep.rs 5.0 KiB Download Raw
1use std::collections::{HashMap, HashSet};
2use std::time::{Duration, Instant};
3
4use session::App;
5
6pub(crate) const SLEEP_AFTER: Duration = Duration::from_secs(180);
7
8/// What keeps the web views running; any of these blocks closing them.
9#[derive(Debug, Clone, Copy, Default)]
10pub(crate) struct Activity {
11 pub restarting: bool,
12 pub all_parked: bool,
13 pub login_pending: bool,
14 pub held: bool,
15 pub in_flight: bool,
16 pub dialogs_open: bool,
17 pub subscribed: bool,
18}
19
20pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {
21 [
22 (activity.restarting, "restarting"),
23 (!activity.all_parked, "an app is not parked"),
24 (activity.login_pending, "sign-in pending"),
25 (activity.held, "an app is held"),
26 (activity.in_flight, "a protocol call is running"),
27 (activity.dialogs_open, "a dialog is open"),
28 (activity.subscribed, "a tab subscriber is attached"),
29 ]
30 .into_iter()
31 .find_map(|(blocked, reason)| blocked.then_some(reason))
32}
33
34/// The web views close once both apps are parked and idle, and come back on the next protocol call, at the park page.
35#[derive(Debug)]
36pub(crate) struct Sleep<C> {
37 asleep: bool,
38 last_use: Instant,
39 loading: HashSet<App>,
40 queued: HashMap<App, Vec<C>>,
41}
42
43impl<C> Default for Sleep<C> {
44 fn default() -> Self {
45 Sleep { asleep: false, last_use: Instant::now(), loading: HashSet::new(), queued: HashMap::new() }
46 }
47}
48
49impl<C> Sleep<C> {
50 pub(crate) fn touch(&mut self, now: Instant) {
51 self.last_use = now;
52 }
53
54 pub(crate) fn asleep(&self) -> bool {
55 self.asleep
56 }
57
58 pub(crate) fn should_sleep(&self, activity: Activity, now: Instant) -> bool {
59 self.quiet(now) && blocker(activity).is_none()
60 }
61
62 /// Answers whether the views have been idle long enough to close.
63 pub(crate) fn quiet(&self, now: Instant) -> bool {
64 !self.asleep && self.loading.is_empty() && now.duration_since(self.last_use) >= SLEEP_AFTER
65 }
66
67 pub(crate) fn fall_asleep(&mut self) {
68 self.asleep = true;
69 }
70
71 /// Answers whether the views have to be created now.
72 pub(crate) fn wake(&mut self) -> bool {
73 if !self.asleep {
74 return false;
75 }
76 self.asleep = false;
77 self.loading = App::ALL.into_iter().collect();
78 true
79 }
80
81 /// A call for an app that is still loading its page waits; `Some` hands it back to run now.
82 pub(crate) fn admit(&mut self, app: App, command: C) -> Option<C> {
83 if self.loading.contains(&app) {
84 self.queued.entry(app).or_default().push(command);
85 None
86 } else {
87 Some(command)
88 }
89 }
90
91 pub(crate) fn loaded(&mut self, app: App) -> Vec<C> {
92 self.loading.remove(&app);
93 self.queued.remove(&app).unwrap_or_default()
94 }
95
96 pub(crate) fn reset(&mut self) -> Vec<C> {
97 self.asleep = false;
98 self.loading.clear();
99 self.queued.drain().flat_map(|(_, commands)| commands).collect()
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106
107 fn idle() -> Activity {
108 Activity { all_parked: true, ..Activity::default() }
109 }
110
111 #[test]
112 fn parked_idle_views_sleep_after_the_quiet_period() {
113 let sleep: Sleep<u32> = Sleep::default();
114 let start = sleep.last_use;
115 assert!(!sleep.should_sleep(idle(), start + SLEEP_AFTER - Duration::from_secs(1)));
116 assert!(sleep.should_sleep(idle(), start + SLEEP_AFTER));
117 }
118
119 #[test]
120 fn a_running_app_a_login_or_any_user_keeps_them_awake() {
121 let sleep: Sleep<u32> = Sleep::default();
122 let later = sleep.last_use + SLEEP_AFTER;
123 assert!(!sleep.should_sleep(Activity { all_parked: false, ..idle() }, later));
124 for blocked in [
125 Activity { login_pending: true, ..idle() },
126 Activity { held: true, ..idle() },
127 Activity { in_flight: true, ..idle() },
128 Activity { dialogs_open: true, ..idle() },
129 Activity { subscribed: true, ..idle() },
130 Activity { restarting: true, ..idle() },
131 ] {
132 assert!(!sleep.should_sleep(blocked, later), "{blocked:?}");
133 }
134 }
135
136 #[test]
137 fn the_first_blocker_is_named() {
138 assert_eq!(blocker(idle()), None);
139 assert_eq!(blocker(Activity { held: true, dialogs_open: true, ..idle() }), Some("an app is held"));
140 }
141
142 #[test]
143 fn calls_wait_for_the_page_after_waking_and_run_in_order() {
144 let mut sleep = Sleep::default();
145 sleep.fall_asleep();
146 assert!(sleep.wake());
147 assert!(!sleep.wake());
148 assert_eq!(sleep.admit(App::Teams, 1), None);
149 assert_eq!(sleep.admit(App::Teams, 2), None);
150 assert_eq!(sleep.admit(App::Outlook, 3), None);
151 assert!(!sleep.should_sleep(idle(), sleep.last_use + SLEEP_AFTER));
152 assert_eq!(sleep.loaded(App::Teams), vec![1, 2]);
153 assert_eq!(sleep.admit(App::Teams, 4), Some(4));
154 assert_eq!(sleep.reset(), vec![3]);
155 assert_eq!(sleep.admit(App::Outlook, 5), Some(5));
156 }
157}