From 3d8d6b6f3be1d68f36b4b2f065a1f7673e604751 Mon Sep 17 00:00:00 2001 From: kozabrada123 <59031733+kozabrada123@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:24:32 +0200 Subject: [PATCH] Make GatewayHandle Clone --- src/api/auth/login.rs | 2 +- src/api/auth/register.rs | 2 +- src/gateway.rs | 6 ++---- src/instance.rs | 6 +++--- tests/common/mod.rs | 2 +- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index 46951be..a95c3ca 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -46,7 +46,7 @@ impl Instance { self.clone_limits_if_some(), login_result.settings, Arc::new(RwLock::new(object)), - Arc::new(gateway), + gateway, ); Ok(user) } diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 44c29d8..1a95d3d 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -54,7 +54,7 @@ impl Instance { self.clone_limits_if_some(), Arc::new(RwLock::new(settings)), Arc::new(RwLock::new(user_object)), - Arc::new(gateway), + gateway, ); Ok(user) } diff --git a/src/gateway.rs b/src/gateway.rs index 68205b5..ebe5ca2 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -158,7 +158,7 @@ pub type ObservableObject = dyn Send + Sync + Any; /// [`GatewayEvents`](GatewayEvent), which you can subscribe to. Gateway events include all currently /// implemented types with the trait [`WebSocketEvent`] /// Using this handle you can also send Gateway Events directly. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct GatewayHandle { pub url: String, pub events: Arc>, @@ -170,7 +170,6 @@ pub struct GatewayHandle { >, >, >, - pub handle: JoinHandle<()>, /// Tells gateway tasks to close kill_send: tokio::sync::broadcast::Sender<()>, pub(crate) store: Arc>>>>, @@ -410,7 +409,7 @@ impl Gateway { }; // Now we can continuously check for messages in a different task, since we aren't going to receive another hello - let handle: JoinHandle<()> = task::spawn(async move { + task::spawn(async move { gateway.gateway_listen_task().await; }); @@ -418,7 +417,6 @@ impl Gateway { url: websocket_url.clone(), events: shared_events, websocket_send: shared_websocket_send.clone(), - handle, kill_send: kill_send.clone(), store, }) diff --git a/src/instance.rs b/src/instance.rs index a3b5325..8ced86a 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -94,7 +94,7 @@ pub struct ChorusUser { pub limits: Option>, pub settings: Arc>, pub object: Arc>, - pub gateway: Arc, // TODO: Can this be an Arc? That way we could have Clone implemented on ChorusUser + pub gateway: GatewayHandle, } impl ChorusUser { @@ -117,7 +117,7 @@ impl ChorusUser { limits: Option>, settings: Arc>, object: Arc>, - gateway: Arc, + gateway: GatewayHandle, ) -> ChorusUser { ChorusUser { belongs_to, @@ -139,7 +139,7 @@ impl ChorusUser { let object = Arc::new(RwLock::new(User::default())); let wss_url = instance.read().unwrap().urls.wss.clone(); // Dummy gateway object - let gateway = Arc::new(Gateway::new(wss_url).await.unwrap()); + let gateway = Gateway::new(wss_url).await.unwrap(); ChorusUser { token, belongs_to: instance.clone(), diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 19c6509..a7b9d5d 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -42,7 +42,7 @@ impl TestBundle { limits: self.user.limits.clone(), settings: self.user.settings.clone(), object: self.user.object.clone(), - gateway: Arc::new(Gateway::new(self.instance.urls.wss.clone()).await.unwrap()), + gateway: Gateway::new(self.instance.urls.wss.clone()).await.unwrap(), } } }