ferroui_dynamic_data/cache/
sorted_observable_cache_ex.rs1use super::testing::TypedChangeSetAggregator;
11use super::{ChangeSetWithContext, ISortedChangeSet, SortedChangeSet};
12use ferroui_reactive::ObservableExt;
13use ferroui_system::IObservable;
14use std::any::Any;
15use std::hash::Hash;
16use std::rc::Rc;
17
18pub trait SortedObservableCacheEx<T: Clone + 'static, K: Eq + Hash + Clone + 'static, C: ISortedChangeSet<T, K>> {
21 fn sorted_source(&self) -> Rc<dyn IObservable<C>>;
23
24 fn sorted_change_sets(&self) -> Rc<dyn IObservable<SortedChangeSet<T, K>>>;
27
28 fn as_sorted_aggregator(&self) -> TypedChangeSetAggregator<T, K, C> {
33 TypedChangeSetAggregator::new(self.sorted_source())
34 }
35
36 super::ops::treat_moves_as_remove_add::treat_moves_as_remove_add_methods!();
37 super::ops::update_index::update_index_methods!();
38 super::ops::virtualise_and_page::virtualise_and_page_sorted_methods!();
39}
40
41impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static, C: ISortedChangeSet<T, K>> SortedObservableCacheEx<T, K, C> for Rc<dyn IObservable<C>> {
42 fn sorted_source(&self) -> Rc<dyn IObservable<C>> {
43 self.clone()
44 }
45
46 fn sorted_change_sets(&self) -> Rc<dyn IObservable<SortedChangeSet<T, K>>> {
47 if let Some(sorted) = (self as &dyn Any).downcast_ref::<Rc<dyn IObservable<SortedChangeSet<T, K>>>>() {
48 return sorted.clone();
49 }
50 self.select(|changes: C| changes.to_sorted_change_set())
51 }
52}
53
54pub trait ContextObservableCacheEx<T: Clone + 'static, K: Eq + Hash + Clone + 'static, X: Clone + 'static> {
57 fn as_context_aggregator(&self) -> TypedChangeSetAggregator<T, K, ChangeSetWithContext<T, K, X>>;
60}
61
62impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static, X: Clone + 'static> ContextObservableCacheEx<T, K, X> for Rc<dyn IObservable<ChangeSetWithContext<T, K, X>>> {
63 fn as_context_aggregator(&self) -> TypedChangeSetAggregator<T, K, ChangeSetWithContext<T, K, X>> {
64 TypedChangeSetAggregator::new(self.clone())
65 }
66}