rx_rust/operators/utility/do_after_disposal.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 when the subscription is disposed.
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_disposal::DoAfterDisposal,
20/// },
21/// };
22/// use std::sync::{Arc, Mutex};
23///
24/// let disposed = Arc::new(Mutex::new(false));
25/// let disposed_observer = Arc::clone(&disposed);
26///
27/// let subscription = DoAfterDisposal::new(
28/// FromIter::new(vec![1, 2]),
29/// move || *disposed_observer.lock().unwrap() = true,
30/// )
31/// .subscribe_with_callback(
32/// |_value| {},
33/// |_termination| {},
34/// );
35///
36/// drop(subscription);
37///
38/// assert!(*disposed.lock().unwrap());
39/// ```
40#[derive(Educe)]
41#[educe(Debug, Clone)]
42pub struct DoAfterDisposal<OE, F> {
43 source: OE,
44 callback: F,
45}
46
47impl<OE, F> DoAfterDisposal<OE, F> {
48 pub fn new<'or, 'sub, T, E>(source: OE, callback: F) -> Self
49 where
50 OE: Observable<'or, 'sub, T, E>,
51 F: FnOnce(),
52 {
53 Self { source, callback }
54 }
55}
56
57impl<'or, 'sub, T, E, OE, F> Observable<'or, 'sub, T, E> for DoAfterDisposal<OE, F>
58where
59 T: 'or,
60 E: 'or,
61 OE: Observable<'or, 'sub, T, E>,
62 F: FnOnce() + NecessarySendSync + 'sub,
63{
64 fn subscribe(self, observer: impl Observer<T, E> + NecessarySendSync + 'or) -> Subscription<'sub> {
65 self.source
66 .hook_on_subscription(move |observable, observer| {
67 observable.subscribe(observer)
68 + Subscription::new_with_disposal_callback(self.callback)
69 })
70 .subscribe(observer)
71 }
72}