diff --git a/frontend/appflowy_tauri/src-tauri/Cargo.lock b/frontend/appflowy_tauri/src-tauri/Cargo.lock index 1a0257e4e2..d1bbc4c745 100644 --- a/frontend/appflowy_tauri/src-tauri/Cargo.lock +++ b/frontend/appflowy_tauri/src-tauri/Cargo.lock @@ -6572,9 +6572,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -6582,9 +6582,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" dependencies = [ "bumpalo", "log", @@ -6609,9 +6609,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -6619,9 +6619,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", @@ -6632,9 +6632,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "wasm-streams" diff --git a/frontend/rust-lib/.gitignore b/frontend/rust-lib/.gitignore index 9e0f18dea7..b49040c898 100644 --- a/frontend/rust-lib/.gitignore +++ b/frontend/rust-lib/.gitignore @@ -18,3 +18,4 @@ AppFlowy-Collab/ .env.** **/unit_test** jniLibs/ +**/pkg/ \ No newline at end of file diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lock index e8fb3cf902..f9ca104888 100644 --- a/frontend/rust-lib/Cargo.lock +++ b/frontend/rust-lib/Cargo.lock @@ -2247,8 +2247,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -2767,6 +2769,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", + "getrandom 0.2.10", "nanoid", "parking_lot", "pin-project", @@ -2778,6 +2781,7 @@ dependencies = [ "tokio", "tracing", "validator", + "wasm-bindgen", ] [[package]] @@ -5669,9 +5673,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -5679,9 +5683,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" dependencies = [ "bumpalo", "log", @@ -5706,9 +5710,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5716,9 +5720,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", @@ -5729,9 +5733,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "wasm-streams" diff --git a/frontend/rust-lib/event-integration/Cargo.toml b/frontend/rust-lib/event-integration/Cargo.toml index e32725ee66..1db088dc76 100644 --- a/frontend/rust-lib/event-integration/Cargo.toml +++ b/frontend/rust-lib/event-integration/Cargo.toml @@ -55,4 +55,4 @@ zip = "0.6.6" default = ["supabase_cloud_test"] dart = ["flowy-core/dart"] supabase_cloud_test = [] -single_thread = ["flowy-core/single_thread"] \ No newline at end of file +single_thread = ["flowy-core/wasm_build"] \ No newline at end of file diff --git a/frontend/rust-lib/flowy-core/Cargo.toml b/frontend/rust-lib/flowy-core/Cargo.toml index c9000d222b..01befb09c5 100644 --- a/frontend/rust-lib/flowy-core/Cargo.toml +++ b/frontend/rust-lib/flowy-core/Cargo.toml @@ -73,4 +73,4 @@ ts = [ ] rev-sqlite = ["flowy-user/rev-sqlite"] openssl_vendored = ["flowy-sqlite/openssl_vendored"] -single_thread = ["lib-dispatch/single_thread"] +wasm_build = ["lib-dispatch/wasm_build"] diff --git a/frontend/rust-lib/flowy-core/src/lib.rs b/frontend/rust-lib/flowy-core/src/lib.rs index f8e2c88ed6..e83b65043b 100644 --- a/frontend/rust-lib/flowy-core/src/lib.rs +++ b/frontend/rust-lib/flowy-core/src/lib.rs @@ -54,13 +54,13 @@ pub struct AppFlowyCore { } impl AppFlowyCore { - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] pub async fn new(config: AppFlowyCoreConfig) -> Self { let runtime = Arc::new(AFPluginRuntime::new().unwrap()); Self::init(config, runtime).await } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] pub fn new(config: AppFlowyCoreConfig) -> Self { let runtime = Arc::new(AFPluginRuntime::new().unwrap()); let cloned_runtime = runtime.clone(); @@ -190,14 +190,15 @@ impl AppFlowyCore { error!("Init user failed: {}", err) } } - let event_dispatcher = Arc::new(AFPluginDispatcher::construct(runtime, || { + let event_dispatcher = Arc::new(AFPluginDispatcher::new( + runtime, make_plugins( Arc::downgrade(&folder_manager), Arc::downgrade(&database_manager), Arc::downgrade(&user_manager), Arc::downgrade(&document_manager), - ) - })); + ), + )); Self { config, diff --git a/frontend/rust-lib/lib-dispatch/Cargo.toml b/frontend/rust-lib/lib-dispatch/Cargo.toml index b8ce73de80..336d1f6234 100644 --- a/frontend/rust-lib/lib-dispatch/Cargo.toml +++ b/frontend/rust-lib/lib-dispatch/Cargo.toml @@ -12,9 +12,9 @@ futures-channel = "0.3.26" futures.workspace = true futures-util = "0.3.26" bytes = {version = "1.4", features = ["serde"]} -tokio = { workspace = true, features = ["full"] } +tokio = { workspace = true, features = ["rt", "sync"] } nanoid = "0.4.0" -thread-id = "3.3.0" + dyn-clone = "1.0" derivative = "2.2.0" serde_json = { workspace = true, optional = true } @@ -23,17 +23,24 @@ serde_repr = { workspace = true, optional = true } validator = "0.16.1" tracing.workspace = true parking_lot = "0.12" - -#optional crate bincode = { version = "1.3", optional = true} protobuf = { workspace = true, optional = true } +getrandom = { version = "0.2", optional = true } +wasm-bindgen = { version = "0.2.89", optional = true } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +thread-id = "3.3.0" [dev-dependencies] -tokio = { workspace = true, features = ["full"] } +tokio = { workspace = true, features = ["rt"] } futures-util = "0.3.26" [features] -default = ["use_protobuf", ] +default = ["use_protobuf"] use_serde = ["bincode", "serde_json", "serde", "serde_repr"] use_protobuf= ["protobuf"] -single_thread = [] +wasm_build = ["getrandom/js", "wasm-bindgen"] + + +[lib] +crate-type = ["cdylib", "rlib"] diff --git a/frontend/rust-lib/lib-dispatch/src/dispatcher.rs b/frontend/rust-lib/lib-dispatch/src/dispatcher.rs index a7a05c7b92..69e797229a 100644 --- a/frontend/rust-lib/lib-dispatch/src/dispatcher.rs +++ b/frontend/rust-lib/lib-dispatch/src/dispatcher.rs @@ -16,51 +16,51 @@ use crate::{ service::{AFPluginServiceFactory, Service}, }; -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub trait AFConcurrent {} -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] impl AFConcurrent for T where T: ?Sized {} -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub trait AFConcurrent: Send + Sync {} -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] impl AFConcurrent for T where T: Send + Sync {} -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub type AFBoxFuture<'a, T> = futures_core::future::LocalBoxFuture<'a, T>; -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub type AFBoxFuture<'a, T> = futures_core::future::BoxFuture<'a, T>; pub type AFStateMap = std::sync::Arc; -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub(crate) fn downcast_owned(boxed: AFBox) -> Option { boxed.downcast().ok().map(|boxed| *boxed) } -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub(crate) fn downcast_owned(boxed: AFBox) -> Option { boxed.downcast().ok().map(|boxed| *boxed) } -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub(crate) type AFBox = Box; -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub(crate) type AFBox = Box; -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub type BoxFutureCallback = Box AFBoxFuture<'static, ()> + 'static>; -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub type BoxFutureCallback = Box AFBoxFuture<'static, ()> + Send + Sync + 'static>; -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub fn af_spawn(future: T) -> tokio::task::JoinHandle where T: Future + Send + 'static, @@ -69,7 +69,7 @@ where tokio::spawn(future) } -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub fn af_spawn(future: T) -> tokio::task::JoinHandle where T: Future + Send + 'static, @@ -84,11 +84,7 @@ pub struct AFPluginDispatcher { } impl AFPluginDispatcher { - pub fn construct(runtime: Arc, module_factory: F) -> AFPluginDispatcher - where - F: FnOnce() -> Vec, - { - let plugins = module_factory(); + pub fn new(runtime: Arc, plugins: Vec) -> AFPluginDispatcher { tracing::trace!("{}", plugin_info(&plugins)); AFPluginDispatcher { plugins: as_plugin_map(plugins), @@ -206,7 +202,7 @@ impl AFPluginDispatcher { } } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] pub fn sync_send( dispatch: Arc, request: AFPluginRequest, @@ -218,7 +214,7 @@ impl AFPluginDispatcher { )) } - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] #[track_caller] pub fn spawn(&self, future: F) -> tokio::task::JoinHandle where @@ -227,7 +223,7 @@ impl AFPluginDispatcher { self.runtime.spawn(future) } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] #[track_caller] pub fn spawn(&self, future: F) -> tokio::task::JoinHandle where @@ -237,7 +233,7 @@ impl AFPluginDispatcher { self.runtime.spawn(future) } - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] pub async fn run_until(&self, future: F) -> F::Output where F: Future + 'static, @@ -246,7 +242,7 @@ impl AFPluginDispatcher { self.runtime.run_until(handle).await.unwrap() } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] pub async fn run_until<'a, F>(&self, future: F) -> F::Output where F: Future + Send + 'a, diff --git a/frontend/rust-lib/lib-dispatch/src/runtime.rs b/frontend/rust-lib/lib-dispatch/src/runtime.rs index 691c862250..b3f28a164c 100644 --- a/frontend/rust-lib/lib-dispatch/src/runtime.rs +++ b/frontend/rust-lib/lib-dispatch/src/runtime.rs @@ -8,7 +8,7 @@ use tokio::task::JoinHandle; pub struct AFPluginRuntime { inner: Runtime, - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] local: tokio::task::LocalSet, } @@ -27,12 +27,12 @@ impl AFPluginRuntime { let inner = default_tokio_runtime()?; Ok(Self { inner, - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] local: tokio::task::LocalSet::new(), }) } - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] #[track_caller] pub fn spawn(&self, future: F) -> JoinHandle where @@ -41,7 +41,7 @@ impl AFPluginRuntime { self.local.spawn_local(future) } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] #[track_caller] pub fn spawn(&self, future: F) -> JoinHandle where @@ -51,7 +51,7 @@ impl AFPluginRuntime { self.inner.spawn(future) } - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] pub async fn run_until(&self, future: F) -> F::Output where F: Future, @@ -59,7 +59,7 @@ impl AFPluginRuntime { self.local.run_until(future).await } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] pub async fn run_until(&self, future: F) -> F::Output where F: Future, @@ -67,7 +67,7 @@ impl AFPluginRuntime { future.await } - #[cfg(feature = "single_thread")] + #[cfg(feature = "wasm_build")] #[track_caller] pub fn block_on(&self, f: F) -> F::Output where @@ -76,7 +76,7 @@ impl AFPluginRuntime { self.local.block_on(&self.inner, f) } - #[cfg(not(feature = "single_thread"))] + #[cfg(not(feature = "wasm_build"))] #[track_caller] pub fn block_on(&self, f: F) -> F::Output where @@ -86,16 +86,14 @@ impl AFPluginRuntime { } } -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub fn default_tokio_runtime() -> io::Result { runtime::Builder::new_current_thread() .thread_name("dispatch-rt-st") - .enable_io() - .enable_time() .build() } -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub fn default_tokio_runtime() -> io::Result { runtime::Builder::new_multi_thread() .thread_name("dispatch-rt-mt") diff --git a/frontend/rust-lib/lib-dispatch/src/service/boxed.rs b/frontend/rust-lib/lib-dispatch/src/service/boxed.rs index 76780e1d30..ab69de3c0d 100644 --- a/frontend/rust-lib/lib-dispatch/src/service/boxed.rs +++ b/frontend/rust-lib/lib-dispatch/src/service/boxed.rs @@ -16,7 +16,7 @@ where BoxServiceFactory(Box::new(FactoryWrapper(factory))) } -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] type Inner = Box< dyn AFPluginServiceFactory< Req, @@ -27,7 +27,7 @@ type Inner = Box< Future = AFBoxFuture<'static, Result, Err>>, >, >; -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] type Inner = Box< dyn AFPluginServiceFactory< Req, @@ -58,12 +58,12 @@ where } } -#[cfg(feature = "single_thread")] +#[cfg(feature = "wasm_build")] pub type BoxService = Box< dyn Service>>, >; -#[cfg(not(feature = "single_thread"))] +#[cfg(not(feature = "wasm_build"))] pub type BoxService = Box< dyn Service>> + Sync diff --git a/frontend/rust-lib/lib-dispatch/tests/api/module.rs b/frontend/rust-lib/lib-dispatch/tests/api/module.rs index 4e105a8257..8e2a675cdf 100644 --- a/frontend/rust-lib/lib-dispatch/tests/api/module.rs +++ b/frontend/rust-lib/lib-dispatch/tests/api/module.rs @@ -11,9 +11,10 @@ pub async fn hello() -> String { async fn test() { let event = "1"; let runtime = Arc::new(AFPluginRuntime::new().unwrap()); - let dispatch = Arc::new(AFPluginDispatcher::construct(runtime, || { - vec![AFPlugin::new().event(event, hello)] - })); + let dispatch = Arc::new(AFPluginDispatcher::new( + runtime, + vec![AFPlugin::new().event(event, hello)], + )); let request = AFPluginRequest::new(event); let _ = AFPluginDispatcher::async_send_with_callback(dispatch.clone(), request, |resp| { Box::pin(async move {