BlueGit

ict rusty-teams

Files

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

sleep.rs

sleep.rs 4.4 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
20/// The web views close once both apps are parked and idle, and come back on the next protocol call, at the park page.
21#[derive(Debug)]
22pub(crate) struct Sleep<C> {
23 asleep: bool,
24 last_use: Instant,
25 loading: HashSet<App>,
26 queued: HashMap<App, Vec<C>>,
27}
28
29impl<C> Default for Sleep<C> {
30 fn default() -> Self {
31 Sleep { asleep: false, last_use: Instant::now(), loading: HashSet::new(), queued: HashMap::new() }
32 }
33}
34
35impl<C> Sleep<C> {
36 pub(crate) fn touch(&mut self, now: Instant) {
37 self.last_use = now;
38 }
39
40 pub(crate) fn asleep(&self) -> bool {
41 self.asleep
42 }
43
44 pub(crate) fn should_sleep(&self, activity: Activity, now: Instant) -> bool {
45 !self.asleep
46 && self.loading.is_empty()
47 && now.duration_since(self.last_use) >= SLEEP_AFTER
48 && activity.all_parked
49 && !(activity.restarting
50 || activity.login_pending
51 || activity.held
52 || activity.in_flight
53 || activity.dialogs_open
54 || activity.subscribed)
55 }
56
57 pub(crate) fn fall_asleep(&mut self) {
58 self.asleep = true;
59 }
60
61 /// Answers whether the views have to be created now.
62 pub(crate) fn wake(&mut self) -> bool {
63 if !self.asleep {
64 return false;
65 }
66 self.asleep = false;
67 self.loading = App::ALL.into_iter().collect();
68 true
69 }
70
71 /// A call for an app that is still loading its page waits; `Some` hands it back to run now.
72 pub(crate) fn admit(&mut self, app: App, command: C) -> Option<C> {
73 if self.loading.contains(&app) {
74 self.queued.entry(app).or_default().push(command);
75 None
76 } else {
77 Some(command)
78 }
79 }
80
81 pub(crate) fn loaded(&mut self, app: App) -> Vec<C> {
82 self.loading.remove(&app);
83 self.queued.remove(&app).unwrap_or_default()
84 }
85
86 pub(crate) fn reset(&mut self) -> Vec<C> {
87 self.asleep = false;
88 self.loading.clear();
89 self.queued.drain().flat_map(|(_, commands)| commands).collect()
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 fn idle() -> Activity {
98 Activity { all_parked: true, ..Activity::default() }
99 }
100
101 #[test]
102 fn parked_idle_views_sleep_after_the_quiet_period() {
103 let sleep: Sleep<u32> = Sleep::default();
104 let start = sleep.last_use;
105 assert!(!sleep.should_sleep(idle(), start + SLEEP_AFTER - Duration::from_secs(1)));
106 assert!(sleep.should_sleep(idle(), start + SLEEP_AFTER));
107 }
108
109 #[test]
110 fn a_running_app_a_login_or_any_user_keeps_them_awake() {
111 let sleep: Sleep<u32> = Sleep::default();
112 let later = sleep.last_use + SLEEP_AFTER;
113 assert!(!sleep.should_sleep(Activity { all_parked: false, ..idle() }, later));
114 for blocked in [
115 Activity { login_pending: true, ..idle() },
116 Activity { held: true, ..idle() },
117 Activity { in_flight: true, ..idle() },
118 Activity { dialogs_open: true, ..idle() },
119 Activity { subscribed: true, ..idle() },
120 Activity { restarting: true, ..idle() },
121 ] {
122 assert!(!sleep.should_sleep(blocked, later), "{blocked:?}");
123 }
124 }
125
126 #[test]
127 fn calls_wait_for_the_page_after_waking_and_run_in_order() {
128 let mut sleep = Sleep::default();
129 sleep.fall_asleep();
130 assert!(sleep.wake());
131 assert!(!sleep.wake());
132 assert_eq!(sleep.admit(App::Teams, 1), None);
133 assert_eq!(sleep.admit(App::Teams, 2), None);
134 assert_eq!(sleep.admit(App::Outlook, 3), None);
135 assert!(!sleep.should_sleep(idle(), sleep.last_use + SLEEP_AFTER));
136 assert_eq!(sleep.loaded(App::Teams), vec![1, 2]);
137 assert_eq!(sleep.admit(App::Teams, 4), Some(4));
138 assert_eq!(sleep.reset(), vec![3]);
139 assert_eq!(sleep.admit(App::Outlook, 5), Some(5));
140 }
141}