From aa7d8a33f88205eaf410a60302212a50ca6e9fad Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 29 Apr 2024 12:20:12 -0400 Subject: [PATCH 1/9] feat: custom data on sync client --- socketio/src/client/builder.rs | 8 ++++++++ socketio/src/client/raw_client.rs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/socketio/src/client/builder.rs b/socketio/src/client/builder.rs index 724971f0..2feacd22 100644 --- a/socketio/src/client/builder.rs +++ b/socketio/src/client/builder.rs @@ -41,6 +41,7 @@ pub struct ClientBuilder { opening_headers: Option, transport_type: TransportType, auth: Option, + data: Option>, pub(crate) reconnect: bool, pub(crate) reconnect_on_disconnect: bool, // None reconnect attempts represent infinity. @@ -98,9 +99,15 @@ impl ClientBuilder { max_reconnect_attempts: None, reconnect_delay_min: 1000, reconnect_delay_max: 5000, + data: None, } } + pub fn data(mut self, data: Arc) -> Self { + self.data = Some(data); + self + } + /// Sets the target namespace of the client. The namespace should start /// with a leading `/`. Valid examples are e.g. `/admin`, `/foo`. pub fn namespace>(mut self, namespace: T) -> Self { @@ -365,6 +372,7 @@ impl ClientBuilder { self.on, self.on_any, self.auth, + self.data.unwrap_or(Arc::new(())), )?; socket.connect()?; diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index 0686683f..5aa2378e 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -41,6 +41,7 @@ pub struct RawClient { nsp: String, // Data send in the opening packet (commonly used as for auth) auth: Option, + data: Arc, } impl RawClient { @@ -54,6 +55,7 @@ impl RawClient { on: Arc>>>, on_any: Arc>>>, auth: Option, + data: Arc, ) -> Result { Ok(RawClient { socket, @@ -62,9 +64,21 @@ impl RawClient { on_any, outstanding_acks: Arc::new(Mutex::new(Vec::new())), auth, + data, }) } + // TODO: Write documentation + pub fn data(&self) -> Arc { + self.try_data() + .expect("RawClient::data does not match ClientBuilder::data") + } + + // TODO: Write documentation + pub fn try_data(&self) -> Option> { + Arc::clone(&self.data).downcast().ok() + } + /// Connects the client to a server. Afterwards the `emit_*` methods can be /// called to interact with the server. Attention: it's not allowed to add a /// callback after a call to this method. From 0f3d383c4de89e134b46bb8ecc9e2092411f82a5 Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 29 Apr 2024 12:40:23 -0400 Subject: [PATCH 2/9] feat: custom data on async client --- socketio/src/asynchronous/client/builder.rs | 8 ++++++++ socketio/src/asynchronous/client/client.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/socketio/src/asynchronous/client/builder.rs b/socketio/src/asynchronous/client/builder.rs index 44710e19..3a56b41f 100644 --- a/socketio/src/asynchronous/client/builder.rs +++ b/socketio/src/asynchronous/client/builder.rs @@ -6,6 +6,7 @@ use rust_engineio::{ header::{HeaderMap, HeaderValue}, }; use std::collections::HashMap; +use std::sync::Arc; use url::Url; use crate::{error::Result, Event, Payload, TransportType}; @@ -38,6 +39,7 @@ pub struct ClientBuilder { pub(crate) max_reconnect_attempts: Option, pub(crate) reconnect_delay_min: u64, pub(crate) reconnect_delay_max: u64, + pub(crate) data: Option>, } impl ClientBuilder { @@ -97,9 +99,15 @@ impl ClientBuilder { max_reconnect_attempts: None, reconnect_delay_min: 1000, reconnect_delay_max: 5000, + data: None, } } + pub fn data(mut self, data: Arc) -> Self { + self.data = Some(data); + self + } + /// Sets the target namespace of the client. The namespace should start /// with a leading `/`. Valid examples are e.g. `/admin`, `/foo`. /// If the String provided doesn't start with a leading `/`, it is diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index 67feb7db..f02ab249 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -74,6 +74,7 @@ pub struct Client { auth: Option, builder: Arc>, disconnect_reason: Arc>, + data: Arc, } impl Client { @@ -87,11 +88,23 @@ impl Client { nsp: builder.namespace.to_owned(), outstanding_acks: Arc::new(RwLock::new(Vec::new())), auth: builder.auth.clone(), + data: builder.data.clone().unwrap_or(Arc::new(())), builder: Arc::new(RwLock::new(builder)), disconnect_reason: Arc::new(RwLock::new(DisconnectReason::default())), }) } + // TODO: Documentation + pub fn data(&self) -> Arc { + self.try_data() + .expect("Client::data does not match ClientBuilder::data") + } + + // TODO: Documentation + pub fn try_data(&self) -> Option> { + Arc::clone(&self.data).downcast().ok() + } + /// Connects the client to a server. Afterwards the `emit_*` methods can be /// called to interact with the server. pub(crate) async fn connect(&self) -> Result<()> { From f410d7c67d9567a4bfc656d9ef7c9268f606a72d Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 29 Apr 2024 14:22:59 -0400 Subject: [PATCH 3/9] doc: add documentation for data and try_data --- socketio/src/asynchronous/client/client.rs | 6 ++++-- socketio/src/client/raw_client.rs | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index f02ab249..ca77a5a5 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -94,13 +94,15 @@ impl Client { }) } - // TODO: Documentation + /// Fetches data given by [`ClientBuilder::data`] pub fn data(&self) -> Arc { self.try_data() .expect("Client::data does not match ClientBuilder::data") } - // TODO: Documentation + /// Attempts to fetches data given by [`ClientBuilder::data`] + /// + /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn try_data(&self) -> Option> { Arc::clone(&self.data).downcast().ok() } diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index 5aa2378e..e11ce7c2 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -14,6 +14,7 @@ use std::time::Duration; use std::time::Instant; use crate::socket::Socket as InnerSocket; +use crate::asynchronous::ClientBuilder; /// Represents an `Ack` as given back to the caller. Holds the internal `id` as /// well as the current ack'ed state. Holds data which will be accessible as @@ -68,13 +69,15 @@ impl RawClient { }) } - // TODO: Write documentation + /// Fetches data given by [`ClientBuilder::data`] pub fn data(&self) -> Arc { self.try_data() .expect("RawClient::data does not match ClientBuilder::data") } - // TODO: Write documentation + /// Attempts to fetches data given by [`ClientBuilder::data`] + /// + /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn try_data(&self) -> Option> { Arc::clone(&self.data).downcast().ok() } From f27bdbfede20a354c086d96ffd853f7b111207ba Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 29 Apr 2024 14:54:57 -0400 Subject: [PATCH 4/9] doc: fix typos --- socketio/src/asynchronous/client/client.rs | 2 +- socketio/src/client/raw_client.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index ca77a5a5..db2a1f31 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -100,7 +100,7 @@ impl Client { .expect("Client::data does not match ClientBuilder::data") } - /// Attempts to fetches data given by [`ClientBuilder::data`] + /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn try_data(&self) -> Option> { diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index e11ce7c2..22791f84 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -75,7 +75,7 @@ impl RawClient { .expect("RawClient::data does not match ClientBuilder::data") } - /// Attempts to fetches data given by [`ClientBuilder::data`] + /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn try_data(&self) -> Option> { From 220bdbe325149259f994c4c65070ba73384125ab Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 15 May 2024 03:38:55 -0400 Subject: [PATCH 5/9] refactor: rename data to set_data on client bulder --- socketio/src/asynchronous/client/builder.rs | 2 +- socketio/src/client/builder.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/socketio/src/asynchronous/client/builder.rs b/socketio/src/asynchronous/client/builder.rs index 3a56b41f..90cbb087 100644 --- a/socketio/src/asynchronous/client/builder.rs +++ b/socketio/src/asynchronous/client/builder.rs @@ -103,7 +103,7 @@ impl ClientBuilder { } } - pub fn data(mut self, data: Arc) -> Self { + pub fn set_data(mut self, data: Arc) -> Self { self.data = Some(data); self } diff --git a/socketio/src/client/builder.rs b/socketio/src/client/builder.rs index 2feacd22..eec84cf1 100644 --- a/socketio/src/client/builder.rs +++ b/socketio/src/client/builder.rs @@ -103,7 +103,7 @@ impl ClientBuilder { } } - pub fn data(mut self, data: Arc) -> Self { + pub fn set_data(mut self, data: Arc) -> Self { self.data = Some(data); self } From 101193dd0748cc2bd4e32c4fec6107bd165237b3 Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 15 May 2024 03:43:04 -0400 Subject: [PATCH 6/9] doc: set temporary docs for set_data --- socketio/src/asynchronous/client/builder.rs | 2 ++ socketio/src/client/builder.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/socketio/src/asynchronous/client/builder.rs b/socketio/src/asynchronous/client/builder.rs index 90cbb087..f9c2eae2 100644 --- a/socketio/src/asynchronous/client/builder.rs +++ b/socketio/src/asynchronous/client/builder.rs @@ -103,6 +103,8 @@ impl ClientBuilder { } } + /// Sets the client's custom data. + // TODO: write example usage pub fn set_data(mut self, data: Arc) -> Self { self.data = Some(data); self diff --git a/socketio/src/client/builder.rs b/socketio/src/client/builder.rs index eec84cf1..6caa46cb 100644 --- a/socketio/src/client/builder.rs +++ b/socketio/src/client/builder.rs @@ -103,6 +103,8 @@ impl ClientBuilder { } } + /// Sets the client's custom data. + // TODO: write example usage pub fn set_data(mut self, data: Arc) -> Self { self.data = Some(data); self From fd8012b39818f9de41cb6f088a7aa5a90a03e032 Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 15 May 2024 03:57:54 -0400 Subject: [PATCH 7/9] feat: remove data functions that panic --- socketio/src/asynchronous/client/client.rs | 6 ------ socketio/src/client/raw_client.rs | 6 ------ 2 files changed, 12 deletions(-) diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index db2a1f31..5fa56978 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -94,12 +94,6 @@ impl Client { }) } - /// Fetches data given by [`ClientBuilder::data`] - pub fn data(&self) -> Arc { - self.try_data() - .expect("Client::data does not match ClientBuilder::data") - } - /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index 22791f84..8de29b48 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -69,12 +69,6 @@ impl RawClient { }) } - /// Fetches data given by [`ClientBuilder::data`] - pub fn data(&self) -> Arc { - self.try_data() - .expect("RawClient::data does not match ClientBuilder::data") - } - /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] From d18cb8f66da1d826a4719b2f7b6a9cb1e1680003 Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 15 May 2024 03:58:43 -0400 Subject: [PATCH 8/9] refactor: rename try_data to custom_data --- socketio/src/asynchronous/client/client.rs | 2 +- socketio/src/client/raw_client.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index 5fa56978..8c3d0b2e 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -97,7 +97,7 @@ impl Client { /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] - pub fn try_data(&self) -> Option> { + pub fn custom_data(&self) -> Option> { Arc::clone(&self.data).downcast().ok() } diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index 8de29b48..a3e84011 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -72,7 +72,7 @@ impl RawClient { /// Attempts to fetch data given by [`ClientBuilder::data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] - pub fn try_data(&self) -> Option> { + pub fn custom_data(&self) -> Option> { Arc::clone(&self.data).downcast().ok() } From 01759081b46e6bc4371eefa837af5a3b5334bbf7 Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 15 May 2024 03:59:33 -0400 Subject: [PATCH 9/9] doc: reference ClientBuilder::set_data instead --- socketio/src/asynchronous/client/client.rs | 2 +- socketio/src/client/raw_client.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/socketio/src/asynchronous/client/client.rs b/socketio/src/asynchronous/client/client.rs index 8c3d0b2e..9409940a 100644 --- a/socketio/src/asynchronous/client/client.rs +++ b/socketio/src/asynchronous/client/client.rs @@ -94,7 +94,7 @@ impl Client { }) } - /// Attempts to fetch data given by [`ClientBuilder::data`] + /// Attempts to fetch data given by [`ClientBuilder::set_data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn custom_data(&self) -> Option> { diff --git a/socketio/src/client/raw_client.rs b/socketio/src/client/raw_client.rs index a3e84011..fd24d159 100644 --- a/socketio/src/client/raw_client.rs +++ b/socketio/src/client/raw_client.rs @@ -14,7 +14,7 @@ use std::time::Duration; use std::time::Instant; use crate::socket::Socket as InnerSocket; -use crate::asynchronous::ClientBuilder; +use crate::client::builder::ClientBuilder; /// Represents an `Ack` as given back to the caller. Holds the internal `id` as /// well as the current ack'ed state. Holds data which will be accessible as @@ -69,7 +69,7 @@ impl RawClient { }) } - /// Attempts to fetch data given by [`ClientBuilder::data`] + /// Attempts to fetch data given by [`ClientBuilder::set_data`] /// /// None is returned if data was not given or data does not match [`ClientBuilder::data`] pub fn custom_data(&self) -> Option> {