feat: folder web (#4580)

* chore: folder wasm

* chore: folder wasm

* chore: resolve deps

* chore: fix trait

* chore: try localset

* chore: fix

* chore: fix

* chore: fix

* chore: async init sdk

* chore: fix test

* chore: fix test
This commit is contained in:
Nathan.fooo
2024-02-04 05:50:23 +08:00
committed by GitHub
parent 08938b8c70
commit fda70ff560
65 changed files with 818 additions and 558 deletions

View File

@ -44,5 +44,6 @@ futures-util = "0.3.26"
default = ["use_protobuf"]
use_serde = ["bincode", "serde_json", "serde", "serde_repr"]
use_protobuf= ["protobuf"]
local_set = []

View File

@ -16,51 +16,51 @@ use crate::{
service::{AFPluginServiceFactory, Service},
};
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub trait AFConcurrent {}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
impl<T> AFConcurrent for T where T: ?Sized {}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub trait AFConcurrent: Send + Sync {}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
impl<T> AFConcurrent for T where T: Send + Sync {}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub type AFBoxFuture<'a, T> = futures_core::future::LocalBoxFuture<'a, T>;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub type AFBoxFuture<'a, T> = futures_core::future::BoxFuture<'a, T>;
pub type AFStateMap = std::sync::Arc<AFPluginStateMap>;
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub(crate) fn downcast_owned<T: 'static>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub(crate) fn downcast_owned<T: 'static + Send + Sync>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub(crate) type AFBox = Box<dyn Any>;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub(crate) type AFBox = Box<dyn Any + Send + Sync>;
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + 'static>;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + Send + Sync + 'static>;
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + 'static,
@ -69,7 +69,7 @@ where
tokio::task::spawn_local(future)
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + Send + 'static,
@ -170,15 +170,6 @@ impl AFPluginDispatcher {
callback: Some(Box::new(callback)),
};
// Spawns a future onto the runtime.
//
// This spawns the given future onto the runtime's executor, usually a
// thread pool. The thread pool is then responsible for polling the future
// until it completes.
//
// The provided future will start running in the background immediately
// when `spawn` is called, even if you don't await the returned
// `JoinHandle`.
let handle = dispatch.runtime.spawn(async move {
service.call(service_ctx).await.unwrap_or_else(|e| {
tracing::error!("Dispatch runtime error: {:?}", e);
@ -186,17 +177,35 @@ impl AFPluginDispatcher {
})
});
let runtime = dispatch.runtime.clone();
DispatchFuture {
fut: Box::pin(async move {
let result = runtime.run_until(handle).await;
result.unwrap_or_else(|e| {
let msg = format!("EVENT_DISPATCH join error: {:?}", e);
tracing::error!("{}", msg);
let error = InternalError::JoinError(msg);
error.as_response()
})
}),
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
{
let result = dispatch.runtime.block_on(handle);
DispatchFuture {
fut: Box::pin(async move {
result.unwrap_or_else(|e| {
let msg = format!("EVENT_DISPATCH join error: {:?}", e);
tracing::error!("{}", msg);
let error = InternalError::JoinError(msg);
error.as_response()
})
}),
}
}
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
{
let runtime = dispatch.runtime.clone();
DispatchFuture {
fut: Box::pin(async move {
let result = runtime.run_until(handle).await;
result.unwrap_or_else(|e| {
let msg = format!("EVENT_DISPATCH join error: {:?}", e);
tracing::error!("{}", msg);
let error = InternalError::JoinError(msg);
error.as_response()
})
}),
}
}
}
@ -212,7 +221,7 @@ impl AFPluginDispatcher {
))
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -221,7 +230,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -231,7 +240,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future + 'static,
@ -240,7 +249,7 @@ impl AFPluginDispatcher {
self.runtime.run_until(handle).await.unwrap()
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub async fn run_until<'a, F>(&self, future: F) -> F::Output
where
F: Future + Send + 'a,

View File

@ -8,14 +8,14 @@ use tokio::task::JoinHandle;
pub struct AFPluginRuntime {
inner: Runtime,
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
local: tokio::task::LocalSet,
}
impl Display for AFPluginRuntime {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if cfg!(target_arch = "wasm32") {
write!(f, "Runtime(single_thread)")
if cfg!(any(target_arch = "wasm32", feature = "local_set")) {
write!(f, "Runtime(current_thread)")
} else {
write!(f, "Runtime(multi_thread)")
}
@ -27,12 +27,12 @@ impl AFPluginRuntime {
let inner = default_tokio_runtime()?;
Ok(Self {
inner,
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
local: tokio::task::LocalSet::new(),
})
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
#[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(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
@ -51,7 +51,7 @@ impl AFPluginRuntime {
self.inner.spawn(future)
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
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(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future,
@ -67,7 +67,7 @@ impl AFPluginRuntime {
future.await
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
#[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(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
#[track_caller]
pub fn block_on<F>(&self, f: F) -> F::Output
where
@ -86,14 +86,14 @@ impl AFPluginRuntime {
}
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub fn default_tokio_runtime() -> io::Result<Runtime> {
runtime::Builder::new_current_thread()
.thread_name("dispatch-rt-st")
.build()
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
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(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
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(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
type Inner<Cfg, Req, Res, Err> = Box<
dyn AFPluginServiceFactory<
Req,
@ -58,12 +58,12 @@ where
}
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "local_set"))]
pub type BoxService<Req, Res, Err> = Box<
dyn Service<Req, Response = Res, Error = Err, Future = AFBoxFuture<'static, Result<Res, Err>>>,
>;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), not(feature = "local_set")))]
pub type BoxService<Req, Res, Err> = Box<
dyn Service<Req, Response = Res, Error = Err, Future = AFBoxFuture<'static, Result<Res, Err>>>
+ Sync