Moddify observe to still store T internally

This commit is contained in:
bitfl0wer 2023-08-13 15:52:16 +02:00
parent 29d4ef23a2
commit 73cc139e78
2 changed files with 21 additions and 25 deletions

View File

@ -196,17 +196,14 @@ impl GatewayHandle {
.unwrap(); .unwrap();
} }
pub async fn observe<T: Updateable>( pub async fn observe<T: Updateable + Clone>(
&self, &self,
object: Arc<RwLock<T>>, object: Arc<RwLock<T>>,
) -> watch::Receiver<Arc<RwLock<T>>> { ) -> watch::Receiver<T> {
let mut store = self.store.lock().await; let mut store = self.store.lock().await;
if let Some(channel) = store.get(&object.clone().read().unwrap().id()) { if let Some(channel) = store.get(&object.clone().read().unwrap().id()) {
let (_, rx) = channel let (_, rx) = channel
.downcast_ref::<( .downcast_ref::<(watch::Sender<T>, watch::Receiver<T>)>()
watch::Sender<Arc<RwLock<T>>>,
watch::Receiver<Arc<RwLock<T>>>,
)>()
.unwrap_or_else(|| { .unwrap_or_else(|| {
panic!( panic!(
"Snowflake {} already exists in the store, but it is not of type T.", "Snowflake {} already exists in the store, but it is not of type T.",
@ -216,7 +213,7 @@ impl GatewayHandle {
rx.clone() rx.clone()
} else { } else {
let id = object.read().unwrap().id(); let id = object.read().unwrap().id();
let channel = watch::channel(object); let channel = watch::channel(object.read().unwrap().clone());
let receiver = channel.1.clone(); let receiver = channel.1.clone();
store.insert(id, Box::new(channel)); store.insert(id, Box::new(channel));
receiver receiver

View File

@ -1,7 +1,7 @@
mod common; mod common;
use chorus::gateway::*; use chorus::gateway::*;
use chorus::types::{self, Channel}; use chorus::types::{self, Channel, ChannelModifySchema};
#[tokio::test] #[tokio::test]
/// Tests establishing a connection (hello and heartbeats) on the local gateway; /// Tests establishing a connection (hello and heartbeats) on the local gateway;
@ -31,28 +31,27 @@ async fn test_self_updating_structs() {
let mut bundle = common::setup().await; let mut bundle = common::setup().await;
let channel_updater = bundle.user.gateway.observe(bundle.channel.clone()).await; let channel_updater = bundle.user.gateway.observe(bundle.channel.clone()).await;
let received_channel = channel_updater.borrow().clone(); let received_channel = channel_updater.borrow().clone();
assert_eq!(received_channel, bundle.channel.read().unwrap().clone());
let updater = bundle.user.gateway.observe(bundle.channel.clone()).await;
assert_eq!( assert_eq!(
*received_channel.read().unwrap(), updater.borrow().clone().name.unwrap(),
*bundle.channel.read().unwrap() bundle.channel.read().unwrap().clone().name.unwrap()
); );
let channel = &mut bundle.channel;
let modify_data = types::ChannelModifySchema { let channel = bundle.channel.read().unwrap().clone();
name: Some("beepboop".to_string()), let modify_schema = ChannelModifySchema {
name: Some("selfupdating".to_string()),
..Default::default() ..Default::default()
}; };
let channel_id = channel.read().unwrap().id; Channel::modify(&channel, modify_schema, &mut bundle.user)
Channel::modify(
channel.read().as_ref().unwrap(),
modify_data,
channel_id,
&mut bundle.user,
)
.await .await
.unwrap(); .unwrap();
let received_channel = channel_updater.borrow();
assert_eq!( assert_eq!(
received_channel.read().unwrap().name.as_ref().unwrap(), updater.borrow().clone().name.unwrap(),
"beepboop" "selfupdating".to_string()
); );
common::teardown(bundle).await common::teardown(bundle).await
} }