helper.rs 1.4 KB

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