Compare commits

..

No commits in common. "a606db5cb049a2f4aa65f492a9e415a29a81493f" and "5db2978eb5eac8d70ba5f47eaf8c0677fd524dfd" have entirely different histories.

2 changed files with 10 additions and 90 deletions

View File

@ -1,7 +1,4 @@
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use crate::data_struct::tailchat::UserId;
@ -32,36 +29,3 @@ pub struct UpdateDMConverse {
#[serde(rename = "updatedAt")]
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,13 +28,16 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ICA_VERSION: &str = "1.6.0";
pub const TAILCHAT_VERSION: &str = "1.1.0";
macro_rules! async_callback_with_state {
($f:expr, $state:expr) => {{
use futures_util::FutureExt;
let state = $state.clone();
move |payload: Payload, client: Client| $f(payload, client, state.clone()).boxed()
}};
}
/// usage:
/// #[derive(Clone)]
/// struct BotState(String);
///
/// async fn some_event_with_state(payload: Payload, client: Client, state: Arc<BotState>) {
/// // do something
///
// macro_rules! wrap_callback_with_state {
// ($f:expr, $state:tt) => {};
// }
#[tokio::main]
async fn main() {
@ -101,50 +104,3 @@ async fn main() {
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;
}