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_async

crates.io ci codecov license

Reduces observed values into one and emits it to active subscribers once completed. Once completed, it also replays the result to late subscribers.

See Also

  • PublishSubject - Forwards observed signals to active subscribers without replaying values, but terminal state is replayed.
  • 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

Run the example with:

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

fn main() {
    let mut subject = AsyncSubject::<i32>::default();

    let mut _subscription_1 = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("async_subject sub_1"));

    subject.next(1);
    subject.next(2);

    let mut _subscription_2 = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("async_subject sub_2"));

    subject.next(3);
    subject.complete();

    let mut _subscription_3 = subject
        .clone()
        .subscribe(PrintObserver::<i32>::new("async_subject sub_3"));
}

Output:

async_subject sub_1 - next: 3
async_subject sub_2 - next: 3
async_subject sub_1 - completed
async_subject sub_1 - unsubscribed
async_subject sub_2 - completed
async_subject sub_2 - unsubscribed
async_subject sub_3 - next: 3
async_subject sub_3 - completed
async_subject sub_3 - unsubscribed