feat: calling user event from web (#4535)

* refactor: user manager

* refactor: user manager

* refactor: session location

* refactor: user manager

* chore: gen ts files

* feat: implement indexeddb persistence

* chore: integrate user manager

* chore: update

* chore: run on web thread

* chore: run on web thread

* chore: fix test

* chore: add test

* chore: add test

* chore: add user & sign in with password

* chore: fix test

* chore: update docs

* chore: fix warnings

* chore: gen files

* chore: add user

* chore: add files

* chore: update config

* chore: update scirpt

* chore: update scirpt

* fix: build

* chore: update command

* fix: ci

* ci: fix

* fix: compile

* fix: compile

* fix: ci

* fix: compile

* fix: tauri build

* chore: fix test

* chore: fix test
This commit is contained in:
Nathan.fooo
2024-01-30 05:36:27 +08:00
committed by GitHub
parent 86a0569d84
commit 55c97b56a3
164 changed files with 9334 additions and 2885 deletions

View File

@ -34,6 +34,7 @@ thread-id = "3.3.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"]}
wasm-bindgen = { version = "0.2.89" }
wasm-bindgen-futures = "0.4"
[dev-dependencies]
tokio = { workspace = true, features = ["rt"] }
@ -43,7 +44,5 @@ futures-util = "0.3.26"
default = ["use_protobuf"]
use_serde = ["bincode", "serde_json", "serde", "serde_repr"]
use_protobuf= ["protobuf"]
single_thread = []
wasm_build = ["single_thread"]

View File

@ -16,60 +16,60 @@ use crate::{
service::{AFPluginServiceFactory, Service},
};
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub trait AFConcurrent {}
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
impl<T> AFConcurrent for T where T: ?Sized {}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub trait AFConcurrent: Send + Sync {}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
impl<T> AFConcurrent for T where T: Send + Sync {}
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub type AFBoxFuture<'a, T> = futures_core::future::LocalBoxFuture<'a, T>;
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub type AFBoxFuture<'a, T> = futures_core::future::BoxFuture<'a, T>;
pub type AFStateMap = std::sync::Arc<AFPluginStateMap>;
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub(crate) fn downcast_owned<T: 'static>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn downcast_owned<T: 'static + Send + Sync>(boxed: AFBox) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub(crate) type AFBox = Box<dyn Any>;
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) type AFBox = Box<dyn Any + Send + Sync>;
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + 'static>;
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub type BoxFutureCallback =
Box<dyn FnOnce(AFPluginEventResponse) -> AFBoxFuture<'static, ()> + Send + Sync + 'static>;
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
T: Future + 'static,
T::Output: 'static,
{
tokio::spawn(future)
tokio::task::spawn_local(future)
}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn af_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
where
T: Future + Send + 'static,
@ -92,10 +92,7 @@ impl AFPluginDispatcher {
}
}
pub async fn async_send<Req>(
dispatch: Arc<AFPluginDispatcher>,
request: Req,
) -> AFPluginEventResponse
pub async fn async_send<Req>(dispatch: &AFPluginDispatcher, request: Req) -> AFPluginEventResponse
where
Req: Into<AFPluginRequest>,
{
@ -103,7 +100,7 @@ impl AFPluginDispatcher {
}
pub async fn async_send_with_callback<Req, Callback>(
dispatch: Arc<AFPluginDispatcher>,
dispatch: &AFPluginDispatcher,
request: Req,
callback: Callback,
) -> AFPluginEventResponse
@ -146,7 +143,7 @@ impl AFPluginDispatcher {
}
pub fn box_async_send<Req>(
dispatch: Arc<AFPluginDispatcher>,
dispatch: &AFPluginDispatcher,
request: Req,
) -> DispatchFuture<AFPluginEventResponse>
where
@ -156,7 +153,7 @@ impl AFPluginDispatcher {
}
pub fn boxed_async_send_with_callback<Req, Callback>(
dispatch: Arc<AFPluginDispatcher>,
dispatch: &AFPluginDispatcher,
request: Req,
callback: Callback,
) -> DispatchFuture<AFPluginEventResponse>
@ -189,9 +186,10 @@ impl AFPluginDispatcher {
})
});
let runtime = dispatch.runtime.clone();
DispatchFuture {
fut: Box::pin(async move {
let result = dispatch.runtime.run_until(handle).await;
let result = runtime.run_until(handle).await;
result.unwrap_or_else(|e| {
let msg = format!("EVENT_DISPATCH join error: {:?}", e);
tracing::error!("{}", msg);
@ -202,19 +200,19 @@ impl AFPluginDispatcher {
}
}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn sync_send(
dispatch: Arc<AFPluginDispatcher>,
request: AFPluginRequest,
) -> AFPluginEventResponse {
futures::executor::block_on(AFPluginDispatcher::async_send_with_callback(
dispatch,
dispatch.as_ref(),
request,
|_| Box::pin(async {}),
))
}
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -223,7 +221,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
#[track_caller]
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
@ -233,7 +231,7 @@ impl AFPluginDispatcher {
self.runtime.spawn(future)
}
#[cfg(feature = "single_thread")]
#[cfg(target_arch = "wasm32")]
pub async fn run_until<F>(&self, future: F) -> F::Output
where
F: Future + 'static,
@ -242,7 +240,7 @@ impl AFPluginDispatcher {
self.runtime.run_until(handle).await.unwrap()
}
#[cfg(not(feature = "single_thread"))]
#[cfg(not(target_arch = "wasm32"))]
pub async fn run_until<'a, F>(&self, future: F) -> F::Output
where
F: Future + Send + 'a,

View File

@ -18,6 +18,15 @@ impl Payload {
}
}
impl AsRef<[u8]> for Payload {
fn as_ref(&self) -> &[u8] {
match self {
Payload::None => &[],
Payload::Bytes(bytes) => bytes.as_ref(),
}
}
}
impl std::fmt::Debug for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
format_payload_print(self, f)

View File

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

View File

@ -16,7 +16,7 @@ async fn test() {
vec![AFPlugin::new().event(event, hello)],
));
let request = AFPluginRequest::new(event);
let _ = AFPluginDispatcher::async_send_with_callback(dispatch.clone(), request, |resp| {
let _ = AFPluginDispatcher::async_send_with_callback(dispatch.as_ref(), request, |resp| {
Box::pin(async move {
dbg!(&resp);
})