浏览代码

rename some struct

appflowy 3 年之前
父节点
当前提交
d867093bf5

+ 1 - 1
rust-lib/dart-ffi/src/lib.rs

@@ -18,7 +18,7 @@ pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
     let bytes = unsafe { std::slice::from_raw_parts(input, len) }.to_vec();
     let request = EventRequest::from_data(bytes);
 
-    let stream_data = StreamData::new(port, Some(request)).with_callback(Box::new(|_config, response| {
+    let stream_data = CommandData::new(port, Some(request)).with_callback(Box::new(|_config, response| {
         log::info!("async resp: {:?}", response);
     }));
 

+ 9 - 9
rust-lib/flowy-sdk/src/lib.rs

@@ -19,14 +19,14 @@ pub fn init_system<F>(modules: Vec<Module>) {
     FlowySystem::construct(
         || modules,
         |module_map| {
-            let mut stream = CommandStream::<i64>::new(module_map.clone());
-            let stream_fut = CommandStreamFuture::new(module_map, stream.take_data_rx());
+            let mut stream = CommandSender::<i64>::new(module_map.clone());
+            let runner = CommandSenderRunner::new(module_map, stream.take_data_rx());
 
-            STREAM_SENDER.with(|cell| {
+            CMD_SENDER.with(|cell| {
                 *cell.borrow_mut() = Some(stream);
             });
 
-            stream_fut
+            runner
         },
     )
     .run()
@@ -34,18 +34,18 @@ pub fn init_system<F>(modules: Vec<Module>) {
 }
 
 thread_local!(
-    static STREAM_SENDER: RefCell<Option<CommandStream<i64>>> = RefCell::new(None);
+    static CMD_SENDER: RefCell<Option<CommandSender<i64>>> = RefCell::new(None);
 );
 
-pub fn sync_send(data: StreamData<i64>) -> EventResponse {
-    STREAM_SENDER.with(|cell| match &*cell.borrow() {
+pub fn sync_send(data: CommandData<i64>) -> EventResponse {
+    CMD_SENDER.with(|cell| match &*cell.borrow() {
         Some(stream) => stream.sync_send(data),
         None => panic!(""),
     })
 }
 
-pub fn async_send(data: StreamData<i64>) {
-    STREAM_SENDER.with(|cell| match &*cell.borrow() {
+pub fn async_send(data: CommandData<i64>) {
+    CMD_SENDER.with(|cell| match &*cell.borrow() {
         Some(stream) => {
             stream.async_send(data);
         },

+ 60 - 60
rust-lib/flowy-sys/src/stream.rs

@@ -15,27 +15,66 @@ use tokio::{
 macro_rules! service_factor_impl {
     ($name:ident) => {
         #[allow(non_snake_case, missing_docs)]
-        impl<T> ServiceFactory<StreamData<T>> for $name<T>
+        impl<T> ServiceFactory<CommandData<T>> for $name<T>
         where
             T: 'static,
         {
             type Response = EventResponse;
             type Error = SystemError;
-            type Service = BoxService<StreamData<T>, Self::Response, Self::Error>;
+            type Service = BoxService<CommandData<T>, Self::Response, Self::Error>;
             type Config = ();
             type Future = LocalBoxFuture<'static, Result<Self::Service, Self::Error>>;
 
             fn new_service(&self, _cfg: Self::Config) -> Self::Future {
                 let module_map = self.module_map.clone();
-                let service = Box::new(CommandStreamService { module_map });
+                let service = Box::new(CommandSenderService { module_map });
                 Box::pin(async move { Ok(service as Self::Service) })
             }
         }
     };
 }
 
+struct CommandSenderService {
+    module_map: ModuleMap,
+}
+
+impl<T: 'static> Service<CommandData<T>> for CommandSenderService {
+    type Response = EventResponse;
+    type Error = SystemError;
+    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
+
+    fn call(&self, mut data: CommandData<T>) -> Self::Future {
+        let module_map = self.module_map.clone();
+        let request = data.request.take().unwrap();
+        let fut = async move {
+            let result = {
+                match module_map.get(request.get_event()) {
+                    Some(module) => {
+                        let config = request.get_id().to_owned();
+                        let fut = module.new_service(config);
+                        let service_fut = fut.await?.call(request);
+                        service_fut.await
+                    },
+                    None => {
+                        let msg = format!("Can not find the module to handle the request:{:?}", request);
+                        Err(InternalError::new(msg).into())
+                    },
+                }
+            };
+
+            let response = result.unwrap_or_else(|e| e.into());
+            if let Some(callback) = data.callback {
+                callback(data.config, response.clone());
+            }
+
+            Ok(response)
+        };
+        Box::pin(fut)
+    }
+}
+
 pub type BoxStreamCallback<T> = Box<dyn FnOnce(T, EventResponse) + 'static + Send + Sync>;
-pub struct StreamData<T>
+pub struct CommandData<T>
 where
     T: 'static,
 {
@@ -44,7 +83,7 @@ where
     callback: Option<BoxStreamCallback<T>>,
 }
 
-impl<T> StreamData<T> {
+impl<T> CommandData<T> {
     pub fn new(config: T, request: Option<EventRequest>) -> Self {
         Self {
             config,
@@ -59,20 +98,20 @@ impl<T> StreamData<T> {
     }
 }
 
-pub struct CommandStream<T>
+pub struct CommandSender<T>
 where
     T: 'static,
 {
     module_map: ModuleMap,
-    data_tx: UnboundedSender<StreamData<T>>,
-    data_rx: Option<UnboundedReceiver<StreamData<T>>>,
+    data_tx: UnboundedSender<CommandData<T>>,
+    data_rx: Option<UnboundedReceiver<CommandData<T>>>,
 }
 
-service_factor_impl!(CommandStream);
+service_factor_impl!(CommandSender);
 
-impl<T> CommandStream<T> {
+impl<T> CommandSender<T> {
     pub fn new(module_map: ModuleMap) -> Self {
-        let (data_tx, data_rx) = unbounded_channel::<StreamData<T>>();
+        let (data_tx, data_rx) = unbounded_channel::<CommandData<T>>();
         Self {
             module_map,
             data_tx,
@@ -80,9 +119,9 @@ impl<T> CommandStream<T> {
         }
     }
 
-    pub fn async_send(&self, data: StreamData<T>) { let _ = self.data_tx.send(data); }
+    pub fn async_send(&self, data: CommandData<T>) { let _ = self.data_tx.send(data); }
 
-    pub fn sync_send(&self, data: StreamData<T>) -> EventResponse {
+    pub fn sync_send(&self, data: CommandData<T>) -> EventResponse {
         let factory = self.new_service(());
 
         futures::executor::block_on(async {
@@ -91,25 +130,25 @@ impl<T> CommandStream<T> {
         })
     }
 
-    pub fn tx(&self) -> UnboundedSender<StreamData<T>> { self.data_tx.clone() }
+    pub fn tx(&self) -> UnboundedSender<CommandData<T>> { self.data_tx.clone() }
 
-    pub fn take_data_rx(&mut self) -> UnboundedReceiver<StreamData<T>> { self.data_rx.take().unwrap() }
+    pub fn take_data_rx(&mut self) -> UnboundedReceiver<CommandData<T>> { self.data_rx.take().unwrap() }
 }
 
-pub struct CommandStreamFuture<T: 'static> {
+pub struct CommandSenderRunner<T: 'static> {
     module_map: ModuleMap,
-    data_rx: UnboundedReceiver<StreamData<T>>,
+    data_rx: UnboundedReceiver<CommandData<T>>,
 }
 
-service_factor_impl!(CommandStreamFuture);
+service_factor_impl!(CommandSenderRunner);
 
-impl<T: 'static> CommandStreamFuture<T> {
-    pub fn new(module_map: ModuleMap, data_rx: UnboundedReceiver<StreamData<T>>) -> Self {
+impl<T: 'static> CommandSenderRunner<T> {
+    pub fn new(module_map: ModuleMap, data_rx: UnboundedReceiver<CommandData<T>>) -> Self {
         Self { module_map, data_rx }
     }
 }
 
-impl<T> Future for CommandStreamFuture<T>
+impl<T> Future for CommandSenderRunner<T>
 where
     T: 'static,
 {
@@ -129,42 +168,3 @@ where
         }
     }
 }
-
-pub struct CommandStreamService {
-    module_map: ModuleMap,
-}
-
-impl<T: 'static> Service<StreamData<T>> for CommandStreamService {
-    type Response = EventResponse;
-    type Error = SystemError;
-    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
-
-    fn call(&self, mut data: StreamData<T>) -> Self::Future {
-        let module_map = self.module_map.clone();
-        let request = data.request.take().unwrap();
-        let fut = async move {
-            let result = {
-                match module_map.get(request.get_event()) {
-                    Some(module) => {
-                        let config = request.get_id().to_owned();
-                        let fut = module.new_service(config);
-                        let service_fut = fut.await?.call(request);
-                        service_fut.await
-                    },
-                    None => {
-                        let msg = format!("Can not find the module to handle the request:{:?}", request);
-                        Err(InternalError::new(msg).into())
-                    },
-                }
-            };
-
-            let response = result.unwrap_or_else(|e| e.into());
-            if let Some(callback) = data.callback {
-                callback(data.config, response.clone());
-            }
-
-            Ok(response)
-        };
-        Box::pin(fut)
-    }
-}

+ 5 - 5
rust-lib/flowy-sys/src/system.rs

@@ -1,7 +1,7 @@
 use crate::{
     module::{Event, Module},
     rt::Runtime,
-    stream::CommandStreamFuture,
+    stream::CommandSenderRunner,
 };
 use futures_core::{ready, task::Context};
 use std::{cell::RefCell, collections::HashMap, future::Future, io, rc::Rc, sync::Arc};
@@ -28,10 +28,10 @@ pub struct FlowySystem {
 }
 
 impl FlowySystem {
-    pub fn construct<F, S, T>(module_factory: F, stream_factory: S) -> SystemRunner
+    pub fn construct<F, S, T>(module_factory: F, sender_factory: S) -> SystemRunner
     where
         F: FnOnce() -> Vec<Module>,
-        S: FnOnce(ModuleMap) -> CommandStreamFuture<T>,
+        S: FnOnce(ModuleMap) -> CommandSenderRunner<T>,
         T: 'static,
     {
         let runtime = Runtime::new().unwrap();
@@ -54,8 +54,8 @@ impl FlowySystem {
         });
 
         let system = Self { sys_cmd_tx };
-        let stream_fut = stream_factory(Rc::new(module_map));
-        runtime.spawn(stream_fut);
+        let sender_runner = sender_factory(Rc::new(module_map));
+        runtime.spawn(sender_runner);
 
         FlowySystem::set_current(system);
         let runner = SystemRunner { rt: runtime, stop_rx };

+ 12 - 12
rust-lib/flowy-sys/tests/api/helper.rs

@@ -1,4 +1,4 @@
-use flowy_sys::prelude::{CommandStream, CommandStreamFuture, EventResponse, FlowySystem, Module, StreamData};
+use flowy_sys::prelude::{CommandData, CommandSender, CommandSenderRunner, EventResponse, FlowySystem, Module};
 use std::{
     cell::RefCell,
     sync::{Once, RwLock},
@@ -21,18 +21,18 @@ pub struct ExecutorAction {
 pub struct FlowySystemExecutor {}
 
 thread_local!(
-    static STREAM_SENDER: RefCell<Option<CommandStream<i64>>> = RefCell::new(None);
+    static CMD_SENDER: RefCell<Option<CommandSender<i64>>> = RefCell::new(None);
 );
 
-pub fn sync_send(data: StreamData<i64>) -> EventResponse {
-    STREAM_SENDER.with(|cell| match &*cell.borrow() {
+pub fn sync_send(data: CommandData<i64>) -> EventResponse {
+    CMD_SENDER.with(|cell| match &*cell.borrow() {
         Some(stream) => stream.sync_send(data),
         None => panic!(""),
     })
 }
 
-pub fn async_send(data: StreamData<i64>) {
-    STREAM_SENDER.with(|cell| match &*cell.borrow() {
+pub fn async_send(data: CommandData<i64>) {
+    CMD_SENDER.with(|cell| match &*cell.borrow() {
         Some(stream) => {
             stream.async_send(data);
         },
@@ -40,8 +40,6 @@ pub fn async_send(data: StreamData<i64>) {
     });
 }
 
-pub fn stop_system() { FlowySystem::current().stop(); }
-
 pub fn init_system<F>(modules: Vec<Module>, f: F)
 where
     F: FnOnce() + 'static,
@@ -49,17 +47,19 @@ where
     FlowySystem::construct(
         || modules,
         |module_map| {
-            let mut stream = CommandStream::<i64>::new(module_map.clone());
-            let stream_fut = CommandStreamFuture::new(module_map, stream.take_data_rx());
+            let mut stream = CommandSender::<i64>::new(module_map.clone());
+            let runner = CommandSenderRunner::new(module_map, stream.take_data_rx());
 
-            STREAM_SENDER.with(|cell| {
+            CMD_SENDER.with(|cell| {
                 *cell.borrow_mut() = Some(stream);
             });
 
-            stream_fut
+            runner
         },
     )
     .spawn(async { f() })
     .run()
     .unwrap();
 }
+
+pub fn stop_system() { FlowySystem::current().stop(); }

+ 1 - 1
rust-lib/flowy-sys/tests/api/module_event.rs

@@ -20,7 +20,7 @@ fn test_init() {
 
     init_system(modules, || {
         let request = EventRequest::new(no_params_command);
-        let stream_data = StreamData::new(1, Some(request)).with_callback(Box::new(|_config, response| {
+        let stream_data = CommandData::new(1, Some(request)).with_callback(Box::new(|_config, response| {
             log::info!("async resp: {:?}", response);
         }));