2021-09-07 15:30:43 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
|
2021-07-21 07:43:05 +00:00
|
|
|
pub mod dart;
|
|
|
|
pub mod entities;
|
|
|
|
mod protobuf;
|
|
|
|
|
2021-09-07 15:30:43 +00:00
|
|
|
use crate::{dart::RustStreamSender, entities::ObservableSubject};
|
|
|
|
use flowy_dispatch::prelude::ToBytes;
|
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
pub struct NotifyBuilder {
|
2021-09-07 15:30:43 +00:00
|
|
|
id: String,
|
|
|
|
payload: Option<Bytes>,
|
|
|
|
error: Option<Bytes>,
|
2021-09-08 05:50:20 +00:00
|
|
|
source: String,
|
2021-09-07 15:30:43 +00:00
|
|
|
ty: i32,
|
|
|
|
}
|
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
impl NotifyBuilder {
|
2021-09-08 05:50:20 +00:00
|
|
|
pub fn new<T: Into<i32>>(id: &str, ty: T, source: &str) -> Self {
|
2021-09-07 15:30:43 +00:00
|
|
|
Self {
|
|
|
|
id: id.to_owned(),
|
|
|
|
ty: ty.into(),
|
|
|
|
payload: None,
|
|
|
|
error: None,
|
2021-09-08 05:50:20 +00:00
|
|
|
source: source.to_owned(),
|
2021-09-07 15:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn payload<T>(mut self, payload: T) -> Self
|
|
|
|
where
|
|
|
|
T: ToBytes,
|
|
|
|
{
|
|
|
|
match payload.into_bytes() {
|
|
|
|
Ok(bytes) => self.payload = Some(bytes),
|
|
|
|
Err(e) => {
|
|
|
|
log::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) => {
|
|
|
|
log::error!("Set observable error failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
pub fn send(self) {
|
2021-09-07 15:30:43 +00:00
|
|
|
let payload = match self.payload {
|
|
|
|
None => None,
|
|
|
|
Some(bytes) => Some(bytes.to_vec()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let error = match self.error {
|
|
|
|
None => None,
|
|
|
|
Some(bytes) => Some(bytes.to_vec()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let subject = ObservableSubject {
|
2021-09-08 05:50:20 +00:00
|
|
|
source: self.source,
|
2021-09-07 15:30:43 +00:00
|
|
|
ty: self.ty,
|
|
|
|
id: self.id,
|
|
|
|
payload,
|
|
|
|
error,
|
|
|
|
};
|
2021-09-08 10:25:32 +00:00
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
log::debug!("Notify {}", subject);
|
2021-09-07 15:30:43 +00:00
|
|
|
match RustStreamSender::post(subject) {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(error) => log::error!("Send observable subject failed: {}", error),
|
|
|
|
}
|
2021-07-21 07:43:05 +00:00
|
|
|
}
|
|
|
|
}
|