1use std::sync::Arc;
2
3#[derive(Clone)]
4pub struct EventSender {
5 callback: Arc<dyn Fn(&str, serde_json::Value) + Send + Sync>,
6}
7
8impl EventSender {
9 pub fn new<F>(callback: F) -> Self
10 where
11 F: Fn(&str, serde_json::Value) + Send + Sync + 'static,
12 {
13 Self {
14 callback: Arc::new(callback),
15 }
16 }
17
18 pub fn send(&self, event_type: &str, payload: serde_json::Value) {
19 let mut full_payload = payload.clone();
20 if let serde_json::Value::Object(ref mut map) = full_payload {
21 map.insert(
22 "type".to_string(),
23 serde_json::Value::String(event_type.to_string()),
24 );
25 } else {
26 full_payload = serde_json::json!({
27 "type": event_type,
28 "data": payload
29 });
30 }
31 (self.callback)(event_type, full_payload);
32 }
33}