mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #577 from AppFlowy-IO/refactor/remove_unused_dispatch_crate_code
chore: remove unused code
This commit is contained in:
commit
c8d78f58f5
@ -14,7 +14,7 @@ use flowy_net::{
|
|||||||
use flowy_text_block::TextBlockManager;
|
use flowy_text_block::TextBlockManager;
|
||||||
use flowy_user::services::{notifier::UserStatus, UserSession, UserSessionConfig};
|
use flowy_user::services::{notifier::UserStatus, UserSession, UserSessionConfig};
|
||||||
use lib_dispatch::prelude::*;
|
use lib_dispatch::prelude::*;
|
||||||
use lib_dispatch::util::tokio_default_runtime;
|
use lib_dispatch::runtime::tokio_default_runtime;
|
||||||
use module::mk_modules;
|
use module::mk_modules;
|
||||||
pub use module::*;
|
pub use module::*;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::runtime::FlowyRuntime;
|
||||||
use crate::{
|
use crate::{
|
||||||
errors::{DispatchError, Error, InternalError},
|
errors::{DispatchError, Error, InternalError},
|
||||||
module::{as_module_map, Module, ModuleMap, ModuleRequest},
|
module::{as_module_map, Module, ModuleMap, ModuleRequest},
|
||||||
@ -10,13 +11,14 @@ use futures_util::task::Context;
|
|||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use std::{future::Future, sync::Arc};
|
use std::{future::Future, sync::Arc};
|
||||||
use tokio::macros::support::{Pin, Poll};
|
use tokio::macros::support::{Pin, Poll};
|
||||||
|
|
||||||
pub struct EventDispatcher {
|
pub struct EventDispatcher {
|
||||||
module_map: ModuleMap,
|
module_map: ModuleMap,
|
||||||
runtime: tokio::runtime::Runtime,
|
runtime: FlowyRuntime,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventDispatcher {
|
impl EventDispatcher {
|
||||||
pub fn construct<F>(runtime: tokio::runtime::Runtime, module_factory: F) -> EventDispatcher
|
pub fn construct<F>(runtime: FlowyRuntime, module_factory: F) -> EventDispatcher
|
||||||
where
|
where
|
||||||
F: FnOnce() -> Vec<Module>,
|
F: FnOnce() -> Vec<Module>,
|
||||||
{
|
{
|
||||||
|
@ -8,10 +8,10 @@ pub mod util;
|
|||||||
mod byte_trait;
|
mod byte_trait;
|
||||||
mod data;
|
mod data;
|
||||||
mod dispatcher;
|
mod dispatcher;
|
||||||
mod system;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
|
pub mod runtime;
|
||||||
|
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
|
|
||||||
|
26
frontend/rust-lib/lib-dispatch/src/runtime.rs
Normal file
26
frontend/rust-lib/lib-dispatch/src/runtime.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use std::{io, thread};
|
||||||
|
use tokio::runtime;
|
||||||
|
|
||||||
|
pub type FlowyRuntime = tokio::runtime::Runtime;
|
||||||
|
|
||||||
|
pub fn tokio_default_runtime() -> io::Result<FlowyRuntime> {
|
||||||
|
runtime::Builder::new_multi_thread()
|
||||||
|
.thread_name("dispatch-rt")
|
||||||
|
.enable_io()
|
||||||
|
.enable_time()
|
||||||
|
.on_thread_start(move || {
|
||||||
|
tracing::trace!(
|
||||||
|
"{:?} thread started: thread_id= {}",
|
||||||
|
thread::current(),
|
||||||
|
thread_id::get()
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.on_thread_stop(move || {
|
||||||
|
tracing::trace!(
|
||||||
|
"{:?} thread stopping: thread_id= {}",
|
||||||
|
thread::current(),
|
||||||
|
thread_id::get(),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
}
|
@ -1,165 +0,0 @@
|
|||||||
use crate::module::{as_module_map, Module, ModuleMap};
|
|
||||||
use futures_core::{ready, task::Context};
|
|
||||||
use std::{cell::RefCell, fmt::Debug, future::Future, io, sync::Arc};
|
|
||||||
use tokio::{
|
|
||||||
macros::support::{Pin, Poll},
|
|
||||||
sync::{
|
|
||||||
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
|
||||||
oneshot,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
thread_local!(
|
|
||||||
static CURRENT: RefCell<Option<Arc<FlowySystem>>> = RefCell::new(None);
|
|
||||||
);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub enum SystemCommand {
|
|
||||||
Exit(i8),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FlowySystem {
|
|
||||||
sys_cmd_tx: UnboundedSender<SystemCommand>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FlowySystem {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn construct<F, S>(module_factory: F, sender_factory: S) -> SystemRunner
|
|
||||||
where
|
|
||||||
F: FnOnce() -> Vec<Module>,
|
|
||||||
S: FnOnce(ModuleMap, &Runtime),
|
|
||||||
{
|
|
||||||
let runtime = Arc::new(Runtime::new().unwrap());
|
|
||||||
let (sys_cmd_tx, sys_cmd_rx) = unbounded_channel::<SystemCommand>();
|
|
||||||
let (stop_tx, stop_rx) = oneshot::channel();
|
|
||||||
|
|
||||||
runtime.spawn(SystemController {
|
|
||||||
stop_tx: Some(stop_tx),
|
|
||||||
sys_cmd_rx,
|
|
||||||
});
|
|
||||||
|
|
||||||
let module_map = as_module_map(module_factory());
|
|
||||||
sender_factory(module_map, &runtime);
|
|
||||||
|
|
||||||
let system = Self { sys_cmd_tx };
|
|
||||||
FlowySystem::set_current(system);
|
|
||||||
SystemRunner { rt: runtime, stop_rx }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn stop(&self) {
|
|
||||||
match self.sys_cmd_tx.send(SystemCommand::Exit(0)) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Stop system error: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn set_current(sys: FlowySystem) {
|
|
||||||
CURRENT.with(|cell| {
|
|
||||||
*cell.borrow_mut() = Some(Arc::new(sys));
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn current() -> Arc<FlowySystem> {
|
|
||||||
CURRENT.with(|cell| match *cell.borrow() {
|
|
||||||
Some(ref sys) => sys.clone(),
|
|
||||||
None => panic!("System is not running"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SystemController {
|
|
||||||
stop_tx: Option<oneshot::Sender<i8>>,
|
|
||||||
sys_cmd_rx: UnboundedReceiver<SystemCommand>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for SystemController {
|
|
||||||
type Output = ();
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
loop {
|
|
||||||
match ready!(Pin::new(&mut self.sys_cmd_rx).poll_recv(cx)) {
|
|
||||||
None => return Poll::Ready(()),
|
|
||||||
Some(cmd) => match cmd {
|
|
||||||
SystemCommand::Exit(code) => {
|
|
||||||
if let Some(tx) = self.stop_tx.take() {
|
|
||||||
let _ = tx.send(code);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SystemRunner {
|
|
||||||
rt: Arc<Runtime>,
|
|
||||||
stop_rx: oneshot::Receiver<i8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SystemRunner {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn run(self) -> io::Result<()> {
|
|
||||||
let SystemRunner { rt, stop_rx } = self;
|
|
||||||
match rt.block_on(stop_rx) {
|
|
||||||
Ok(code) => {
|
|
||||||
if code != 0 {
|
|
||||||
Err(io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!("Non-zero exit code: {}", code),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn spawn<F: Future<Output = ()> + 'static>(self, future: F) -> Self {
|
|
||||||
self.rt.spawn(future);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use crate::util::tokio_default_runtime;
|
|
||||||
use tokio::{runtime, task::LocalSet};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Runtime {
|
|
||||||
local: LocalSet,
|
|
||||||
rt: runtime::Runtime,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Runtime {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn new() -> io::Result<Runtime> {
|
|
||||||
let rt = tokio_default_runtime()?;
|
|
||||||
Ok(Runtime {
|
|
||||||
rt,
|
|
||||||
local: LocalSet::new(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn spawn<F>(&self, future: F) -> &Self
|
|
||||||
where
|
|
||||||
F: Future<Output = ()> + 'static,
|
|
||||||
{
|
|
||||||
self.local.spawn_local(future);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn block_on<F>(&self, f: F) -> F::Output
|
|
||||||
where
|
|
||||||
F: Future + 'static,
|
|
||||||
{
|
|
||||||
self.local.block_on(&self.rt, f)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +1 @@
|
|||||||
use std::{io, thread};
|
|
||||||
|
|
||||||
use tokio::runtime;
|
|
||||||
|
|
||||||
pub mod ready;
|
pub mod ready;
|
||||||
|
|
||||||
pub fn tokio_default_runtime() -> io::Result<tokio::runtime::Runtime> {
|
|
||||||
runtime::Builder::new_multi_thread()
|
|
||||||
.thread_name("flowy-rt")
|
|
||||||
.enable_io()
|
|
||||||
.enable_time()
|
|
||||||
.on_thread_start(move || {
|
|
||||||
tracing::trace!(
|
|
||||||
"{:?} thread started: thread_id= {}",
|
|
||||||
thread::current(),
|
|
||||||
thread_id::get()
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.on_thread_stop(move || {
|
|
||||||
tracing::trace!(
|
|
||||||
"{:?} thread stopping: thread_id= {}",
|
|
||||||
thread::current(),
|
|
||||||
thread_id::get(),
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use lib_dispatch::{prelude::*, util::tokio_default_runtime};
|
use lib_dispatch::prelude::*;
|
||||||
|
use lib_dispatch::runtime::tokio_default_runtime;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub async fn hello() -> String {
|
pub async fn hello() -> String {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user