Skip to main content

zlink_core/
notified.rs

1//! Notified state API traits.
2//!
3//! This module defines traits that document the expected API for notified types. Runtime crates
4//! (like `zlink-tokio` and `zlink-smol`) implement these traits on their concrete types.
5
6use core::{fmt::Debug, future::Future};
7
8use crate::Reply;
9
10/// Trait for a notified state that tracks a value and broadcasts changes.
11///
12/// This is useful for implementing service properties that notify subscribers when they change.
13pub trait State<T, ReplyParams>: Clone
14where
15    T: Into<ReplyParams> + Clone + Debug,
16    ReplyParams: Clone + Send + 'static + Debug,
17{
18    /// The stream type returned by [`stream`](Self::stream).
19    type Stream: futures_util::Stream<Item = Reply<ReplyParams>>;
20
21    /// Create a new notified state with the given initial value.
22    fn new(value: T) -> Self;
23
24    /// Set the value and notify all listeners.
25    fn set(&mut self, value: T) -> impl Future<Output = ()> + Send;
26
27    /// Get the current value.
28    fn get(&self) -> T;
29
30    /// Get a stream of replies for this state.
31    fn stream(&self) -> Self::Stream;
32
33    /// Get a stream of replies for this state, that only yields one reply: the current state.
34    fn stream_once(&self) -> Self::Stream;
35}