1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! `Clone` (`Cache/ObservableCacheEx.Clone.cs`).
macro_rules! clone_methods {
() => {
/// The change sets, applied to `target` as they pass: an addition adds the item, an update
/// replaces the previous item by it, and a removal removes it (`Clone(ICollection<TObject>)`;
/// named `clone_to` beside `Clone::clone`).
fn clone_to(&self, target: std::rc::Rc<std::cell::RefCell<Vec<T>>>) -> std::rc::Rc<dyn ferroui_system::IObservable<crate::cache::ChangeSet<T, K>>>
where
T: PartialEq,
{
ferroui_reactive::ObservableExt::do_(&self.change_sets(), move |changes: &crate::cache::ChangeSet<T, K>| {
let mut target = target.borrow_mut();
for item in changes {
match item.reason() {
crate::cache::ChangeReason::Add => target.push(item.current().clone()),
crate::cache::ChangeReason::Update => {
if let Some(previous) = item.previous() {
if let Some(index) = target.iter().position(|candidate| candidate == previous) {
target.remove(index);
}
}
target.push(item.current().clone());
}
crate::cache::ChangeReason::Remove => {
if let Some(index) = target.iter().position(|candidate| candidate == item.current()) {
target.remove(index);
}
}
crate::cache::ChangeReason::Refresh | crate::cache::ChangeReason::Moved => {}
}
}
})
}
};
}
pub(crate) use clone_methods;