use timeline::history_step::HistoryStep;
use self::timeline::Timeline;
use super::{Entity, id::entity_id::EntityId, identity::IdentityStub, operation::Operation};
pub mod timeline;
#[derive(Debug)]
pub struct Snapshot<E: Entity> {
id: EntityId<E>,
timeline: E::Timeline,
}
impl<E: Entity> Clone for Snapshot<E>
where
E::Timeline: Clone,
{
fn clone(&self) -> Self {
Self {
id: self.id,
timeline: self.timeline.clone(),
}
}
}
impl<E: Entity> Snapshot<E> {
#[must_use]
pub fn timeline(&self) -> &E::Timeline {
&self.timeline
}
#[must_use]
pub fn id(&self) -> EntityId<E> {
self.id
}
pub fn apply(&mut self, op: &Operation<E>) {
self.timeline.add(op);
}
pub fn actors<'a>(&'a self) -> impl Iterator<Item = IdentityStub> + use<'a, E>
where
<E as Entity>::HistoryStep: 'a,
{
self.timeline.history().iter().map(HistoryStep::author)
}
pub fn from_root_operation(op: &Operation<E>) -> Self {
let timeline = E::Timeline::from_root_operation(op);
Self {
id: op.id(),
timeline,
}
}
}