operator_tap
The tap operator lets you forward upstream values to a destination observer.
The destination could be anything, a
PrintObserver
to log upstream values to the console, or even a subject to trigger another
pipeline.
Good to know
- Keep in mind that the destination observer passed in will not get
upgraded1, meaning a tap operator will never call
unsubscribeon the destination, even if it’s a subscriber that upgrades to itself and has anunsubscribeimplementation. However, the error and complete signals are forwarded. If you want to avoid forwardingerrorandcomplete, usetap_nextinstead.
See Also
- TapNextOperator -
Run a callback for each
nextvalue while letting signals pass through. - OnNextOperator - Invoke a callback for each value that can also decide whether to forward it.
- OnSubscribeOperator - Run a callback when a subscription is established.
- FinalizeOperator - Execute cleanup when the observable finishes or unsubscribes.
Example
cargo run -p rx_core --example operator_tap_example
(1..=3)
.into_observable()
.tap(PrintObserver::new("tap_destination"))
.subscribe(PrintObserver::new("tap_operator"));
Output:
tap_destination - next: 1
tap_operator - next: 1
tap_destination - next: 2
tap_operator - next: 2
tap_destination - next: 3
tap_operator - next: 3
tap_destination - completed
tap_operator - completed
tap_operator - unsubscribed
-
Documentation on UpgradeableObserver ↩