supabase_client_realtime/
callback.rs1use std::sync::Arc;
2
3use serde_json::Value;
4use tokio::sync::RwLock;
5
6use crate::error::RealtimeError;
7use crate::types::{
8 PostgresChangePayload, PostgresChangesEvent, PresenceMeta, PresenceState,
9 SubscriptionStatus,
10};
11
12pub type PostgresChangesCallback =
15 Arc<dyn Fn(PostgresChangePayload) + Send + Sync + 'static>;
16
17pub type BroadcastCallback = Arc<dyn Fn(Value) + Send + Sync + 'static>;
18
19pub type PresenceSyncCallback =
20 Arc<dyn Fn(&PresenceState) + Send + Sync + 'static>;
21
22pub type PresenceJoinCallback =
23 Arc<dyn Fn(String, Vec<PresenceMeta>) + Send + Sync + 'static>;
24
25pub type PresenceLeaveCallback =
26 Arc<dyn Fn(String, Vec<PresenceMeta>) + Send + Sync + 'static>;
27
28pub type StatusCallback =
29 Arc<dyn Fn(SubscriptionStatus, Option<RealtimeError>) + Send + Sync + 'static>;
30
31pub(crate) enum Binding {
35 PostgresChanges {
36 filter_index: usize,
38 event: PostgresChangesEvent,
40 callback: PostgresChangesCallback,
41 },
42 Broadcast {
43 event: String,
44 callback: BroadcastCallback,
45 },
46 PresenceSync(PresenceSyncCallback),
47 PresenceJoin(PresenceJoinCallback),
48 PresenceLeave(PresenceLeaveCallback),
49}
50
51pub(crate) struct CallbackRegistry {
55 pub bindings: Arc<RwLock<Vec<Binding>>>,
56 pub status_callback: Arc<RwLock<Option<StatusCallback>>>,
57}
58
59impl CallbackRegistry {
60 pub fn new() -> Self {
61 Self {
62 bindings: Arc::new(RwLock::new(Vec::new())),
63 status_callback: Arc::new(RwLock::new(None)),
64 }
65 }
66}