use std::collections::HashSet;
const EMPTY_PAGE_LIMIT: u8 = 3;
#[derive(Debug, Default)]
pub(crate) struct CursorGuard {
seen: HashSet<String>,
consecutive_empty: u8,
}
impl CursorGuard {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn advance(
&mut self,
cursor: Option<String>,
has_more: Option<bool>,
was_empty: bool,
) -> Option<String> {
if has_more == Some(false) {
return None;
}
self.consecutive_empty = if was_empty { self.consecutive_empty + 1 } else { 0 };
if self.consecutive_empty >= EMPTY_PAGE_LIMIT {
return None;
}
let cursor = cursor?;
if cursor.is_empty() || !self.seen.insert(cursor.clone()) {
return None;
}
Some(cursor)
}
}