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_behavior

crates.io ci codecov license

Always holds a value that is replayed to late subscribers.

See Also

  • PublishSubject - Forwards observed signals to active subscribers without replaying values, but terminal state is replayed.
  • AsyncSubject - Reduces observed values into one and emits it on completion, replaying the result 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

Run the example with:

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

fn main() {
    let mut subject = BehaviorSubject::<i32>::new(10);

    let mut hello_subscription = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("hello"));

    subject.next(11);

    let _s1 = subject
        .clone()
        .map(|next| next * 2)
        .subscribe(PrintObserver::<i32>::new("hi double"));

    subject.next(12);
    hello_subscription.unsubscribe();
    subject.next(13);
    subject.complete();

    let mut _completed_subscription = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("hello_completed"));
}

Output:

hello - next: 10
hello - next: 11
hi double - next: 22
hi double - next: 24
hello - next: 12
hello - unsubscribed
hi double - next: 26
hi double - completed
hi double - unsubscribed
hello_completed - next: 13
hello_completed - completed
hello_completed - unsubscribed