Skip to main content

Observable

Trait Observable 

Source
pub trait Observable<T> {
    // Required methods
    fn source_id(&self) -> SourceId;
    fn source_name(&self) -> Arc<str> ⓘ;
    fn get(&self) -> T;
    fn subscribe(&self, observer: Observer) -> Subscription;

    // Provided methods
    fn read(&self, reader: &mut dyn FnMut(&T)) { ... }
    fn value_version(&self) -> Option<u64> { ... }
    fn changed(&self) -> Changed ⓘ
       where Self: Sized { ... }
}
Expand description

Readable value that can notify subscribers after meaningful changes.

SUI’s runtime consumes this trait, so applications may provide adapters for reducer stores, channels, actor snapshots, or other state architectures.

The simplest custom implementation keeps a Signal<u64> revision next to its data, calls Signal::mark_changed after each change, and delegates source_id, source_name, subscribe, and value_version to it. That gives it batching and observer pruning for free.

Required Methods§

Source

fn source_id(&self) -> SourceId

Source

fn source_name(&self) -> Arc<str> ⓘ

Source

fn get(&self) -> T

Source

fn subscribe(&self, observer: Observer) -> Subscription

Provided Methods§

Source

fn read(&self, reader: &mut dyn FnMut(&T))

Call reader once with a borrow of the current value.

The default clones through get. Implementations that store the value should override it to avoid the copy; selectors read their source through this method.

Source

fn value_version(&self) -> Option<u64>

A counter identifying the current value, or None if unknown.

Selectors use it to reuse a derived value while the source has not changed. It must increase after every change to the value, so that a version read before read never claims a newer value than the one read. Return None if that cannot be guaranteed.

Source

fn changed(&self) -> Changed ⓘ
where Self: Sized,

A future that resolves with the next change notification after this call. It subscribes immediately, so a change made before the first poll is not missed. It needs no particular executor.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Observable<u64> for RichDocumentModel

Source§

impl<K, T> Observable<u64> for VirtualCollectionModel<K, T>
where K: Clone + Eq + Hash + 'static, T: Clone + PartialEq + 'static,

Source§

impl<S, I, O> Observable<O> for Selector<S, I, O>
where S: Observable<I> + Send + Sync + 'static, I: Send + Sync + 'static, O: Clone + PartialEq + Send + Sync + 'static,

Source§

impl<SA, A, SB, B, SC, C, SD, D> Observable<(A, B, C, D)> for Zip<(SA, SB, SC, SD)>
where SA: Observable<A>, SB: Observable<B>, SC: Observable<C>, SD: Observable<D>,

Source§

impl<SA, A, SB, B, SC, C> Observable<(A, B, C)> for Zip<(SA, SB, SC)>
where SA: Observable<A>, SB: Observable<B>, SC: Observable<C>,

Source§

impl<SA, A, SB, B> Observable<(A, B)> for Zip<(SA, SB)>
where SA: Observable<A>, SB: Observable<B>,

Source§

impl<T, E> Observable<TaskState<T, E>> for Task<T, E>
where T: Clone + 'static, E: Clone + 'static,

Source§

impl<T> Observable<T> for Signal<T>
where T: Clone + 'static,