use revision::revisioned;
use crate::kvs::impl_kv_value_revisioned;
use crate::val::{RecordId, Value};
#[revisioned(revision = 1)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub(crate) enum LiveAction {
Create,
Update,
Delete,
}
#[revisioned(revision = 1)]
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LiveEvent {
pub action: LiveAction,
pub id: RecordId,
pub before: Value,
pub after: Value,
}
#[revisioned(revision = 1)]
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LiveEvents(pub Vec<LiveEvent>);
impl_kv_value_revisioned!(LiveEvents);
impl LiveEvents {
pub(crate) fn new() -> Self {
Self(Vec::new())
}
pub(crate) fn push_record_change(&mut self, id: RecordId, before: Value, after: Value) {
let action = if after.is_nullish() {
LiveAction::Delete
} else if before.is_none() {
LiveAction::Create
} else {
LiveAction::Update
};
self.0.push(LiveEvent {
action,
id,
before,
after,
});
}
}