Files
spacedrive/core/src/notifications.rs
Ericson "Fogo" Soares 68ca2382fd [ENG-1627] Move all jobs and actors to an "Old" namespace (#2157)
* Bunch of new warns due to a stronger clippy config

* Moving old processing stuff to a new namespace
Also fixing a bunch of typos through the entire codebase.

* Rustfmt
2024-03-04 18:57:35 +00:00

35 lines
968 B
Rust

use crate::api::notifications::Notification;
use std::sync::{atomic::AtomicU32, Arc};
use tokio::sync::broadcast;
#[derive(Clone)]
pub struct Notifications(
// Keep this private and use `Node::emit_notification` or `Library::emit_notification` instead.
broadcast::Sender<Notification>,
// Counter for `NotificationId::Node(_)`. NotificationId::Library(_, _)` is autogenerated by the DB.
Arc<AtomicU32>,
);
impl Notifications {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let (tx, _) = broadcast::channel(30);
Self(tx, Arc::new(AtomicU32::new(0)))
}
pub fn subscribe(&self) -> broadcast::Receiver<Notification> {
self.0.subscribe()
}
/// DO NOT USE THIS. Use `Node::emit_notification` or `Library::emit_notification` instead.
pub fn _internal_send(&self, notification: Notification) {
self.0.send(notification).ok();
}
pub fn _internal_next_id(&self) -> u32 {
self.1.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}
}