mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Refactor/dart notification (#1740)
* refactor: notification send * refactor: rename dart-notify to flowy-notification * ci: fix clippy wanrings * ci: fix rust code converage
This commit is contained in:
24
frontend/rust-lib/flowy-notification/Cargo.toml
Normal file
24
frontend/rust-lib/flowy-notification/Cargo.toml
Normal file
@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "flowy-notification"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lazy_static = { version = "1.4.0" }
|
||||
protobuf = { version = "2.20.0" }
|
||||
allo-isolate = { version = "^0.1", features = ["catch-unwind"] }
|
||||
tracing = { version = "0.1", features = ["log"] }
|
||||
bytes = { version = "1.0" }
|
||||
serde = "1.0"
|
||||
|
||||
flowy-derive = { path = "../flowy-derive" }
|
||||
lib-dispatch = { path = "../lib-dispatch" }
|
||||
|
||||
[build-dependencies]
|
||||
flowy-codegen = { path = "../flowy-codegen" }
|
||||
|
||||
[features]
|
||||
dart = ["flowy-codegen/dart"]
|
||||
ts = ["flowy-codegen/ts"]
|
2
frontend/rust-lib/flowy-notification/Flowy.toml
Normal file
2
frontend/rust-lib/flowy-notification/Flowy.toml
Normal file
@ -0,0 +1,2 @@
|
||||
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
|
||||
proto_input = ["src/entities"]
|
3
frontend/rust-lib/flowy-notification/build.rs
Normal file
3
frontend/rust-lib/flowy-notification/build.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
|
||||
}
|
3
frontend/rust-lib/flowy-notification/src/entities/mod.rs
Normal file
3
frontend/rust-lib/flowy-notification/src/entities/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod subject;
|
||||
|
||||
pub use subject::*;
|
47
frontend/rust-lib/flowy-notification/src/entities/subject.rs
Normal file
47
frontend/rust-lib/flowy-notification/src/entities/subject.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use flowy_derive::ProtoBuf;
|
||||
use std::{fmt, fmt::Formatter};
|
||||
|
||||
#[derive(Debug, Clone, ProtoBuf, serde::Serialize)]
|
||||
pub struct SubscribeObject {
|
||||
#[pb(index = 1)]
|
||||
pub source: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub ty: i32,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub id: String,
|
||||
|
||||
#[pb(index = 4, one_of)]
|
||||
pub payload: Option<Vec<u8>>,
|
||||
|
||||
#[pb(index = 5, one_of)]
|
||||
pub error: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SubscribeObject {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&format!("{} changed: ", &self.source))?;
|
||||
if let Some(payload) = &self.payload {
|
||||
f.write_str(&format!("send {} payload", payload.len()))?;
|
||||
}
|
||||
|
||||
if let Some(payload) = &self.error {
|
||||
f.write_str(&format!("receive {} error", payload.len()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::default::Default for SubscribeObject {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source: "".to_string(),
|
||||
ty: 0,
|
||||
id: "".to_string(),
|
||||
payload: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
}
|
94
frontend/rust-lib/flowy-notification/src/lib.rs
Normal file
94
frontend/rust-lib/flowy-notification/src/lib.rs
Normal file
@ -0,0 +1,94 @@
|
||||
pub mod entities;
|
||||
mod protobuf;
|
||||
|
||||
use crate::entities::SubscribeObject;
|
||||
use bytes::Bytes;
|
||||
use lazy_static::lazy_static;
|
||||
use lib_dispatch::prelude::ToBytes;
|
||||
use std::sync::RwLock;
|
||||
|
||||
lazy_static! {
|
||||
static ref NOTIFICATION_SENDER: RwLock<Vec<Box<dyn NotificationSender>>> = RwLock::new(vec![]);
|
||||
}
|
||||
|
||||
pub fn register_notification_sender<T: NotificationSender>(sender: T) {
|
||||
let box_sender = Box::new(sender);
|
||||
match NOTIFICATION_SENDER.write() {
|
||||
Ok(mut write_guard) => write_guard.push(box_sender),
|
||||
Err(err) => tracing::error!("Failed to push notification sender: {:?}", err),
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NotificationSender: Send + Sync + 'static {
|
||||
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub struct NotificationBuilder {
|
||||
id: String,
|
||||
payload: Option<Bytes>,
|
||||
error: Option<Bytes>,
|
||||
source: String,
|
||||
ty: i32,
|
||||
}
|
||||
|
||||
impl NotificationBuilder {
|
||||
pub fn new<T: Into<i32>>(id: &str, ty: T, source: &str) -> Self {
|
||||
Self {
|
||||
id: id.to_owned(),
|
||||
ty: ty.into(),
|
||||
payload: None,
|
||||
error: None,
|
||||
source: source.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn payload<T>(mut self, payload: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match payload.into_bytes() {
|
||||
Ok(bytes) => self.payload = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable payload failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn error<T>(mut self, error: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match error.into_bytes() {
|
||||
Ok(bytes) => self.error = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable error failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn send(self) {
|
||||
let payload = self.payload.map(|bytes| bytes.to_vec());
|
||||
let error = self.error.map(|bytes| bytes.to_vec());
|
||||
let subject = SubscribeObject {
|
||||
source: self.source,
|
||||
ty: self.ty,
|
||||
id: self.id,
|
||||
payload,
|
||||
error,
|
||||
};
|
||||
|
||||
match NOTIFICATION_SENDER.read() {
|
||||
Ok(read_guard) => read_guard.iter().for_each(|sender| {
|
||||
if let Err(e) = sender.send_subject(subject.clone()) {
|
||||
tracing::error!("Post notification failed: {}", e);
|
||||
}
|
||||
}),
|
||||
Err(err) => {
|
||||
tracing::error!("Read notification sender failed: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user