diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-05 17:46:16 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-05 17:46:16 +0100 |
commit | adf4d33f226a745330a3bb802fe9b96f263a0895 (patch) | |
tree | 28ba9871f00752998437c511f093b60c1b310bab /src/mail/snapshot.rs | |
parent | 335750a29a83edba9bce2fb7e1452001e4962d1f (diff) | |
download | aerogramme-adf4d33f226a745330a3bb802fe9b96f263a0895.tar.gz aerogramme-adf4d33f226a745330a3bb802fe9b96f263a0895.zip |
added some utility structures
Diffstat (limited to 'src/mail/snapshot.rs')
-rw-r--r-- | src/mail/snapshot.rs | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/mail/snapshot.rs b/src/mail/snapshot.rs index 7256d50..54bec64 100644 --- a/src/mail/snapshot.rs +++ b/src/mail/snapshot.rs @@ -1,11 +1,52 @@ use std::sync::Arc; + +use anyhow::Result; + use super::mailbox::Mailbox; use super::uidindex::UidIndex; -pub struct Snapshot { +/// 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 { pub mailbox: Arc<Mailbox>, pub snapshot: UidIndex, } -impl Snapshot { +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 { + let old_snapshot = self.snapshot.clone(); + self.snapshot = self.mailbox.current_uid_index().await; + + old_snapshot + } } |