use crate::serve::storage::Failure;
use serde_json::Value;
pub(super) struct Replay {
conversation: String,
pub dirty: bool,
pub held: Vec<Value>,
bytes: usize,
pub refused: Option<Failure>,
}
impl Replay {
pub const fn new(conversation: String) -> Self {
Self {
conversation,
dirty: false,
held: Vec::new(),
bytes: 0,
refused: None,
}
}
pub fn capture(&mut self, item: &Value, bound: usize) -> bool {
if item["conversation"] != self.conversation {
return false;
}
if item["durable"] == true {
self.dirty = true;
} else if self.refused.is_none() {
let bytes = self.bytes.saturating_add(item.to_string().len());
if bytes > bound {
self.refused = Some(crate::door::assistant_state::bound(
"events_after_bytes",
bound,
bytes,
));
} else {
self.bytes = bytes;
self.held.push(item.clone());
}
}
true
}
}