Skip to main content

cratestack_sql/descriptor/
subscribed.rs

1//! `@@subscribe` on a model descriptor: whether the model's events are
2//! notified to the subscription backplane
3//! (`docs/design/subscriptions-websocket.md` ยง4.3).
4
5use super::ModelDescriptor;
6
7/// Where a write path enqueues a model event: the model's name, and whether
8/// the event must also be notified to subscribers (`@@subscribe`). A model
9/// that only `@@emit`s skips the notification, and with it Postgres' commit
10/// lock on `NOTIFY`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct EventTarget {
13    pub model: &'static str,
14    pub notify_subscribers: bool,
15}
16
17impl<M, PK> ModelDescriptor<M, PK> {
18    /// Mark the model `@@subscribe`. A separate step from
19    /// [`ModelDescriptor::new`] so descriptors built without it keep
20    /// compiling unchanged.
21    pub const fn with_subscribed(self) -> Self {
22        Self {
23            subscribed: true,
24            ..self
25        }
26    }
27
28    pub const fn event_target(&self) -> EventTarget {
29        EventTarget {
30            model: self.schema_name,
31            notify_subscribers: self.subscribed,
32        }
33    }
34}