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

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

Structs

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

Traits

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

Type Definitions

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

Trait Aliases