fixed session

This commit is contained in:
2026-03-30 13:39:13 +02:00
parent ec984ffaa3
commit a853de031d
2 changed files with 9 additions and 4 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
use std::pin::Pin; use std::{pin::Pin, sync::Arc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -9,7 +9,7 @@ pub mod ws;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
pub type BoxFuture<'a, T = Option<(bool, serde_json::Value)>> = pub type BoxFuture<'a, T = Option<(bool, serde_json::Value)>> =
Pin<Box<dyn Future<Output = T> + Send + 'a>>; Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub type MethodHandler = Box<dyn Fn(u32, serde_json::Value) -> BoxFuture<'static> + Send + Sync>; pub type MethodHandler = Arc<dyn Fn(u32, serde_json::Value) -> BoxFuture<'static> + Send + Sync>;
pub trait Method { pub trait Method {
const NAME: &'static str; const NAME: &'static str;
+7 -2
View File
@@ -87,7 +87,12 @@ impl Session {
match msg { match msg {
Message::Request { id, method, data } => { Message::Request { id, method, data } => {
if let Some(m) = s.methods.lock().await.get(&method) { let handler = {
let methods = s.methods.lock().await;
methods.get(&method).cloned()
};
if let Some(m) = handler {
if let Some((err, res)) = (m)(id, data).await { if let Some((err, res)) = (m)(id, data).await {
if err { if err {
s.respond_error(id, res) s.respond_error(id, res)
@@ -157,7 +162,7 @@ impl Session {
self.methods.lock().await.insert( self.methods.lock().await.insert(
M::NAME.to_string(), M::NAME.to_string(),
Box::new(move |id, value| { Arc::new(move |id, value| {
let handler = Arc::clone(&handler); let handler = Arc::clone(&handler);
Box::pin(async move { Box::pin(async move {