From 8ca4ba6e50b7df575d010901004ff895af984ae3 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 13 Aug 2023 16:46:21 +0200 Subject: [PATCH] Change observe() again, pass Arc> into message.update(), Add comment explaining closure --- src/gateway.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/gateway.rs b/src/gateway.rs index 145eaa0..3e0c4f3 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -199,11 +199,14 @@ impl GatewayHandle { pub async fn observe( &self, object: Arc>, - ) -> watch::Receiver { + ) -> watch::Receiver>> { let mut store = self.store.lock().await; if let Some(channel) = store.get(&object.clone().read().unwrap().id()) { let (_, rx) = channel - .downcast_ref::<(watch::Sender, watch::Receiver)>() + .downcast_ref::<( + watch::Sender>>, + watch::Receiver>>, + )>() .unwrap_or_else(|| { panic!( "Snowflake {} already exists in the store, but it is not of type T.", @@ -213,7 +216,7 @@ impl GatewayHandle { rx.clone() } else { let id = object.read().unwrap().id(); - let channel = watch::channel(object.read().unwrap().clone()); + let channel = watch::channel(object); let receiver = channel.1.clone(); store.insert(id, Box::new(channel)); receiver @@ -481,8 +484,11 @@ impl Gateway { $( let message: $message_type = message; if let Some(to_update) = self.store.lock().await.get(&message.id()) { - if let Some((tx, _)) = to_update.downcast_ref::<(watch::Sender<$update_type>, watch::Receiver<$update_type>)>() { - tx.send_modify(|object| message.update(object)); + if let Some((tx, _)) = to_update.downcast_ref::<(watch::Sender>>, watch::Receiver>>)>() { + // `object` is the current value of the `watch::channel`. It's being passed into `message.update()` to be modified + // within the closure function. Then, this closure is passed to the `tx.send_modify()` method which applies the + // modification to the current value of the watch channel. + tx.send_modify(|object| message.update(object.clone())); } else { warn!("Received {} for {}, but it has been observed to be a different type!", $name, message.id()) }