fixed session
This commit is contained in:
+2
-2
@@ -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
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user