helper.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use flowy_sys::prelude::{CommandData, CommandSender, CommandSenderRunner, EventResponse, FlowySystem, Module};
  2. use std::{
  3. cell::RefCell,
  4. sync::{Once, RwLock},
  5. task::Context,
  6. };
  7. #[allow(dead_code)]
  8. pub fn setup_env() {
  9. static INIT: Once = Once::new();
  10. INIT.call_once(|| {
  11. std::env::set_var("RUST_LOG", "flowy_sys=debug,debug");
  12. env_logger::init();
  13. });
  14. }
  15. pub struct ExecutorAction {
  16. command: String,
  17. }
  18. pub struct FlowySystemExecutor {}
  19. thread_local!(
  20. static CMD_SENDER: RefCell<Option<CommandSender<i64>>> = RefCell::new(None);
  21. );
  22. pub fn sync_send(data: CommandData<i64>) -> EventResponse {
  23. CMD_SENDER.with(|cell| match &*cell.borrow() {
  24. Some(stream) => stream.sync_send(data),
  25. None => panic!(""),
  26. })
  27. }
  28. pub fn async_send(data: CommandData<i64>) {
  29. CMD_SENDER.with(|cell| match &*cell.borrow() {
  30. Some(stream) => {
  31. stream.async_send(data);
  32. },
  33. None => panic!(""),
  34. });
  35. }
  36. pub fn init_system<F>(modules: Vec<Module>, f: F)
  37. where
  38. F: FnOnce() + 'static,
  39. {
  40. FlowySystem::construct(
  41. || modules,
  42. |module_map| {
  43. let mut stream = CommandSender::<i64>::new(module_map.clone());
  44. let runner = CommandSenderRunner::new(module_map, stream.take_data_rx());
  45. CMD_SENDER.with(|cell| {
  46. *cell.borrow_mut() = Some(stream);
  47. });
  48. runner
  49. },
  50. )
  51. .spawn(async { f() })
  52. .run()
  53. .unwrap();
  54. }
  55. pub fn stop_system() { FlowySystem::current().stop(); }