| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use session::App; |
| 4 | |
| 5 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 6 | pub(crate) enum ParkDecision { |
| 7 | Park, |
| 8 | Wait, |
| 9 | } |
| 10 | |
| 11 | /// A navigation of the app tab destroys the page context of every running protocol call |
| 12 | /// and the Trouter sockets of a live call, so parking waits until the tab is idle. |
| 13 | #[derive(Debug, Default)] |
| 14 | pub(crate) struct ParkGate { |
| 15 | holds: HashMap<App, usize>, |
| 16 | in_flight: HashMap<App, usize>, |
| 17 | } |
| 18 | |
| 19 | impl ParkGate { |
| 20 | pub(crate) fn hold(&mut self, app: App, held: bool) { |
| 21 | adjust(&mut self.holds, app, held); |
| 22 | } |
| 23 | |
| 24 | pub(crate) fn command_started(&mut self, app: App) { |
| 25 | adjust(&mut self.in_flight, app, true); |
| 26 | } |
| 27 | |
| 28 | pub(crate) fn command_finished(&mut self, app: App) { |
| 29 | adjust(&mut self.in_flight, app, false); |
| 30 | } |
| 31 | |
| 32 | pub(crate) fn forget_commands(&mut self) { |
| 33 | self.in_flight.clear(); |
| 34 | } |
| 35 | |
| 36 | pub(crate) fn in_flight(&self, app: App) -> usize { |
| 37 | self.in_flight.get(&app).copied().unwrap_or(0) |
| 38 | } |
| 39 | |
| 40 | pub(crate) fn decide(&self, app: App) -> ParkDecision { |
| 41 | let held = self.holds.get(&app).copied().unwrap_or(0) > 0; |
| 42 | if held || self.in_flight(app) > 0 { |
| 43 | ParkDecision::Wait |
| 44 | } else { |
| 45 | ParkDecision::Park |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn adjust(counts: &mut HashMap<App, usize>, app: App, up: bool) { |
| 51 | let count = counts.entry(app).or_default(); |
| 52 | *count = if up { *count + 1 } else { count.saturating_sub(1) }; |
| 53 | } |
| 54 | |
| 55 | #[cfg(test)] |
| 56 | mod tests { |
| 57 | use super::*; |
| 58 | |
| 59 | #[test] |
| 60 | fn an_idle_tab_parks() { |
| 61 | assert_eq!(ParkGate::default().decide(App::Teams), ParkDecision::Park); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn a_running_command_delays_the_park_until_it_answers() { |
| 66 | let mut gate = ParkGate::default(); |
| 67 | gate.command_started(App::Teams); |
| 68 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 69 | assert_eq!(gate.decide(App::Outlook), ParkDecision::Park); |
| 70 | gate.command_finished(App::Teams); |
| 71 | assert_eq!(gate.decide(App::Teams), ParkDecision::Park); |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn a_held_tab_waits_for_every_hold_to_end() { |
| 76 | let mut gate = ParkGate::default(); |
| 77 | gate.hold(App::Teams, true); |
| 78 | gate.hold(App::Teams, true); |
| 79 | gate.hold(App::Teams, false); |
| 80 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 81 | gate.hold(App::Teams, false); |
| 82 | assert_eq!(gate.decide(App::Teams), ParkDecision::Park); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn extra_releases_do_not_underflow() { |
| 87 | let mut gate = ParkGate::default(); |
| 88 | gate.hold(App::Teams, false); |
| 89 | gate.command_finished(App::Teams); |
| 90 | gate.hold(App::Teams, true); |
| 91 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn a_restart_forgets_unanswered_commands_but_keeps_holds() { |
| 96 | let mut gate = ParkGate::default(); |
| 97 | gate.command_started(App::Outlook); |
| 98 | gate.hold(App::Teams, true); |
| 99 | gate.forget_commands(); |
| 100 | assert_eq!(gate.decide(App::Outlook), ParkDecision::Park); |
| 101 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 102 | } |
| 103 | } |