Crate rust_observable

Source
Expand description

Work with observables.

The Observable type can be used to model push-based data sources. In addition, observables are:

  • Compositional: Observables can be composed with higher-order combinators.
  • Lazy: Observables do not start emitting data until an observer has subscribed.

This module follows the TC39 Observable proposal. User observers other than Observer can be defined by implementing the AbstractObserver trait.

§Example

use rust_observable::*;

fn my_observable() -> Observable<String> {
    Observable::new(|observer| {
        // send initial data
        observer.next("initial value".into());

        // return a cleanup function that runs on
        // unsubscribe.
        || {
            println!("cleanup on unsubscribe");
        }
    })
}

let _ = my_observable()
    .subscribe(observer! {
        next: |value| {},
        error: |error| {},
        complete: || {},
        start: |subscription| {},
    })
    .unsubscribe();

// you can also use functional methods such as `filter` and `map`.
let _ = my_observable()
    .filter(|value| true)
    .map(|value| value);

You can directly construct an Observable from a list of values:

Observable::from(["red", "green", "blue"])
    .subscribe(observer! {
        next: |color| {
            println!("{}", color);
        },
    });

Macros§

observer
The observer! macro constructs an Observer by allowing you to omit any of the listeners and not needing to box them explictly.

Structs§

Observable
An Observable represents a sequence of values which may be observed.
Observer
An Observer is used to receive data from an Observable, and is supplied as an argument to subscribe.
Subscription
A Subscription is returned by subscribe.
SubscriptionObserver
A SubscriptionObserver wraps the observer object supplied to subscribe.

Traits§

AbstractObserver
An AbstractObserver is used to receive data from an Observable, and is supplied as an argument to subscribe in boxed form.

Type Aliases§

BoxedObserver
The BoxedObserver type represents an abstract observer into a box.

Trait Aliases§

ObserverStartFunction
SubscriberFunction
SubscriptionCleanupFunction