Skip to content

Commit

Permalink
Merge pull request #25 from null8626/main
Browse files Browse the repository at this point in the history
  • Loading branch information
ffamilyfriendly authored Aug 1, 2024
2 parents 0b2595e + ea61e9e commit a584c01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "topgg"
version = "1.4.1"
version = "1.4.2"
edition = "2021"
authors = ["null (https://github.com/null8626)", "Top.gg <support@top.gg> (https://top.gg)"]
description = "The official Rust wrapper for the Top.gg API"
Expand Down Expand Up @@ -53,4 +53,4 @@ webhook = ["urlencoding"]
rocket = ["webhook", "dep:rocket"]
axum = ["webhook", "async-trait", "serde_json", "dep:axum"]
warp = ["webhook", "async-trait", "dep:warp"]
actix-web = ["webhook", "dep:actix-web"]
actix-web = ["webhook", "dep:actix-web"]
23 changes: 20 additions & 3 deletions src/autoposter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::Stats;
use crate::{Result, Stats};
use core::{
ops::{Deref, DerefMut},
time::Duration,
};
use std::sync::Arc;
use tokio::{
sync::{RwLock, RwLockWriteGuard, Semaphore},
sync::{mpsc, RwLock, RwLockWriteGuard, Semaphore},
task::{spawn, JoinHandle},
time::sleep,
};
Expand Down Expand Up @@ -131,6 +131,7 @@ pub trait Handler: Send + Sync + 'static {
pub struct Autoposter<H> {
handler: Arc<H>,
thread: JoinHandle<()>,
receiver: Option<mpsc::UnboundedReceiver<Result<()>>>,
}

impl<H> Autoposter<H>
Expand All @@ -156,6 +157,7 @@ where

let client = client.as_client();
let handler = Arc::new(handler);
let (sender, receiver) = mpsc::unbounded_channel();

Self {
handler: Arc::clone(&handler),
Expand All @@ -166,12 +168,15 @@ where
{
let stats = handler.stats().stats.read().await;

let _ = client.post_stats(&stats).await;
if sender.send(client.post_stats(&stats).await).is_err() {
break;
}
};

sleep(interval).await;
}
}),
receiver: Some(receiver),
}
}

Expand All @@ -180,6 +185,18 @@ where
pub fn handler(&self) -> Arc<H> {
Arc::clone(&self.handler)
}

/// Returns a future that resolves every time the [`Autoposter`] has attempted to post the bot's stats. If you want to use the receiver directly, call [`receiver`].
#[inline(always)]
pub async fn recv(&mut self) -> Option<Result<()>> {
self.receiver.as_mut().expect("receiver is already taken from the receiver() method. please call recv() directly from the receiver.").recv().await
}

/// Takes the receiver responsible for [`recv`]. Subsequent calls to this function and [`recv`] after this call will panic.
#[inline(always)]
pub fn receiver(&mut self) -> mpsc::UnboundedReceiver<Result<()>> {
self.receiver.take().expect("receiver() can only be called once.")
}
}

impl<H> Deref for Autoposter<H> {
Expand Down
6 changes: 4 additions & 2 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,16 @@ util::debug_struct! {
#[derive(Clone, Serialize, Deserialize)]
Stats {
protected {
#[serde(skip_serializing_if = "Option::is_none")]
shard_count: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
server_count: Option<usize>,
}

private {
#[serde(default, deserialize_with = "util::deserialize_default")]
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "util::deserialize_default")]
shards: Option<Vec<usize>>,
#[serde(default, deserialize_with = "util::deserialize_default")]
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "util::deserialize_default")]
shard_id: Option<usize>,
}

Expand Down

0 comments on commit a584c01

Please sign in to comment.