use std::collections::HashMap;
use futures::future::BoxFuture;
use crate::core::engine::FrozenDiContainer;
use crate::realtime::connection::WsClient;
use crate::web::Error;
pub trait ArclyGateway: Send + Sync + 'static {
fn on_connect(&self, client: WsClient) -> impl std::future::Future<Output = ()> + Send {
let _ = client;
async {}
}
fn on_disconnect(&self, client: WsClient) -> impl std::future::Future<Output = ()> + Send {
let _ = client;
async {}
}
}
pub type LifecycleHook = Box<dyn Fn(WsClient) -> BoxFuture<'static, ()> + Send + Sync>;
pub type MessageHandler = std::sync::Arc<
dyn Fn(WsClient, std::sync::Arc<str>) -> BoxFuture<'static, Result<(), Error>> + Send + Sync,
>;
pub struct GatewayRuntime {
pub path: &'static str,
pub on_connect: LifecycleHook,
pub on_disconnect: LifecycleHook,
pub dispatch: HashMap<&'static str, MessageHandler>,
}
impl GatewayRuntime {
#[inline]
pub fn handler(&self, event: &str) -> Option<&MessageHandler> {
self.dispatch.get(event)
}
}
pub struct GatewayDescriptor {
pub name: &'static str,
pub path: &'static str,
pub build: fn(&'static FrozenDiContainer) -> &'static GatewayRuntime,
}
inventory::collect!(&'static GatewayDescriptor);