Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

subject_publish

crates.io ci codecov license

Forwards observed signals to all active subscribers. Does not replay values to late subscribers, but always replays terminal state.

See Also

  • AsyncSubject - Reduces observed values into one and emits it on completion, replaying the result to late subscribers.
  • BehaviorSubject - Always holds a value that is replayed to late subscribers.
  • ReplaySubject - Buffers the last N values and replays them to late subscribers.
  • ProvenanceSubject - BehaviorSubject that also stores an additional filtering value to track provenance.

Example

cargo run -p rx_core --example subject_publish_example
use rx_core::prelude::*;

fn main() {
    let mut subject = PublishSubject::<i32>::default();
    subject.next(1);

    let mut subscription = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("subject_example"));
    subject.next(2);
    subject.next(3);
    subscription.unsubscribe();
    subject.next(4);
    subject.complete();

    let _subscription_2 = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("subject_example_2"));
}

Output:

subject_example - next: 2
subject_example - next: 3
subject_example - unsubscribed
subject_example_2 - completed
subject_example_2 - unsubscribed