Skip to main content

notedthat_write/
sinks.rs

1//! Where a committed write is announced: the indexing queue and, when one is
2//! configured, the object change event log.
3//!
4//! Bundled so every write path takes one argument and every surface names its
5//! [`EventSource`] once, where it builds the bundle, rather than at each call.
6
7use notedthat_core::{EventPublisher, EventSource};
8use notedthat_indexer::{IndexHealth, IndexQueueSender};
9
10/// The two places a durable write is reported to, and on whose behalf.
11#[derive(Clone)]
12pub struct WriteSinks<'a> {
13    /// The in-process indexing queue (D38).
14    pub indexer_tx: IndexQueueSender,
15    /// The event log, when `NOTEDTHAT_EVENTS_BACKEND` selects one.
16    pub events: Option<&'a dyn EventPublisher>,
17    /// Where what happened at the queue — enqueued, refused, or a queue with
18    /// no worker behind it — is recorded for the health view (#97). `None`
19    /// only in tests that have no health view to keep.
20    pub index_health: Option<&'a IndexHealth>,
21    /// The surface making the write, stamped on every event it publishes.
22    pub source: EventSource,
23}
24
25impl<'a> WriteSinks<'a> {
26    /// Both sinks, recording on `index_health`.
27    #[must_use]
28    pub fn new(
29        indexer_tx: &'a IndexQueueSender,
30        events: Option<&'a dyn EventPublisher>,
31        index_health: &'a IndexHealth,
32        source: EventSource,
33    ) -> Self {
34        Self {
35            indexer_tx: indexer_tx.clone(),
36            events,
37            index_health: Some(index_health),
38            source,
39        }
40    }
41
42    /// The indexing queue alone, as every deployment without an events backend
43    /// runs, attributed to the HTTP API, with nothing keeping a health record.
44    #[must_use]
45    pub fn indexer_only(indexer_tx: impl Into<IndexQueueSender>) -> Self {
46        Self {
47            indexer_tx: indexer_tx.into(),
48            events: None,
49            index_health: None,
50            source: EventSource::Http,
51        }
52    }
53}