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 anObserverby allowing you to omit any of the listeners and not needing to box them explictly.
Structs§
- Observable
- An
Observablerepresents a sequence of values which may be observed. - Observer
- An
Observeris used to receive data from anObservable, and is supplied as an argument tosubscribe. - Subscription
- A
Subscriptionis returned bysubscribe. - Subscription
Observer - A
SubscriptionObserverwraps the observer object supplied tosubscribe.
Traits§
- Abstract
Observer - An
AbstractObserveris used to receive data from anObservable, and is supplied as an argument tosubscribein boxed form.
Type Aliases§
- Boxed
Observer - The
BoxedObservertype represents an abstract observer into a box.