Compare commits

..

No commits in common. "21699e5899c2b869d0def2d5411f2ab3fe0b4513" and "6637f14b18793f1b0bde67a65b2d052a152bf29d" have entirely different histories.

3 changed files with 17 additions and 21 deletions

View File

@ -71,9 +71,9 @@ pub trait Composite<T: Updateable + Clone + Debug> {
async fn watch_whole(self, gateway: &GatewayHandle) -> Self;
async fn option_observe_fn(
value: Option<Shared<T>>,
value: Option<Arc<RwLock<T>>>,
gateway: &GatewayHandle,
) -> Option<Shared<T>>
) -> Option<Arc<RwLock<T>>>
where
T: Composite<T> + Debug,
{
@ -86,9 +86,9 @@ pub trait Composite<T: Updateable + Clone + Debug> {
}
async fn option_vec_observe_fn(
value: Option<Vec<Shared<T>>>,
value: Option<Vec<Arc<RwLock<T>>>>,
gateway: &GatewayHandle,
) -> Option<Vec<Shared<T>>>
) -> Option<Vec<Arc<RwLock<T>>>>
where
T: Composite<T>,
{
@ -103,14 +103,17 @@ pub trait Composite<T: Updateable + Clone + Debug> {
}
}
async fn value_observe_fn(value: Shared<T>, gateway: &GatewayHandle) -> Shared<T>
async fn value_observe_fn(value: Arc<RwLock<T>>, gateway: &GatewayHandle) -> Arc<RwLock<T>>
where
T: Composite<T>,
{
gateway.observe(value).await
}
async fn vec_observe_fn(value: Vec<Shared<T>>, gateway: &GatewayHandle) -> Vec<Shared<T>>
async fn vec_observe_fn(
value: Vec<Arc<RwLock<T>>>,
gateway: &GatewayHandle,
) -> Vec<Arc<RwLock<T>>>
where
T: Composite<T>,
{
@ -120,20 +123,17 @@ pub trait Composite<T: Updateable + Clone + Debug> {
}
vec
}
}
pub trait IntoShared {
/// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`.
///
/// [`Shared<Self>`] can then be observed using the [`Gateway`], turning the underlying
/// `dyn Composite<Self>` into a self-updating struct, which is a tracked variant of a chorus
/// entity struct, updating its' held information when new information concerning itself arrives
/// over the [`Gateway`] connection, reducing the need for expensive network-API calls.
fn into_shared(self) -> Shared<Self>;
}
impl<T: Sized> IntoShared for T {
fn into_shared(self) -> Shared<Self> {
fn into_shared(self) -> Shared<Self>
where
Self: Sized,
{
Arc::new(RwLock::new(self))
}
}

View File

@ -1,5 +1,5 @@
use chorus::gateway::{Gateway, Shared};
use chorus::types::IntoShared;
use chorus::types::Composite;
use chorus::{
instance::{ChorusUser, Instance},
types::{

View File

@ -2,7 +2,7 @@ mod common;
use chorus::errors::GatewayError;
use chorus::gateway::*;
use chorus::types::{self, ChannelModifySchema, IntoShared, RoleCreateModifySchema, RoleObject};
use chorus::types::{self, ChannelModifySchema, Composite, RoleCreateModifySchema, RoleObject};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
@ -95,11 +95,7 @@ async fn test_recursive_self_updating_structs() {
.await
.unwrap();
// Watch role;
bundle
.user
.gateway
.observe(role.clone().into_shared())
.await;
bundle.user.gateway.observe(role.into_shared()).await;
// Update Guild and check for Guild
let inner_guild = guild.read().unwrap().clone();
assert!(inner_guild.roles.is_some());
@ -111,7 +107,7 @@ async fn test_recursive_self_updating_structs() {
let role_inner = bundle
.user
.gateway
.observe_and_into_inner(role.clone().into_shared())
.observe_and_into_inner(role.into_shared())
.await;
assert_eq!(role_inner.name, "yippieee");
// Check if the change propagated