mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
fix: module receive command through rx
This commit is contained in:
parent
85e8e4b8af
commit
d2b53fe1f3
@ -37,7 +37,7 @@ pub type CommandServiceFactory = BoxServiceFactory<(), ServiceRequest, ServiceRe
|
|||||||
pub struct Module {
|
pub struct Module {
|
||||||
name: String,
|
name: String,
|
||||||
data: DataContainer,
|
data: DataContainer,
|
||||||
factory_map: HashMap<Command, CommandServiceFactory>,
|
service_map: HashMap<Command, CommandServiceFactory>,
|
||||||
req_tx: UnboundedSender<FlowyRequest>,
|
req_tx: UnboundedSender<FlowyRequest>,
|
||||||
req_rx: UnboundedReceiver<FlowyRequest>,
|
req_rx: UnboundedReceiver<FlowyRequest>,
|
||||||
resp_tx: UnboundedSender<FlowyResponse>,
|
resp_tx: UnboundedSender<FlowyResponse>,
|
||||||
@ -49,7 +49,7 @@ impl Module {
|
|||||||
Self {
|
Self {
|
||||||
name: "".to_owned(),
|
name: "".to_owned(),
|
||||||
data: DataContainer::new(),
|
data: DataContainer::new(),
|
||||||
factory_map: HashMap::new(),
|
service_map: HashMap::new(),
|
||||||
req_tx,
|
req_tx,
|
||||||
req_rx,
|
req_rx,
|
||||||
resp_tx,
|
resp_tx,
|
||||||
@ -74,11 +74,11 @@ impl Module {
|
|||||||
R: Future + 'static,
|
R: Future + 'static,
|
||||||
R::Output: Responder + 'static,
|
R::Output: Responder + 'static,
|
||||||
{
|
{
|
||||||
self.factory_map.insert(command, factory(HandlerService::new(handler)));
|
self.service_map.insert(command, factory(HandlerService::new(handler)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn can_handle(&self, cmd: &Command) -> bool { self.factory_map.contains_key(cmd) }
|
pub fn can_handle(&self, cmd: &Command) -> bool { self.service_map.contains_key(cmd) }
|
||||||
|
|
||||||
pub fn req_tx(&self) -> UnboundedSender<FlowyRequest> { self.req_tx.clone() }
|
pub fn req_tx(&self) -> UnboundedSender<FlowyRequest> { self.req_tx.clone() }
|
||||||
|
|
||||||
@ -90,6 +90,13 @@ impl Module {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn service_sender_map(&self) -> HashMap<Command, UnboundedSender<FlowyRequest>> {
|
||||||
|
self.service_map
|
||||||
|
.keys()
|
||||||
|
.map(|key| (key.clone(), self.req_tx()))
|
||||||
|
.collect::<HashMap<_, _>>()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for Module {
|
impl Future for Module {
|
||||||
@ -98,7 +105,7 @@ impl Future for Module {
|
|||||||
loop {
|
loop {
|
||||||
match ready!(Pin::new(&mut self.req_rx).poll_recv(cx)) {
|
match ready!(Pin::new(&mut self.req_rx).poll_recv(cx)) {
|
||||||
None => return Poll::Ready(()),
|
None => return Poll::Ready(()),
|
||||||
Some(request) => match self.factory_map.get(request.get_cmd()) {
|
Some(request) => match self.service_map.get(request.get_cmd()) {
|
||||||
Some(factory) => {
|
Some(factory) => {
|
||||||
let fut = ModuleServiceFuture {
|
let fut = ModuleServiceFuture {
|
||||||
request,
|
request,
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
module::{Command, Module},
|
module::{Command, CommandServiceFactory, Module},
|
||||||
request::FlowyRequest,
|
request::FlowyRequest,
|
||||||
response::FlowyResponse,
|
response::FlowyResponse,
|
||||||
rt::Runtime,
|
rt::Runtime,
|
||||||
|
service::BoxServiceFactory,
|
||||||
};
|
};
|
||||||
use futures_core::{future::LocalBoxFuture, ready, task::Context};
|
use futures_core::{future::LocalBoxFuture, ready, task::Context};
|
||||||
use futures_util::{future, pin_mut};
|
use futures_util::{future, pin_mut};
|
||||||
use std::{cell::RefCell, future::Future, io, sync::Arc};
|
use std::{cell::RefCell, collections::HashMap, future::Future, io, sync::Arc};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
macros::support::{Pin, Poll},
|
macros::support::{Pin, Poll},
|
||||||
sync::{
|
sync::{
|
||||||
@ -21,7 +22,7 @@ thread_local!(
|
|||||||
|
|
||||||
pub struct FlowySystem {
|
pub struct FlowySystem {
|
||||||
resp_tx: UnboundedSender<FlowyResponse>,
|
resp_tx: UnboundedSender<FlowyResponse>,
|
||||||
modules: Vec<Module>,
|
sender_map: HashMap<Command, UnboundedSender<FlowyRequest>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FlowySystem {
|
impl FlowySystem {
|
||||||
@ -37,31 +38,26 @@ impl FlowySystem {
|
|||||||
|
|
||||||
let mut system = Self {
|
let mut system = Self {
|
||||||
resp_tx: resp_tx.clone(),
|
resp_tx: resp_tx.clone(),
|
||||||
modules: vec![],
|
sender_map: HashMap::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let factory = module_factory(resp_tx.clone());
|
let factory = module_factory(resp_tx.clone());
|
||||||
factory.into_iter().for_each(|m| {
|
factory.into_iter().for_each(|m| {
|
||||||
|
system.sender_map.extend(m.service_sender_map());
|
||||||
runtime.spawn(m);
|
runtime.spawn(m);
|
||||||
// system.add_module(m);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
FlowySystem::set_current(system);
|
FlowySystem::set_current(system);
|
||||||
|
|
||||||
let runner = SystemRunner { rt: runtime, stop_rx };
|
let runner = SystemRunner { rt: runtime, stop_rx };
|
||||||
runner
|
runner
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_command(&self, cmd: Command, request: FlowyRequest) {
|
pub fn handle_command(&self, cmd: Command, request: FlowyRequest) {
|
||||||
self.modules.iter().for_each(|m| {
|
if let Some(sender) = self.sender_map.get(&cmd) {
|
||||||
if m.can_handle(&cmd) {
|
sender.send(request);
|
||||||
m.handle(request.clone());
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_module(&mut self, module: Module) { self.modules.push(module); }
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn set_current(sys: FlowySystem) {
|
pub fn set_current(sys: FlowySystem) {
|
||||||
CURRENT.with(|cell| {
|
CURRENT.with(|cell| {
|
||||||
@ -91,7 +87,7 @@ impl Future for SystemController {
|
|||||||
match ready!(Pin::new(&mut self.resp_rx).poll_recv(cx)) {
|
match ready!(Pin::new(&mut self.resp_rx).poll_recv(cx)) {
|
||||||
None => return Poll::Ready(()),
|
None => return Poll::Ready(()),
|
||||||
Some(resp) => {
|
Some(resp) => {
|
||||||
// FFI
|
// FF
|
||||||
println!("Receive response: {:?}", resp);
|
println!("Receive response: {:?}", resp);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user