试试新的共享方式,虽然略微麻烦,但至少比之前全局共享好

This commit is contained in:
shenjack 2024-06-14 01:32:23 +08:00
parent 7d2707b35e
commit 9002103c7b
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,4 +1,7 @@
use std::sync::Arc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use crate::data_struct::tailchat::UserId; use crate::data_struct::tailchat::UserId;
@ -29,3 +32,31 @@ pub struct UpdateDMConverse {
#[serde(rename = "updatedAt")] #[serde(rename = "updatedAt")]
pub updated_at: String, pub updated_at: String,
} }
pub type Writeable<T> = Arc<RwLock<T>>;
#[derive(Debug, Clone)]
pub struct BotStatus {
user_id: Writeable<UserId>,
}
impl BotStatus {
pub fn new(user_id: UserId) -> Self {
Self {
user_id: Arc::new(RwLock::new(user_id)),
}
}
pub async fn get_user_id(&self) -> UserId { self.user_id.read().await.clone() }
pub fn block_get_user_id(&self) -> UserId {
tokio::task::block_in_place(|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(self.get_user_id())
})
}
pub async fn set_user_id(&self, user_id: UserId) {
self.user_id.write().await.clone_from(&user_id);
}
}