rx_rust/operators/utility/
do_before_disposal.rs1use 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#[derive(Educe)]
37#[educe(Debug, Clone)]
38pub struct DoBeforeDisposal<OE, F> {
39 source: OE,
40 callback: F,
41}
42
43impl<OE, F> DoBeforeDisposal<OE, F> {
44 pub fn new<'or, 'sub, T, E>(source: OE, callback: F) -> Self
45 where
46 OE: Observable<'or, 'sub, T, E>,
47 F: FnOnce(),
48 {
49 Self { source, callback }
50 }
51}
52
53impl<'or, 'sub, T, E, OE, F> Observable<'or, 'sub, T, E> for DoBeforeDisposal<OE, F>
54where
55 T: 'or,
56 E: 'or,
57 OE: Observable<'or, 'sub, T, E>,
58 F: FnOnce() + NecessarySendSync + 'sub,
59{
60 fn subscribe(self, observer: impl Observer<T, E> + NecessarySendSync + 'or) -> Subscription<'sub> {
61 self.source
62 .hook_on_subscription(move |observable, observer| {
63 Subscription::new_with_disposal_callback(self.callback)
64 + observable.subscribe(observer)
65 })
66 .subscribe(observer)
67 }
68}