rx_rust/operators/utility/
do_after_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 after the item has been 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_after_next::DoAfterNext,
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/// DoAfterNext::new(FromIter::new(vec![1, 2]), move |value| {
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 DoAfterNext<OE, F> {
44    source: OE,
45    callback: F,
46}
47
48impl<OE, F> DoAfterNext<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 DoAfterNext<OE, F>
59where
60    T: Clone,
61    OE: Observable<'or, 'sub, T, E>,
62    F: FnMut(T) + NecessarySendSync + 'or,
63{
64    fn subscribe(
65        mut self,
66        observer: impl Observer<T, E> + NecessarySendSync + 'or,
67    ) -> Subscription<'sub> {
68        self.source
69            .hook_on_next(move |observer, value| {
70                observer.on_next(value.clone());
71                (self.callback)(value);
72            })
73            .subscribe(observer)
74    }
75}