diff options
Diffstat (limited to '')
-rw-r--r-- | lampada/src/lib.rs | 74 |
1 files changed, 67 insertions, 7 deletions
diff --git a/lampada/src/lib.rs b/lampada/src/lib.rs index 7346c42..dacc56d 100644 --- a/lampada/src/lib.rs +++ b/lampada/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(where_clause_attrs)] + use std::{ collections::HashMap, ops::{Deref, DerefMut}, @@ -21,6 +23,8 @@ use tokio::{ task::JoinSet, time::timeout, }; +#[cfg(target_arch = "wasm32")] +use tokio_with_wasm::alias as tokio; use tracing::{debug, info}; use crate::connection::write::WriteHandle; @@ -58,20 +62,24 @@ pub trait Logic { type Cmd; /// run after binding to the stream (e.g. for a chat client, ) + #[cfg(not(target_arch = "wasm32"))] fn handle_connect(self, connection: Connected) -> impl std::future::Future<Output = ()> + Send; /// run before closing the stream (e.g. send unavailable presence in a chat client) + #[cfg(not(target_arch = "wasm32"))] fn handle_disconnect( self, connection: Connected, ) -> impl std::future::Future<Output = ()> + Send; + #[cfg(not(target_arch = "wasm32"))] fn handle_stream_error( self, stream_error: StreamError, ) -> impl std::future::Future<Output = ()> + Send; /// run to handle an incoming xmpp stanza + #[cfg(not(target_arch = "wasm32"))] fn handle_stanza( self, stanza: Stanza, @@ -79,6 +87,7 @@ pub trait Logic { ) -> impl std::future::Future<Output = ()> + std::marker::Send; /// run to handle a command message when a connection is currently established + #[cfg(not(target_arch = "wasm32"))] fn handle_online( self, command: Self::Cmd, @@ -86,21 +95,67 @@ pub trait Logic { ) -> impl std::future::Future<Output = ()> + std::marker::Send; /// run to handle a command message when disconnected + #[cfg(not(target_arch = "wasm32"))] fn handle_offline( self, command: Self::Cmd, ) -> impl std::future::Future<Output = ()> + std::marker::Send; /// run as cleanup after either an abort or a disconnect (e.g. reply to all pending requests with a disconnected error) + #[cfg(not(target_arch = "wasm32"))] fn on_abort(self) -> impl std::future::Future<Output = ()> + std::marker::Send; /// handle connection errors from the core client logic + #[cfg(not(target_arch = "wasm32"))] fn handle_connection_error( self, error: ConnectionError, ) -> impl std::future::Future<Output = ()> + std::marker::Send; // async fn handle_stream_error(self, error) {} + #[cfg(target_arch = "wasm32")] + fn handle_connect(self, connection: Connected) -> impl std::future::Future<Output = ()>; + + /// run before closing the stream (e.g. send unavailable presence in a chat client) + #[cfg(target_arch = "wasm32")] + fn handle_disconnect(self, connection: Connected) -> impl std::future::Future<Output = ()>; + + #[cfg(target_arch = "wasm32")] + fn handle_stream_error( + self, + stream_error: StreamError, + ) -> impl std::future::Future<Output = ()>; + + /// run to handle an incoming xmpp stanza + #[cfg(target_arch = "wasm32")] + fn handle_stanza( + self, + stanza: Stanza, + connection: Connected, + ) -> impl std::future::Future<Output = ()>; + + /// run to handle a command message when a connection is currently established + #[cfg(target_arch = "wasm32")] + fn handle_online( + self, + command: Self::Cmd, + connection: Connected, + ) -> impl std::future::Future<Output = ()>; + + /// run to handle a command message when disconnected + #[cfg(target_arch = "wasm32")] + fn handle_offline(self, command: Self::Cmd) -> impl std::future::Future<Output = ()>; + + /// run as cleanup after either an abort or a disconnect (e.g. reply to all pending requests with a disconnected error) + #[cfg(target_arch = "wasm32")] + fn on_abort(self) -> impl std::future::Future<Output = ()>; + + /// handle connection errors from the core client logic + #[cfg(target_arch = "wasm32")] + fn handle_connection_error( + self, + error: ConnectionError, + ) -> impl std::future::Future<Output = ()>; } /// an actor that implements xmpp core (rfc6120), manages connection/stream status, and delegates any other logic to the generic which implements Logic, allowing different kinds of clients (e.g. chat, social, pubsub) to be built upon the same core @@ -117,10 +172,15 @@ pub struct CoreClient<Lgc: Logic> { logic: Lgc, // config: LampConfig, // TODO: will grow forever at this point, maybe not required as tasks will naturally shut down anyway? - tasks: JoinSet<()>, + // tasks: JoinSet<()>, } -impl<Lgc: Logic + Clone + Send + 'static> CoreClient<Lgc> { +impl<Lgc> CoreClient<Lgc> +where + Lgc: Logic + Clone + 'static, + #[cfg(not(target_arch = "wasm32"))] + Lgc: Send, +{ /// create a new actor pub fn new( jid: JID, @@ -137,7 +197,7 @@ impl<Lgc: Logic + Clone + Send + 'static> CoreClient<Lgc> { receiver, connection_supervisor_shutdown, logic, - tasks: JoinSet::new(), + // tasks: JoinSet::new(), } } @@ -240,10 +300,10 @@ impl<Lgc: Logic + Clone + Send + 'static> CoreClient<Lgc> { }, CoreClientCommand::Command(command) => { match self.connected.as_ref() { - Some((w, s)) => self - .tasks - .spawn(self.logic.clone().handle_online(command, w.clone())), - None => self.tasks.spawn(self.logic.clone().handle_offline(command)), + Some((w, s)) => { + tokio::spawn(self.logic.clone().handle_online(command, w.clone())) + } + None => tokio::spawn(self.logic.clone().handle_offline(command)), }; } } |