ferroui_dynamic_data/cache/ops/switch.rs
1//! `Switch` (`Cache/ObservableCacheEx.Switch.cs`): the change sets of the latest of a sequence of
2//! caches.
3//!
4//! Deviation (DEVIATIONS.md, DynamicData): the sources are a sequence of
5//! [`CacheChangeSetSource`]s, so that the overloads on a sequence of caches
6//! (`IObservable<IObservableCache>`) and on a sequence of sequences of change sets
7//! (`IObservable<IObservable<IChangeSet>>`) are one method of [`SwitchCacheEx`]. Naming: it is
8//! `switch_cache`, as `switch` would hide `ObservableExt::switch`.
9
10use super::combine::CacheChangeSetSource;
11use crate::cache::internal::switch_cache_change_sets;
12use crate::cache::ChangeSet;
13use ferroui_reactive::ObservableExt;
14use ferroui_system::IObservable;
15use std::rc::Rc;
16
17/// `Switch` of a sequence of caches or of sequences of change sets.
18pub trait SwitchCacheEx {
19 /// The type of the items.
20 type Item: Clone + 'static;
21 /// The type of the keys.
22 type Key: Eq + std::hash::Hash + Clone + 'static;
23
24 /// The change sets of the latest source: each new source removes the items of the previous
25 /// one, then delivers its own; the result completes when the sequence and the latest source
26 /// have completed, and an error of either ends it (`Switch(IObservable<IObservableCache>)`,
27 /// `Switch(IObservable<IObservable<IChangeSet>>)`).
28 fn switch_cache(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>;
29}
30
31impl<S: CacheChangeSetSource> SwitchCacheEx for Rc<dyn IObservable<S>> {
32 type Item = S::Item;
33 type Key = S::Key;
34
35 fn switch_cache(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>> {
36 // `sources.Select(cache => cache.Connect()).Switch()` for the caches.
37 switch_cache_change_sets(self.select(|source: S| source.source_change_sets()))
38 }
39}