BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / chat_paging.rs

chat_paging.rs

fix(sidebar): review findings
metis Daniel Däschle · pushed by metis · 7 weeks ago
6d166892 History
chat_paging.rs 5.3 KiB Download Raw
1#[derive(Debug, Clone, Default, PartialEq, Eq)]
2pub struct ChatPaging {
3 next_link: Option<String>,
4 exhausted: bool,
5 loading: bool,
6 epoch: u64,
7}
8
9impl ChatPaging {
10 pub fn restored(next_link: Option<String>, exhausted: bool) -> Self {
11 ChatPaging {
12 next_link,
13 exhausted,
14 ..ChatPaging::default()
15 }
16 }
17
18 pub fn is_known(&self) -> bool {
19 self.next_link.is_some() || self.exhausted
20 }
21
22 pub fn has_more(&self) -> bool {
23 self.next_link.is_some()
24 }
25
26 pub fn is_loading(&self) -> bool {
27 self.loading
28 }
29
30 pub fn next_link(&self) -> Option<&str> {
31 self.next_link.as_deref()
32 }
33
34 pub fn is_exhausted(&self) -> bool {
35 self.exhausted
36 }
37
38 pub fn is_current(&self, epoch: u64) -> bool {
39 self.epoch == epoch
40 }
41
42 pub fn begin(&mut self) -> Option<(String, u64)> {
43 if self.loading {
44 return None;
45 }
46 let link = self.next_link.clone()?;
47 self.loading = true;
48 Some((link, self.epoch))
49 }
50
51 pub fn finish(&mut self, epoch: u64, next_link: Option<String>) {
52 self.loading = false;
53 if self.is_current(epoch) {
54 self.exhausted = next_link.is_none();
55 self.next_link = next_link;
56 }
57 }
58
59 pub fn fail(&mut self, epoch: u64) {
60 self.loading = false;
61 if self.is_current(epoch) {
62 self.next_link = None;
63 self.exhausted = false;
64 }
65 }
66
67 pub fn adopt_continuation(&mut self, next_link: Option<String>) {
68 let before = (self.next_link.clone(), self.exhausted);
69 match next_link {
70 Some(link) if self.next_link.is_none() => {
71 self.next_link = Some(link);
72 self.exhausted = false;
73 }
74 Some(_) => {}
75 None => {
76 self.next_link = None;
77 self.exhausted = true;
78 }
79 }
80 if before != (self.next_link.clone(), self.exhausted) {
81 self.epoch += 1;
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 fn with_link(link: &str) -> ChatPaging {
91 ChatPaging::restored(Some(link.to_owned()), false)
92 }
93
94 #[test]
95 fn a_fresh_state_knows_nothing_and_loads_nothing() {
96 let mut paging = ChatPaging::default();
97 assert!(!paging.is_known());
98 assert!(!paging.has_more());
99 assert_eq!(paging.begin(), None);
100 }
101
102 #[test]
103 fn begin_hands_out_the_link_once_while_a_page_is_in_flight() {
104 let mut paging = with_link("page2");
105 assert_eq!(paging.begin(), Some(("page2".to_owned(), 0)));
106 assert!(paging.is_loading());
107 assert_eq!(paging.begin(), None);
108 }
109
110 #[test]
111 fn finish_with_a_link_allows_the_next_page() {
112 let mut paging = with_link("page2");
113 let (_, epoch) = paging.begin().unwrap();
114 paging.finish(epoch, Some("page3".to_owned()));
115 assert!(!paging.is_loading());
116 assert!(paging.has_more());
117 assert_eq!(paging.begin(), Some(("page3".to_owned(), 0)));
118 }
119
120 #[test]
121 fn finish_without_a_link_is_done_for_good() {
122 let mut paging = with_link("page2");
123 let (_, epoch) = paging.begin().unwrap();
124 paging.finish(epoch, None);
125 assert!(!paging.has_more());
126 assert!(paging.is_exhausted());
127 assert!(paging.is_known());
128 assert_eq!(paging.begin(), None);
129 }
130
131 #[test]
132 fn a_failed_page_drops_the_link_so_the_next_refresh_walks_again() {
133 let mut paging = with_link("page2");
134 let (_, epoch) = paging.begin().unwrap();
135 paging.fail(epoch);
136 assert!(!paging.is_loading());
137 assert!(!paging.has_more());
138 assert!(!paging.is_exhausted());
139 assert!(!paging.is_known());
140 assert_eq!(paging.begin(), None);
141 paging.adopt_continuation(Some("fresh".to_owned()));
142 assert_eq!(paging.next_link(), Some("fresh"));
143 }
144
145 #[test]
146 fn adopting_a_continuation_keeps_deeper_progress() {
147 let mut paging = with_link("deep");
148 paging.adopt_continuation(Some("shallow".to_owned()));
149 assert_eq!(paging.next_link(), Some("deep"));
150
151 let mut fresh = ChatPaging::default();
152 fresh.adopt_continuation(Some("shallow".to_owned()));
153 assert_eq!(fresh.next_link(), Some("shallow"));
154 }
155
156 #[test]
157 fn adopting_a_complete_walk_marks_everything_loaded() {
158 let mut paging = with_link("deep");
159 paging.adopt_continuation(None);
160 assert!(!paging.has_more());
161 assert!(paging.is_exhausted());
162 }
163
164 #[test]
165 fn a_result_from_before_a_refresh_changed_the_state_is_ignored() {
166 let mut paging = with_link("page2");
167 let (_, epoch) = paging.begin().unwrap();
168 paging.adopt_continuation(None);
169 assert!(!paging.is_current(epoch));
170 paging.finish(epoch, Some("stale".to_owned()));
171 assert!(!paging.is_loading());
172 assert_eq!(paging.next_link(), None);
173 assert!(paging.is_exhausted());
174 }
175
176 #[test]
177 fn an_unchanged_adoption_keeps_the_page_in_flight_valid() {
178 let mut paging = with_link("deep");
179 let (_, epoch) = paging.begin().unwrap();
180 paging.adopt_continuation(Some("shallow".to_owned()));
181 assert!(paging.is_current(epoch));
182 }
183}