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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod cage;
pub use cage::Cage;
mod bond;
pub use bond::Bond;
mod lotus;
pub use lotus::Lotus;
pub mod scheduler;
pub use scheduler::{batch, schedule};

use std::cell::RefCell;
use std::fmt::{self, Display};
use std::hash::Hash;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};

use indexmap::{IndexMap, IndexSet};

#[cfg(not(feature = "__single_holder"))]
use crate::HolderId;
use crate::ViewId;

thread_local! {
    #[cfg(feature = "__single_holder")]
    pub(crate) static REVISING_ITEMS: RefCell<IndexMap<RevisableId, Box<dyn Revisable>>> = RefCell::default();
    #[cfg(not(feature = "__single_holder"))]
    pub(crate) static REVISING_ITEMS: RefCell<IndexMap<HolderId, IndexMap<RevisableId, Box<dyn Revisable>>>> = RefCell::default();

    #[cfg(feature = "__single_holder")]
    pub(crate) static PENDING_ITEMS: RefCell<IndexMap<RevisableId, Box<dyn Revisable>>> = RefCell::default();
    #[cfg(not(feature = "__single_holder"))]
    pub(crate) static PENDING_ITEMS: RefCell<IndexMap<HolderId, IndexMap<RevisableId, Box<dyn Revisable>>>> = RefCell::default();

    pub(crate) static TRACKING_STACK: RefCell<TrackingStack> = RefCell::new(TrackingStack::new());
}

#[derive(Default)]
pub(crate) struct TrackingStack {
    pub layers: Vec<IndexMap<RevisableId, Box<dyn Revisable>>>,
}
impl TrackingStack {
    pub(crate) fn new() -> Self {
        Self { layers: Default::default() }
    }
    pub(crate) fn is_idle(&self) -> bool {
        self.layers.is_empty()
    }
    pub(crate) fn push_layer(&mut self) {
        self.layers.push(Default::default());
    }
    pub(crate) fn pop_layer(&mut self) -> Option<IndexMap<RevisableId, Box<dyn Revisable>>> {
        self.layers.pop()
    }
    pub(crate) fn track(&mut self, item: impl Into<Box<dyn Revisable>>) {
        let item = item.into();
        for layer in &mut self.layers {
            layer.insert(item.id(), item.clone_boxed());
        }
    }
}

pub fn gather<R>(func: impl FnOnce() -> R) -> (IndexMap<RevisableId, Box<dyn Revisable>>, R) {
    TRACKING_STACK.with(|tracking_items| tracking_items.borrow_mut().push_layer());
    let result = (func)();
    let gathers = TRACKING_STACK.with(|tracking_items| tracking_items.borrow_mut().pop_layer().unwrap());
    (gathers, result)
}

#[cfg(feature = "__single_holder")]
pub fn untrack<O, R>(opt: O) -> R
where
    O: FnOnce() -> R,
{
    scheduler::UNTRACKING.with(|untracking| {
        if !untracking.get() {
            untracking.set(true);
            let out = opt();
            untracking.set(false);
            out
        } else {
            opt()
        }
    })
}

#[cfg(not(feature = "__single_holder"))]

pub fn untrack<O, R>(holder_id: HolderId, opt: O) -> R
where
    O: FnOnce() -> R,
{
    scheduler::UNTRACKING.with(|untracking| {
        if !untracking.borrow().get(&holder_id).map(|v| *v).unwrap_or(false) {
            untracking.borrow_mut().insert(holder_id, true);
            let out = opt();
            untracking.borrow_mut().insert(holder_id, false);
            out
        } else {
            opt()
        }
    })
}

static NEXT_REVISABLE_ID: AtomicU64 = AtomicU64::new(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RevisableId(u64);
impl RevisableId {
    pub fn next() -> RevisableId {
        RevisableId(NEXT_REVISABLE_ID.fetch_add(1, Ordering::Relaxed))
    }
}
impl Display for RevisableId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RevisableId({})", self.0)
    }
}

pub trait Revisable: fmt::Debug {
    fn id(&self) -> RevisableId;
    #[cfg(not(feature = "__single_holder"))]
    fn holder_id(&self) -> Option<HolderId>;
    fn version(&self) -> usize;
    fn view_ids(&self) -> Rc<RefCell<IndexSet<ViewId>>>;
    fn bind_view(&self, view_id: &ViewId);
    fn unbind_view(&self, view_id: &ViewId);
    fn unlace_view(&self, view_id: &ViewId, loose: usize);
    fn is_revising(&self) -> bool {
        REVISING_ITEMS.with_borrow(|revising_items| {
            cfg_if! {
                if #[cfg(feature = "__single_holder")] {
                    revising_items.contains_key(&self.id())
                } else {
                    if let Some(holder_id) = self.holder_id() {
                        revising_items.get(&holder_id).map(|items|items.contains_key(&self.id())).unwrap_or(false)
                    } else {
                        tracing::debug!("Revisable::is_revising: holder_id is None");
                        false
                    }
                }
            }
        })
    }
    fn clone_boxed(&self) -> Box<dyn Revisable>;
}
impl Eq for dyn Revisable {}
impl PartialEq for dyn Revisable {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}
impl Hash for dyn Revisable {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id().hash(state);
    }
}