Compare commits

...

3 Commits

Author SHA1 Message Date
a606db5cb0
snapshot! 2024-06-14 01:34:36 +08:00
9002103c7b
试试新的共享方式,虽然略微麻烦,但至少比之前全局共享好 2024-06-14 01:32:23 +08:00
7d2707b35e
添加macro 2024-06-14 01:19:25 +08:00
2 changed files with 90 additions and 10 deletions

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,36 @@ 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);
}
}
#[derive(Debug, Clone)]
pub struct BotStatusSnapshot {
user_id: UserId,
}

View File

@ -28,16 +28,13 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ICA_VERSION: &str = "1.6.0"; pub const ICA_VERSION: &str = "1.6.0";
pub const TAILCHAT_VERSION: &str = "1.1.0"; pub const TAILCHAT_VERSION: &str = "1.1.0";
/// usage: macro_rules! async_callback_with_state {
/// #[derive(Clone)] ($f:expr, $state:expr) => {{
/// struct BotState(String); use futures_util::FutureExt;
/// let state = $state.clone();
/// async fn some_event_with_state(payload: Payload, client: Client, state: Arc<BotState>) { move |payload: Payload, client: Client| $f(payload, client, state.clone()).boxed()
/// // do something }};
/// }
// macro_rules! wrap_callback_with_state {
// ($f:expr, $state:tt) => {};
// }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -104,3 +101,50 @@ async fn main() {
info!("Disconnected"); info!("Disconnected");
} }
#[allow(dead_code, unused_variables)]
#[tokio::test]
async fn test_macro() {
use std::sync::Arc;
use tokio::sync::RwLock;
use rust_socketio::asynchronous::{Client, ClientBuilder};
use rust_socketio::Payload;
/// 一个简单的例子
#[derive(Clone)]
struct BotState(String);
/// 一个复杂一些的例子
#[derive(Clone)]
struct BotState2 {
pub name: Arc<RwLock<String>>,
}
async fn some_event_with_state(payload: Payload, client: Client, state: Arc<BotState>) {
// do something with your state
}
async fn some_state_change_event(payload: Payload, client: Client, state: Arc<BotState2>) {
if let Payload::Text(text) = payload {
if let Some(first_one) = text.first() {
let new_name = first_one.as_str().unwrap_or_default();
let old_name = state.name.read().await;
if new_name != *old_name {
// update your name here
*state.name.write().await = new_name.to_string();
}
}
}
}
let state = Arc::new(BotState("hello".to_string()));
let state2 = Arc::new(BotState2 {
name: Arc::new(RwLock::new("hello".to_string())),
});
let socket = ClientBuilder::new("http://example.com")
.on("message", async_callback_with_state!(some_event_with_state, state))
.on("update_status", async_callback_with_state!(some_state_change_event, state2))
.connect()
.await;
}