2023-07-05 20:57:09 +08:00
|
|
|
use std::sync::RwLock;
|
2021-07-21 15:43:05 +08:00
|
|
|
|
2024-05-02 11:42:33 +08:00
|
|
|
use crate::entities::SubscribeObject;
|
2023-01-26 15:40:23 +08:00
|
|
|
use lazy_static::lazy_static;
|
2024-05-02 11:42:33 +08:00
|
|
|
mod builder;
|
|
|
|
pub use builder::*;
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2024-05-02 11:42:33 +08:00
|
|
|
mod debounce;
|
|
|
|
pub use debounce::*;
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
pub mod entities;
|
|
|
|
mod protobuf;
|
2021-09-07 23:30:43 +08:00
|
|
|
|
2023-01-26 15:40:23 +08:00
|
|
|
lazy_static! {
|
2023-02-13 09:29:49 +08:00
|
|
|
static ref NOTIFICATION_SENDER: RwLock<Vec<Box<dyn NotificationSender>>> = RwLock::new(vec![]);
|
2023-01-26 15:40:23 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 10:03:22 +07:00
|
|
|
/// Register a notification sender. The sender will be alive until the process exits.
|
|
|
|
/// Flutter integration test or Tauri hot reload might cause register multiple times.
|
|
|
|
/// So before register a new sender, you might need to unregister the old one. Currently,
|
|
|
|
/// Just remove all senders by calling `unregister_all_notification_sender`.
|
2023-01-26 15:40:23 +08:00
|
|
|
pub fn register_notification_sender<T: NotificationSender>(sender: T) {
|
2023-02-13 09:29:49 +08:00
|
|
|
let box_sender = Box::new(sender);
|
|
|
|
match NOTIFICATION_SENDER.write() {
|
2023-07-05 20:57:09 +08:00
|
|
|
Ok(mut write_guard) => write_guard.push(box_sender),
|
2023-02-13 09:29:49 +08:00
|
|
|
Err(err) => tracing::error!("Failed to push notification sender: {:?}", err),
|
|
|
|
}
|
2023-01-26 15:40:23 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 10:03:22 +07:00
|
|
|
pub fn unregister_all_notification_sender() {
|
|
|
|
match NOTIFICATION_SENDER.write() {
|
|
|
|
Ok(mut write_guard) => write_guard.clear(),
|
|
|
|
Err(err) => tracing::error!("Failed to remove all notification senders: {:?}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:23 +08:00
|
|
|
pub trait NotificationSender: Send + Sync + 'static {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String>;
|
2023-01-26 15:40:23 +08:00
|
|
|
}
|