rx_rust/operators/transforming/
window.rs

1use crate::utils::types::{Mutable, NecessarySendSync, Shared};
2use crate::{
3    disposable::subscription::Subscription,
4    observable::Observable,
5    observer::{Observer, Termination},
6    subject::{publish_subject::PublishSubject, subject_observable::SubjectObservable},
7    utils::unsub_after_termination::subscribe_unsub_after_termination,
8};
9use crate::{safe_lock, safe_lock_observer, safe_lock_option_observer};
10use educe::Educe;
11
12/// Periodically subdivides items from an Observable into Observable windows, each window being emitted when a `boundary` Observable emits an item.
13/// See <https://reactivex.io/documentation/operators/window.html>
14///
15/// # Examples
16/// ```rust
17/// use rx_rust::{
18///     disposable::subscription::Subscription,
19///     observable::observable_ext::ObservableExt,
20///     observer::{Observer, Termination},
21///     operators::transforming::window::Window,
22///     subject::publish_subject::PublishSubject,
23/// };
24/// use std::{convert::Infallible, sync::{Arc, Mutex}};
25///
26/// let windows = Arc::new(Mutex::new(Vec::<Vec<i32>>::new()));
27/// let terminations = Arc::new(Mutex::new(Vec::new()));
28/// let inner_subscriptions = Arc::new(Mutex::new(Vec::<Subscription>::new()));
29///
30/// let mut source: PublishSubject<'_, i32, Infallible> = PublishSubject::default();
31/// let mut boundary: PublishSubject<'_, (), Infallible> = PublishSubject::default();
32/// let windows_observer = Arc::clone(&windows);
33/// let terminations_observer = Arc::clone(&terminations);
34/// let inner_subscriptions_observer = Arc::clone(&inner_subscriptions);
35///
36/// let subscription = Window::new(source.clone(), boundary.clone()).subscribe_with_callback(
37///     move |window| {
38///         let index = {
39///             let mut windows = windows_observer.lock().unwrap();
40///             windows.push(Vec::new());
41///             windows.len() - 1
42///         };
43///         let windows_for_values = Arc::clone(&windows_observer);
44///         let sub = window.subscribe_with_callback(
45///             move |value| {
46///                 windows_for_values.lock().unwrap()[index].push(value);
47///             },
48///             |_| {},
49///         );
50///         inner_subscriptions_observer.lock().unwrap().push(sub);
51///     },
52///     move |termination| terminations_observer
53///         .lock()
54///         .unwrap()
55///         .push(termination),
56/// );
57///
58/// source.on_next(1);
59/// source.on_next(2);
60/// boundary.on_next(());
61/// source.on_next(3);
62/// source.on_termination(Termination::Completed);
63/// drop(subscription);
64/// inner_subscriptions.lock().unwrap().drain(..).for_each(drop);
65///
66/// assert_eq!(
67///     &*windows.lock().unwrap(),
68///     &[vec![1, 2], vec![3]]
69/// );
70/// assert_eq!(
71///     &*terminations.lock().unwrap(),
72///     &[Termination::Completed]
73/// );
74/// ```
75#[derive(Educe)]
76#[educe(Debug, Clone)]
77pub struct Window<OE, OE1> {
78    source: OE,
79    boundary: OE1,
80}
81
82impl<OE, OE1> Window<OE, OE1> {
83    pub fn new<'or, 'sub, T, E>(source: OE, boundary: OE1) -> Self
84    where
85        OE: Observable<'or, 'sub, T, E>,
86        OE1: Observable<'or, 'sub, (), E>,
87    {
88        Self { source, boundary }
89    }
90}
91
92impl<'or, 'sub, T, E, OE, OE1>
93    Observable<'or, 'sub, SubjectObservable<PublishSubject<'or, T, E>>, E> for Window<OE, OE1>
94where
95    T: Clone + NecessarySendSync + 'or,
96    E: Clone + NecessarySendSync + 'or,
97    OE: Observable<'or, 'sub, T, E>,
98    OE1: Observable<'or, 'sub, (), E>,
99    'sub: 'or,
100{
101    fn subscribe(
102        self,
103        observer: impl Observer<SubjectObservable<PublishSubject<'or, T, E>>, E> + NecessarySendSync + 'or,
104    ) -> Subscription<'sub> {
105        subscribe_unsub_after_termination(observer, |mut observer| {
106            let subject = PublishSubject::default();
107            observer.on_next(SubjectObservable::new(subject.clone()));
108
109            let subject = Shared::new(Mutable::new(subject));
110            let observer = Shared::new(Mutable::new(Some(observer)));
111            let window_observer = WindowObserver {
112                observer: observer.clone(),
113                subject: subject.clone(),
114            };
115            let boundary_observer = BoundaryObserver { observer, subject };
116            let subscription_1 = self.boundary.subscribe(boundary_observer);
117            let subscription_2 = self.source.subscribe(window_observer);
118            subscription_1 + subscription_2
119        })
120    }
121}
122
123struct WindowObserver<'or, T, E, OR> {
124    observer: Shared<Mutable<Option<OR>>>,
125    subject: Shared<Mutable<PublishSubject<'or, T, E>>>,
126}
127
128impl<'or, T, E, OR> Observer<T, E> for WindowObserver<'or, T, E, OR>
129where
130    T: Clone,
131    E: Clone,
132    OR: Observer<SubjectObservable<PublishSubject<'or, T, E>>, E>,
133{
134    fn on_next(&mut self, value: T) {
135        safe_lock_observer!(on_next: self.subject, value);
136    }
137
138    fn on_termination(self, termination: Termination<E>) {
139        safe_lock!(clone: self.subject).on_termination(termination.clone());
140        safe_lock_option_observer!(on_termination: self.observer, termination);
141    }
142}
143
144struct BoundaryObserver<'or, T, E, OR> {
145    observer: Shared<Mutable<Option<OR>>>,
146    subject: Shared<Mutable<PublishSubject<'or, T, E>>>,
147}
148
149impl<'or, T, E, OR> Observer<(), E> for BoundaryObserver<'or, T, E, OR>
150where
151    T: Clone,
152    E: Clone,
153    OR: Observer<SubjectObservable<PublishSubject<'or, T, E>>, E>,
154{
155    fn on_next(&mut self, _: ()) {
156        let new_subject = PublishSubject::default();
157        let old_subject = safe_lock!(mem_replace: self.subject, new_subject.clone());
158        old_subject.on_termination(Termination::Completed);
159        safe_lock_option_observer!(on_next: self.observer, SubjectObservable::new(new_subject));
160    }
161
162    fn on_termination(self, termination: Termination<E>) {
163        safe_lock!(clone: self.subject).on_termination(termination.clone());
164        safe_lock_option_observer!(on_termination: self.observer, termination);
165    }
166}