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

View File

@ -1,7 +1,7 @@
mod common;
use chorus::gateway::*;
use chorus::types::{self, Channel};
use chorus::types::{self, Channel, ChannelModifySchema};
#[tokio::test]
/// 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 channel_updater = bundle.user.gateway.observe(bundle.channel.clone()).await;
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!(
*received_channel.read().unwrap(),
*bundle.channel.read().unwrap()
updater.borrow().clone().name.unwrap(),
bundle.channel.read().unwrap().clone().name.unwrap()
);
let channel = &mut bundle.channel;
let modify_data = types::ChannelModifySchema {
name: Some("beepboop".to_string()),
let channel = bundle.channel.read().unwrap().clone();
let modify_schema = ChannelModifySchema {
name: Some("selfupdating".to_string()),
..Default::default()
};
let channel_id = channel.read().unwrap().id;
Channel::modify(
channel.read().as_ref().unwrap(),
modify_data,
channel_id,
&mut bundle.user,
)
Channel::modify(&channel, modify_schema, &mut bundle.user)
.await
.unwrap();
let received_channel = channel_updater.borrow();
assert_eq!(
received_channel.read().unwrap().name.as_ref().unwrap(),
"beepboop"
updater.borrow().clone().name.unwrap(),
"selfupdating".to_string()
);
common::teardown(bundle).await
}