use super::testing::TypedChangeSetAggregator;
use super::{ChangeSetWithContext, ISortedChangeSet, SortedChangeSet};
use ferroui_reactive::ObservableExt;
use ferroui_system::IObservable;
use std::any::Any;
use std::hash::Hash;
use std::rc::Rc;
pub trait SortedObservableCacheEx<T: Clone + 'static, K: Eq + Hash + Clone + 'static, C: ISortedChangeSet<T, K>> {
fn sorted_source(&self) -> Rc<dyn IObservable<C>>;
fn sorted_change_sets(&self) -> Rc<dyn IObservable<SortedChangeSet<T, K>>>;
fn as_sorted_aggregator(&self) -> TypedChangeSetAggregator<T, K, C> {
TypedChangeSetAggregator::new(self.sorted_source())
}
super::ops::treat_moves_as_remove_add::treat_moves_as_remove_add_methods!();
super::ops::update_index::update_index_methods!();
super::ops::virtualise_and_page::virtualise_and_page_sorted_methods!();
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static, C: ISortedChangeSet<T, K>> SortedObservableCacheEx<T, K, C> for Rc<dyn IObservable<C>> {
fn sorted_source(&self) -> Rc<dyn IObservable<C>> {
self.clone()
}
fn sorted_change_sets(&self) -> Rc<dyn IObservable<SortedChangeSet<T, K>>> {
if let Some(sorted) = (self as &dyn Any).downcast_ref::<Rc<dyn IObservable<SortedChangeSet<T, K>>>>() {
return sorted.clone();
}
self.select(|changes: C| changes.to_sorted_change_set())
}
}
pub trait ContextObservableCacheEx<T: Clone + 'static, K: Eq + Hash + Clone + 'static, X: Clone + 'static> {
fn as_context_aggregator(&self) -> TypedChangeSetAggregator<T, K, ChangeSetWithContext<T, K, X>>;
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static, X: Clone + 'static> ContextObservableCacheEx<T, K, X> for Rc<dyn IObservable<ChangeSetWithContext<T, K, X>>> {
fn as_context_aggregator(&self) -> TypedChangeSetAggregator<T, K, ChangeSetWithContext<T, K, X>> {
TypedChangeSetAggregator::new(self.clone())
}
}