2021-06-29 03:19:12 +00:00
|
|
|
use flowy_sys::prelude::{CommandData, CommandSender, CommandSenderRunner, EventResponse, FlowySystem, Module};
|
2021-06-28 14:56:15 +00:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
sync::{Once, RwLock},
|
|
|
|
task::Context,
|
|
|
|
};
|
2021-06-27 07:11:41 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn setup_env() {
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2021-06-28 14:56:15 +00:00
|
|
|
std::env::set_var("RUST_LOG", "flowy_sys=debug,debug");
|
2021-06-27 07:11:41 +00:00
|
|
|
env_logger::init();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ExecutorAction {
|
|
|
|
command: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FlowySystemExecutor {}
|
2021-06-28 14:56:15 +00:00
|
|
|
|
|
|
|
thread_local!(
|
2021-06-29 03:19:12 +00:00
|
|
|
static CMD_SENDER: RefCell<Option<CommandSender<i64>>> = RefCell::new(None);
|
2021-06-28 14:56:15 +00:00
|
|
|
);
|
|
|
|
|
2021-06-29 03:19:12 +00:00
|
|
|
pub fn sync_send(data: CommandData<i64>) -> EventResponse {
|
|
|
|
CMD_SENDER.with(|cell| match &*cell.borrow() {
|
2021-06-28 14:56:15 +00:00
|
|
|
Some(stream) => stream.sync_send(data),
|
|
|
|
None => panic!(""),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-29 03:19:12 +00:00
|
|
|
pub fn async_send(data: CommandData<i64>) {
|
|
|
|
CMD_SENDER.with(|cell| match &*cell.borrow() {
|
2021-06-28 14:56:15 +00:00
|
|
|
Some(stream) => {
|
|
|
|
stream.async_send(data);
|
|
|
|
},
|
|
|
|
None => panic!(""),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_system<F>(modules: Vec<Module>, f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce() + 'static,
|
|
|
|
{
|
|
|
|
FlowySystem::construct(
|
|
|
|
|| modules,
|
|
|
|
|module_map| {
|
2021-06-29 03:19:12 +00:00
|
|
|
let mut stream = CommandSender::<i64>::new(module_map.clone());
|
|
|
|
let runner = CommandSenderRunner::new(module_map, stream.take_data_rx());
|
2021-06-28 14:56:15 +00:00
|
|
|
|
2021-06-29 03:19:12 +00:00
|
|
|
CMD_SENDER.with(|cell| {
|
2021-06-28 14:56:15 +00:00
|
|
|
*cell.borrow_mut() = Some(stream);
|
|
|
|
});
|
|
|
|
|
2021-06-29 03:19:12 +00:00
|
|
|
runner
|
2021-06-28 14:56:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.spawn(async { f() })
|
|
|
|
.run()
|
|
|
|
.unwrap();
|
|
|
|
}
|
2021-06-29 03:19:12 +00:00
|
|
|
|
|
|
|
pub fn stop_system() { FlowySystem::current().stop(); }
|