use crate::notification_backend::NotificationMessage;
use chrono::{DateTime, Utc};
use serde_json::Value;
#[derive(Debug, Clone)]
pub enum SseEventType {
LiveNotification,
ReplayNotification,
Heartbeat,
ConnectionClosing,
Error,
ReplayControl,
}
impl SseEventType {
pub fn as_str(&self) -> &'static str {
match self {
SseEventType::LiveNotification => "live-notification",
SseEventType::ReplayNotification => "replay",
SseEventType::Heartbeat => "heartbeat",
SseEventType::ConnectionClosing => "connection-closing",
SseEventType::Error => "error",
SseEventType::ReplayControl => "replay-control",
}
}
}
pub fn format_sse_event(event_type: SseEventType, data: Value) -> String {
format!("event: {}\ndata: {}\n\n", event_type.as_str(), data)
}
#[derive(Debug, Clone)]
pub(crate) enum DeliveryKind {
Live,
Replay,
}
#[derive(Debug, Clone)]
pub(crate) enum CloseReason {
ServerShutdown,
MaxDurationReached,
EndOfStream,
}
#[derive(Debug, Clone)]
pub(crate) enum ControlEvent {
ConnectionEstablished {
topic: String,
timestamp: DateTime<Utc>,
connection_will_close_in_seconds: u64,
request_id: String,
},
ReplayStarted {
topic: String,
from_sequence: Option<u64>,
from_date: Option<DateTime<Utc>>,
batch_size: usize,
timestamp: DateTime<Utc>,
request_id: String,
},
ReplayCompleted {
topic: String,
timestamp: DateTime<Utc>,
},
ReplayLimitReached {
topic: String,
max_allowed: usize,
timestamp: DateTime<Utc>,
},
}
#[derive(Debug, Clone)]
pub(crate) enum StreamFrame {
Notification {
notification: NotificationMessage,
kind: DeliveryKind,
},
Control(ControlEvent),
Heartbeat {
topic: String,
timestamp: DateTime<Utc>,
},
Error {
topic: String,
message: String,
request_id: String,
},
Close {
topic: String,
reason: CloseReason,
timestamp: DateTime<Utc>,
request_id: String,
},
}