use async_trait::async_trait;
#[async_trait]
pub trait SubscriptionLifecycle: Send + Sync + 'static {
async fn on_connect(
&self,
_params: &serde_json::Value,
_connection_id: &str,
) -> Result<(), String> {
Ok(())
}
async fn on_disconnect(&self, _connection_id: &str) {}
async fn on_subscribe(
&self,
_subscription_name: &str,
_variables: &serde_json::Value,
_connection_id: &str,
) -> Result<(), String> {
Ok(())
}
async fn on_unsubscribe(&self, _subscription_id: &str, _connection_id: &str) {}
}
pub struct NoopLifecycle;
#[async_trait]
impl SubscriptionLifecycle for NoopLifecycle {}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn noop_lifecycle_accepts_connect() {
let lifecycle = NoopLifecycle;
let result = lifecycle.on_connect(&serde_json::json!({}), "conn-1").await;
assert!(result.is_ok(), "noop lifecycle should accept any connection");
}
#[tokio::test]
async fn noop_lifecycle_accepts_subscribe() {
let lifecycle = NoopLifecycle;
let result = lifecycle.on_subscribe("orderCreated", &serde_json::json!({}), "conn-1").await;
assert!(result.is_ok(), "noop lifecycle should accept any subscription");
}
}