#observable #data-source #observer #subscription #push-based #cleanup #subscribe

nightly rust_observable

Push-based data source Observable type

5 releases

0.2.1 Aug 5, 2023
0.2.0 Aug 5, 2023
0.1.2 Aug 5, 2023
0.1.1 Aug 4, 2023
0.1.0 Aug 4, 2023

#18 in #subscribe

MIT/Apache

19KB
380 lines

Observable

Provides an Observable type used to model push-based data sources. It always uses atomic references, allowing it to be passed to other threads. It is based on a TC39 proposal.

The observer! macro constructs an opaque Observer. You can also implement your own observer type if desired.

Requirements:

  • The Rust standard library (std).
  • Nightly channel.
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);

Dependencies

~245–700KB
~17K SLoC