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:
Nathan.fooo
2023-01-26 15:40:23 +08:00
committed by GitHub
parent 67f07463f0
commit 347245aaa1
80 changed files with 269 additions and 231 deletions

View 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"]

View File

@ -0,0 +1,2 @@
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities"]

View File

@ -0,0 +1,3 @@
fn main() {
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
}

View File

@ -0,0 +1,3 @@
mod subject;
pub use subject::*;

View 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,
}
}
}

View 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);
}
}
}
}