sos_core/events/
change.rs

1use crate::{commit::CommitSpan, events::EventLogType, AccountId};
2use serde::{Deserialize, Serialize};
3use std::sync::OnceLock;
4use tokio::sync::watch;
5
6static CHANGES_FEED: OnceLock<watch::Sender<LocalChangeEvent>> =
7    OnceLock::new();
8
9/// Change event.
10///
11/// Used for IPC communication when a process needs
12/// to know if changes have been made externally,
13///
14/// For example, the browser extension helper executable
15/// can detect changes made by the app and update it's
16/// view.
17#[derive(Serialize, Deserialize, Default, Debug, Clone)]
18#[serde(rename_all = "camelCase")]
19pub enum LocalChangeEvent {
20    /// Changes feed was initialized.
21    #[default]
22    Init,
23    /// Account was created.
24    AccountCreated(AccountId),
25    /// Account was modified.
26    AccountModified {
27        /// Account identifier.
28        account_id: AccountId,
29        /// Type of the event log.
30        log_type: EventLogType,
31        /// Span of commit hashes.
32        commit_span: CommitSpan,
33    },
34    /// Account was deleted.
35    AccountDeleted(AccountId),
36}
37
38/// Feed of change events.
39pub fn changes_feed<'a>() -> &'a watch::Sender<LocalChangeEvent> {
40    CHANGES_FEED.get_or_init(|| {
41        let (tx, _) = watch::channel(LocalChangeEvent::default());
42        tx
43    })
44}