lb_rs/service/
events.rs

1pub use tokio::sync::broadcast::{self, Receiver, Sender};
2use tracing::*;
3use uuid::Uuid;
4
5use crate::Lb;
6
7use super::sync::SyncIncrement;
8
9#[derive(Clone)]
10pub struct EventSubs {
11    tx: Sender<Event>,
12}
13
14#[derive(Clone, Debug)]
15pub enum Event {
16    /// A metadata for a given id or it's descendants changed. The id returned
17    /// may be deleted. Updates to document contents will not cause this
18    /// message to be sent (unless a document was deleted).
19    MetadataChanged,
20
21    /// The contents of this document have changed either by this lb
22    /// library or as a result of sync
23    DocumentWritten(Uuid, Option<Actor>),
24
25    PendingSharesChanged,
26
27    Sync(SyncIncrement),
28
29    StatusUpdated,
30}
31
32#[derive(Debug, Clone)]
33pub enum Actor {
34    Workspace,
35    Sync,
36}
37
38impl Default for EventSubs {
39    fn default() -> Self {
40        let (tx, _) = broadcast::channel::<Event>(10000);
41        Self { tx }
42    }
43}
44
45impl EventSubs {
46    pub(crate) fn pending_shares_changed(&self) {
47        self.queue(Event::PendingSharesChanged);
48    }
49
50    pub(crate) fn meta_changed(&self) {
51        self.queue(Event::MetadataChanged);
52    }
53
54    pub(crate) fn doc_written(&self, id: Uuid, actor: Option<Actor>) {
55        self.queue(Event::DocumentWritten(id, actor));
56    }
57
58    pub(crate) fn sync(&self, s: SyncIncrement) {
59        self.queue(Event::Sync(s));
60    }
61
62    pub(crate) fn status_updated(&self) {
63        self.queue(Event::StatusUpdated);
64    }
65
66    fn queue(&self, evt: Event) {
67        if let Err(e) = self.tx.send(evt.clone()) {
68            error!(?evt, ?e, "could not queue");
69        }
70    }
71}
72
73impl Lb {
74    pub fn subscribe(&self) -> Receiver<Event> {
75        self.events.tx.subscribe()
76    }
77}