rx_rust/operators/utility/do_before_next.rs
1use crate::utils::types::NecessarySendSync;
2use crate::{
3 disposable::subscription::Subscription,
4 observable::{Observable, observable_ext::ObservableExt},
5 observer::Observer,
6};
7use educe::Educe;
8
9/// Invokes a callback for each item emitted by the source Observable before the item is emitted to the downstream observer.
10/// See <https://reactivex.io/documentation/operators/do.html>
11///
12/// # Examples
13/// ```rust
14/// use rx_rust::{
15/// observable::observable_ext::ObservableExt,
16/// observer::Termination,
17/// operators::{
18/// creating::from_iter::FromIter,
19/// utility::do_before_next::DoBeforeNext,
20/// },
21/// };
22/// use std::sync::{Arc, Mutex};
23///
24/// let mut values = Vec::new();
25/// let mut terminations = Vec::new();
26/// let side_effects = Arc::new(Mutex::new(Vec::new()));
27/// let side_effects_observer = Arc::clone(&side_effects);
28///
29/// DoBeforeNext::new(FromIter::new(vec![1, 2]), move |value: &i32| {
30/// side_effects_observer.lock().unwrap().push(*value * 10);
31/// })
32/// .subscribe_with_callback(
33/// |value| values.push(value),
34/// |termination| terminations.push(termination),
35/// );
36///
37/// assert_eq!(values, vec![1, 2]);
38/// assert_eq!(terminations, vec![Termination::Completed]);
39/// assert_eq!(&*side_effects.lock().unwrap(), &[10, 20]);
40/// ```
41#[derive(Educe)]
42#[educe(Debug, Clone)]
43pub struct DoBeforeNext<OE, F> {
44 source: OE,
45 callback: F,
46}
47
48impl<OE, F> DoBeforeNext<OE, F> {
49 pub fn new<'or, 'sub, T, E>(source: OE, callback: F) -> Self
50 where
51 OE: Observable<'or, 'sub, T, E>,
52 F: FnMut(&T),
53 {
54 Self { source, callback }
55 }
56}
57
58impl<'or, 'sub, T, E, OE, F> Observable<'or, 'sub, T, E> for DoBeforeNext<OE, F>
59where
60 OE: Observable<'or, 'sub, T, E>,
61 F: FnMut(&T) + NecessarySendSync + 'or,
62{
63 fn subscribe(
64 mut self,
65 observer: impl Observer<T, E> + NecessarySendSync + 'or,
66 ) -> Subscription<'sub> {
67 self.source
68 .hook_on_next(move |observer, value| {
69 (self.callback)(&value);
70 observer.on_next(value);
71 })
72 .subscribe(observer)
73 }
74}