AppFlowy/rust-lib/flowy-sdk/src/lib.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

2021-06-30 15:11:27 +00:00
pub mod module;
pub use module::*;
2021-06-28 15:58:43 +00:00
use flowy_sys::prelude::*;
2021-06-30 15:11:27 +00:00
use module::build_modules;
2021-06-28 15:58:43 +00:00
use std::cell::RefCell;
pub struct FlowySDK {}
impl FlowySDK {
pub fn init(path: &str) {
2021-06-30 15:11:27 +00:00
flowy_log::init_log("flowy", "Debug").unwrap();
2021-06-28 15:58:43 +00:00
2021-06-30 15:11:27 +00:00
log::info!("🔥🔥🔥 System start running");
match init_system(build_modules()).run() {
Ok(_) => {},
Err(e) => log::error!("System run fail with error: {:?}", e),
}
}
2021-06-28 15:58:43 +00:00
}
2021-06-30 15:11:27 +00:00
pub fn init_system(modules: Vec<Module>) -> SystemRunner {
2021-06-28 15:58:43 +00:00
FlowySystem::construct(
|| modules,
|module_map, runtime| {
let mut sender = Sender::<i64>::new(module_map.clone());
runtime.spawn(SenderRunner::new(module_map, sender.take_rx()));
2021-06-28 15:58:43 +00:00
SENDER.with(|cell| {
*cell.borrow_mut() = Some(sender);
2021-06-28 15:58:43 +00:00
});
},
)
}
thread_local!(
static SENDER: RefCell<Option<Sender<i64>>> = RefCell::new(None);
2021-06-28 15:58:43 +00:00
);
2021-06-30 15:11:27 +00:00
pub fn sync_send(data: SenderRequest<i64>) -> EventResponse {
SENDER.with(|cell| match &*cell.borrow() {
2021-06-28 15:58:43 +00:00
Some(stream) => stream.sync_send(data),
None => panic!(""),
})
}
2021-06-30 15:11:27 +00:00
pub fn async_send(data: SenderRequest<i64>) {
SENDER.with(|cell| match &*cell.borrow() {
2021-06-28 15:58:43 +00:00
Some(stream) => {
stream.async_send(data);
},
None => panic!(""),
});
}