aerogramme/aero-collections/src/mail/snapshot.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2024-01-05 14:36:40 +00:00
use std::sync::Arc;
2024-01-05 16:46:16 +00:00
use anyhow::Result;
2024-01-05 14:36:40 +00:00
use super::mailbox::Mailbox;
2024-01-06 10:33:56 +00:00
use super::query::{Query, QueryScope};
2024-01-05 14:36:40 +00:00
use super::uidindex::UidIndex;
2024-01-05 17:59:19 +00:00
use super::unique_ident::UniqueIdent;
2024-01-05 14:36:40 +00:00
2024-01-05 16:46:16 +00:00
/// A Frozen Mailbox has a snapshot of the current mailbox
/// state that is desynchronized with the real mailbox state.
/// It's up to the user to choose when their snapshot must be updated
/// to give useful information to their clients
pub struct FrozenMailbox {
2024-01-05 14:36:40 +00:00
pub mailbox: Arc<Mailbox>,
pub snapshot: UidIndex,
}
2024-01-05 16:46:16 +00:00
impl FrozenMailbox {
/// Create a snapshot from a mailbox, the mailbox + the snapshot
/// becomes the "Frozen Mailbox".
pub async fn new(mailbox: Arc<Mailbox>) -> Self {
let state = mailbox.current_uid_index().await;
Self {
mailbox,
snapshot: state,
}
}
/// Force the synchronization of the inner mailbox
/// but do not update the local snapshot
pub async fn sync(&self) -> Result<()> {
self.mailbox.opportunistic_sync().await
}
/// Peek snapshot without updating the frozen mailbox
/// Can be useful if you want to plan some writes
/// while sending a diff to the client later
pub async fn peek(&self) -> UidIndex {
self.mailbox.current_uid_index().await
}
/// Update the FrozenMailbox local snapshot.
/// Returns the old snapshot, so you can build a diff
pub async fn update(&mut self) -> UidIndex {
2024-01-06 10:33:56 +00:00
let old_snapshot = self.snapshot.clone();
self.snapshot = self.mailbox.current_uid_index().await;
2024-01-05 16:46:16 +00:00
2024-01-06 10:33:56 +00:00
old_snapshot
2024-01-05 16:46:16 +00:00
}
2024-01-05 17:59:19 +00:00
pub fn query<'a, 'b>(&'a self, uuids: &'b [UniqueIdent], scope: QueryScope) -> Query<'a, 'b> {
Query {
2024-01-06 10:33:56 +00:00
frozen: self,
emails: uuids,
scope,
2024-01-05 17:59:19 +00:00
}
}
2024-01-05 14:36:40 +00:00
}