helper.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use flowy_sys::prelude::{CommandStream, CommandStreamFuture, EventResponse, FlowySystem, Module, StreamData};
  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 STREAM_SENDER: RefCell<Option<CommandStream<i64>>> = RefCell::new(None);
  21. );
  22. pub fn sync_send(data: StreamData<i64>) -> EventResponse {
  23. STREAM_SENDER.with(|cell| match &*cell.borrow() {
  24. Some(stream) => stream.sync_send(data),
  25. None => panic!(""),
  26. })
  27. }
  28. pub fn async_send(data: StreamData<i64>) {
  29. STREAM_SENDER.with(|cell| match &*cell.borrow() {
  30. Some(stream) => {
  31. stream.async_send(data);
  32. },
  33. None => panic!(""),
  34. });
  35. }
  36. pub fn stop_system() { FlowySystem::current().stop(); }
  37. pub fn init_system<F>(modules: Vec<Module>, f: F)
  38. where
  39. F: FnOnce() + 'static,
  40. {
  41. FlowySystem::construct(
  42. || modules,
  43. |module_map| {
  44. let mut stream = CommandStream::<i64>::new(module_map.clone());
  45. let stream_fut = CommandStreamFuture::new(module_map, stream.take_data_rx());
  46. STREAM_SENDER.with(|cell| {
  47. *cell.borrow_mut() = Some(stream);
  48. });
  49. stream_fut
  50. },
  51. )
  52. .spawn(async { f() })
  53. .run()
  54. .unwrap();
  55. }