use super::super::{AccountSummaryResult, AccountSummarySnapshot};
use crate::subscriptions::StreamDecoder;
#[derive(Debug, Default)]
pub(in crate::accounts) struct SnapshotBuilder {
snapshot: AccountSummarySnapshot,
pending: bool,
emitted: bool,
}
impl SnapshotBuilder {
pub(in crate::accounts) fn fold(&mut self, batch: Vec<AccountSummaryResult>) -> Option<AccountSummarySnapshot> {
let ended = batch.last().is_some_and(StreamDecoder::is_batch_end);
for result in batch {
if let AccountSummaryResult::Summary(summary) = result {
self.pending |= self.snapshot.apply(summary);
}
}
let complete = if self.emitted { self.pending } else { ended };
complete.then(|| self.take())
}
pub(in crate::accounts) fn flush(&mut self) -> Option<AccountSummarySnapshot> {
self.pending.then(|| self.take())
}
fn take(&mut self) -> AccountSummarySnapshot {
self.pending = false;
self.emitted = true;
self.snapshot.clone()
}
}
#[cfg(test)]
#[path = "snapshots_tests.rs"]
mod tests;