feat: appflowy_web build (#4302)

* chore: create web folder

* fix: support lib-dispatch and notification compiling to wasm

* refactor: wasm

* refactor: call async evnet

* chore: add web ci

* ci: fix

* ci: fix

* ci: fix

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Kilu.He
2024-01-06 11:50:05 +08:00
committed by GitHub
parent 89370b4a55
commit 79c912219d
49 changed files with 4801 additions and 123 deletions

View File

@ -25,12 +25,14 @@ tracing.workspace = true
parking_lot = "0.12"
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"
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"]}
wasm-bindgen = { version = "0.2.89" }
[dev-dependencies]
tokio = { workspace = true, features = ["rt"] }
futures-util = "0.3.26"
@ -39,7 +41,7 @@ futures-util = "0.3.26"
default = ["use_protobuf"]
use_serde = ["bincode", "serde_json", "serde", "serde_repr"]
use_protobuf= ["protobuf"]
wasm_build = ["getrandom/js", "wasm-bindgen"]
single_thread = []
[lib]

View File

@ -16,51 +16,51 @@ use crate::{
service::{AFPluginServiceFactory, Service},
};
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub trait AFConcurrent {}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
impl<T> AFConcurrent for T where T: ?Sized {}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub trait AFConcurrent: Send + Sync {}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
impl<T> AFConcurrent for T where T: Send + Sync {}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub type AFBoxFuture<'a, T> = futures_core::future::LocalBoxFuture<'a, T>;
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub type AFBoxFuture<'a, T> = futures_core::future::BoxFuture<'a, T>;
pub type AFStateMap = std::sync::Arc<AFPluginStateMap>;
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub(crate) fn downcast_owned<T: 'static>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub(crate) fn downcast_owned<T: 'static + Send + Sync>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub(crate) type AFBox = Box<dyn Any>;
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub(crate) type AFBox = Box<dyn Any + Send + Sync>;
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + 'static>;
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + Send + Sync + 'static>;
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + Send + 'static,
@ -69,7 +69,7 @@ where
tokio::spawn(future)
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + Send + 'static,
@ -202,7 +202,7 @@ impl AFPluginDispatcher {
}
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub fn sync_send(
dispatch: Arc<AFPluginDispatcher>,
request: AFPluginRequest,
@ -214,7 +214,7 @@ impl AFPluginDispatcher {
))
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -223,7 +223,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -233,7 +233,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future + 'static,
@ -242,7 +242,7 @@ impl AFPluginDispatcher {
self.runtime.run_until(handle).await.unwrap()
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub async fn run_until<'a, F>(&self, future: F) -> F::Output
where
F: Future + Send + 'a,

View File

@ -8,7 +8,7 @@ use tokio::task::JoinHandle;
pub struct AFPluginRuntime {
inner: Runtime,
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
local: tokio::task::LocalSet,
}
@ -27,12 +27,12 @@ impl AFPluginRuntime {
let inner = default_tokio_runtime()?;
Ok(Self {
inner,
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
local: tokio::task::LocalSet::new(),
})
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
@ -41,7 +41,7 @@ impl AFPluginRuntime {
self.local.spawn_local(future)
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
@ -51,7 +51,7 @@ impl AFPluginRuntime {
self.inner.spawn(future)
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future,
@ -59,7 +59,7 @@ impl AFPluginRuntime {
self.local.run_until(future).await
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future,
@ -67,7 +67,7 @@ impl AFPluginRuntime {
future.await
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
#[track_caller]
pub fn block_on<F>(&self, f: F) -> F::Output
where
@ -76,7 +76,7 @@ impl AFPluginRuntime {
self.local.block_on(&self.inner, f)
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
#[track_caller]
pub fn block_on<F>(&self, f: F) -> F::Output
where
@ -86,14 +86,14 @@ impl AFPluginRuntime {
}
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub fn default_tokio_runtime() -> io::Result<Runtime> {
runtime::Builder::new_current_thread()
.thread_name("dispatch-rt-st")
.build()
}
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub fn default_tokio_runtime() -> io::Result<Runtime> {
runtime::Builder::new_multi_thread()
.thread_name("dispatch-rt-mt")

View File

@ -16,7 +16,7 @@ where
BoxServiceFactory(Box::new(FactoryWrapper(factory)))
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
type Inner<Cfg, Req, Res, Err> = Box<
dyn AFPluginServiceFactory<
Req,
@ -27,7 +27,7 @@ type Inner<Cfg, Req, Res, Err> = Box<
Future = AFBoxFuture<'static, Result<BoxService<Req, Res, Err>, Err>>,
>,
>;
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
type Inner<Cfg, Req, Res, Err> = Box<
dyn AFPluginServiceFactory<
Req,
@ -58,12 +58,12 @@ where
}
}
#[cfg(feature = "wasm_build")]
#[cfg(feature = "single_thread")]
pub type BoxService<Req, Res, Err> = Box<
dyn Service<Req, Response = Res, Error = Err, Future = AFBoxFuture<'static, Result<Res, Err>>>,
>;
#[cfg(not(feature = "wasm_build"))]
#[cfg(not(feature = "single_thread"))]
pub type BoxService<Req, Res, Err> = Box<
dyn Service<Req, Response = Res, Error = Err, Future = AFBoxFuture<'static, Result<Res, Err>>>
+ Sync