Compare commits
6 Commits
b324b6ef10
...
813bee6f1d
Author | SHA1 | Date |
---|---|---|
Flori | 813bee6f1d | |
bitfl0wer | 21699e5899 | |
bitfl0wer | 5372d2c475 | |
bitfl0wer | 29f3ee802a | |
kozabrada123 | 8e25f401a5 | |
Flori | 85e494dd4a |
|
@ -19,6 +19,7 @@ use crate::UrlBundle;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||||
/// The [`Instance`]; what you will be using to perform all sorts of actions on the Spacebar server.
|
/// The [`Instance`]; what you will be using to perform all sorts of actions on the Spacebar server.
|
||||||
|
///
|
||||||
/// If `limits_information` is `None`, then the instance will not be rate limited.
|
/// If `limits_information` is `None`, then the instance will not be rate limited.
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
pub urls: UrlBundle,
|
pub urls: UrlBundle,
|
||||||
|
@ -72,8 +73,18 @@ impl PartialEq for LimitsInformation {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
/// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle). To create an Instance from one singular url, use [`Instance::from_root_url()`].
|
|
||||||
async fn from_url_bundle(urls: UrlBundle) -> ChorusResult<Instance> {
|
pub(crate) fn clone_limits_if_some(&self) -> Option<HashMap<LimitType, Limit>> {
|
||||||
|
if self.limits_information.is_some() {
|
||||||
|
return Some(self.limits_information.as_ref().unwrap().ratelimits.clone());
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle).
|
||||||
|
///
|
||||||
|
/// To create an Instance from one singular url, use [`Instance::new()`].
|
||||||
|
pub async fn from_url_bundle(urls: UrlBundle) -> ChorusResult<Instance> {
|
||||||
let is_limited: Option<LimitsConfiguration> = Instance::is_limited(&urls.api).await?;
|
let is_limited: Option<LimitsConfiguration> = Instance::is_limited(&urls.api).await?;
|
||||||
let limit_information;
|
let limit_information;
|
||||||
|
|
||||||
|
@ -103,17 +114,9 @@ impl Instance {
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn clone_limits_if_some(&self) -> Option<HashMap<LimitType, Limit>> {
|
|
||||||
if self.limits_information.is_some() {
|
|
||||||
return Some(self.limits_information.as_ref().unwrap().ratelimits.clone());
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new [`Instance`] by trying to get the [relevant instance urls](UrlBundle) from a root url.
|
/// Creates a new [`Instance`] by trying to get the [relevant instance urls](UrlBundle) from a root url.
|
||||||
/// Shorthand for `Instance::new(UrlBundle::from_root_domain(root_domain).await?)`.
|
|
||||||
///
|
///
|
||||||
/// If `limited` is `true`, then Chorus will track and enforce rate limits for this instance.
|
/// Shorthand for `Instance::from_url_bundle(UrlBundle::from_root_domain(root_domain).await?)`.
|
||||||
pub async fn new(root_url: &str) -> ChorusResult<Instance> {
|
pub async fn new(root_url: &str) -> ChorusResult<Instance> {
|
||||||
let urls = UrlBundle::from_root_url(root_url).await?;
|
let urls = UrlBundle::from_root_url(root_url).await?;
|
||||||
Instance::from_url_bundle(urls).await
|
Instance::from_url_bundle(urls).await
|
||||||
|
|
|
@ -71,9 +71,9 @@ pub trait Composite<T: Updateable + Clone + Debug> {
|
||||||
async fn watch_whole(self, gateway: &GatewayHandle) -> Self;
|
async fn watch_whole(self, gateway: &GatewayHandle) -> Self;
|
||||||
|
|
||||||
async fn option_observe_fn(
|
async fn option_observe_fn(
|
||||||
value: Option<Arc<RwLock<T>>>,
|
value: Option<Shared<T>>,
|
||||||
gateway: &GatewayHandle,
|
gateway: &GatewayHandle,
|
||||||
) -> Option<Arc<RwLock<T>>>
|
) -> Option<Shared<T>>
|
||||||
where
|
where
|
||||||
T: Composite<T> + Debug,
|
T: Composite<T> + Debug,
|
||||||
{
|
{
|
||||||
|
@ -86,9 +86,9 @@ pub trait Composite<T: Updateable + Clone + Debug> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn option_vec_observe_fn(
|
async fn option_vec_observe_fn(
|
||||||
value: Option<Vec<Arc<RwLock<T>>>>,
|
value: Option<Vec<Shared<T>>>,
|
||||||
gateway: &GatewayHandle,
|
gateway: &GatewayHandle,
|
||||||
) -> Option<Vec<Arc<RwLock<T>>>>
|
) -> Option<Vec<Shared<T>>>
|
||||||
where
|
where
|
||||||
T: Composite<T>,
|
T: Composite<T>,
|
||||||
{
|
{
|
||||||
|
@ -103,17 +103,14 @@ pub trait Composite<T: Updateable + Clone + Debug> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn value_observe_fn(value: Arc<RwLock<T>>, gateway: &GatewayHandle) -> Arc<RwLock<T>>
|
async fn value_observe_fn(value: Shared<T>, gateway: &GatewayHandle) -> Shared<T>
|
||||||
where
|
where
|
||||||
T: Composite<T>,
|
T: Composite<T>,
|
||||||
{
|
{
|
||||||
gateway.observe(value).await
|
gateway.observe(value).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn vec_observe_fn(
|
async fn vec_observe_fn(value: Vec<Shared<T>>, gateway: &GatewayHandle) -> Vec<Shared<T>>
|
||||||
value: Vec<Arc<RwLock<T>>>,
|
|
||||||
gateway: &GatewayHandle,
|
|
||||||
) -> Vec<Arc<RwLock<T>>>
|
|
||||||
where
|
where
|
||||||
T: Composite<T>,
|
T: Composite<T>,
|
||||||
{
|
{
|
||||||
|
@ -123,17 +120,20 @@ pub trait Composite<T: Updateable + Clone + Debug> {
|
||||||
}
|
}
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IntoShared {
|
||||||
/// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`.
|
/// 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
|
/// [`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
|
/// `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
|
/// entity struct, updating its' held information when new information concerning itself arrives
|
||||||
/// over the [`Gateway`] connection, reducing the need for expensive network-API calls.
|
/// over the [`Gateway`] connection, reducing the need for expensive network-API calls.
|
||||||
fn into_shared(self) -> Shared<Self>
|
fn into_shared(self) -> Shared<Self>;
|
||||||
where
|
}
|
||||||
Self: Sized,
|
|
||||||
{
|
impl<T: Sized> IntoShared for T {
|
||||||
|
fn into_shared(self) -> Shared<Self> {
|
||||||
Arc::new(RwLock::new(self))
|
Arc::new(RwLock::new(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use chorus::gateway::{Gateway, Shared};
|
use chorus::gateway::{Gateway, Shared};
|
||||||
use chorus::types::Composite;
|
use chorus::types::IntoShared;
|
||||||
use chorus::{
|
use chorus::{
|
||||||
instance::{ChorusUser, Instance},
|
instance::{ChorusUser, Instance},
|
||||||
types::{
|
types::{
|
||||||
|
|
|
@ -2,7 +2,7 @@ mod common;
|
||||||
|
|
||||||
use chorus::errors::GatewayError;
|
use chorus::errors::GatewayError;
|
||||||
use chorus::gateway::*;
|
use chorus::gateway::*;
|
||||||
use chorus::types::{self, ChannelModifySchema, Composite, RoleCreateModifySchema, RoleObject};
|
use chorus::types::{self, ChannelModifySchema, IntoShared, RoleCreateModifySchema, RoleObject};
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
@ -95,7 +95,11 @@ async fn test_recursive_self_updating_structs() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Watch role;
|
// Watch role;
|
||||||
bundle.user.gateway.observe(role.into_shared()).await;
|
bundle
|
||||||
|
.user
|
||||||
|
.gateway
|
||||||
|
.observe(role.clone().into_shared())
|
||||||
|
.await;
|
||||||
// Update Guild and check for Guild
|
// Update Guild and check for Guild
|
||||||
let inner_guild = guild.read().unwrap().clone();
|
let inner_guild = guild.read().unwrap().clone();
|
||||||
assert!(inner_guild.roles.is_some());
|
assert!(inner_guild.roles.is_some());
|
||||||
|
@ -107,7 +111,7 @@ async fn test_recursive_self_updating_structs() {
|
||||||
let role_inner = bundle
|
let role_inner = bundle
|
||||||
.user
|
.user
|
||||||
.gateway
|
.gateway
|
||||||
.observe_and_into_inner(role.into_shared())
|
.observe_and_into_inner(role.clone().into_shared())
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(role_inner.name, "yippieee");
|
assert_eq!(role_inner.name, "yippieee");
|
||||||
// Check if the change propagated
|
// Check if the change propagated
|
||||||
|
|
Loading…
Reference in New Issue