Skip to main content

euv_core/reactive/runtime/
struct.rs

1use super::*;
2
3/// Storage entry for a type-erased signal inside the reactive arena.
4#[derive(CustomDebug, Data)]
5pub(crate) struct SignalEntry {
6    /// Generation counter for this slot.
7    #[get(pub(crate), type(copy))]
8    #[get_mut(pub(crate))]
9    #[set(pub(crate))]
10    pub(crate) generation: u32,
11    /// Monotonic version counter incremented on every write.
12    #[get(pub(crate), type(copy))]
13    #[get_mut(pub(crate))]
14    #[set(pub(crate))]
15    pub(crate) version: u64,
16    /// Type-erased signal payload; `None` when slot is free.
17    #[debug(skip)]
18    #[get(skip)]
19    #[get_mut(pub(crate))]
20    #[set(pub(crate))]
21    pub(crate) value: Option<Box<dyn Any>>,
22    /// Downstream subscribers that depend on this signal.
23    #[get(pub(crate))]
24    #[get_mut(pub(crate))]
25    #[set(pub(crate))]
26    pub(crate) subscribers: Vec<SubscriberId>,
27}
28
29/// Storage entry for a memoized derived computation inside the reactive arena.
30#[derive(CustomDebug, Data)]
31pub(crate) struct MemoEntry {
32    /// Generation counter for this slot.
33    #[get(pub(crate), type(copy))]
34    #[get_mut(pub(crate))]
35    #[set(pub(crate))]
36    pub(crate) generation: u32,
37    /// Monotonic version counter incremented when computed value changes.
38    #[get(pub(crate), type(copy))]
39    #[get_mut(pub(crate))]
40    #[set(pub(crate))]
41    pub(crate) version: u64,
42    /// Current push-pull evaluation state.
43    #[get(pub(crate), type(copy))]
44    #[get_mut(pub(crate))]
45    #[set(pub(crate))]
46    pub(crate) state: NodeState,
47    /// Cached computed value; `None` when uninitialized or freed.
48    #[debug(skip)]
49    #[get(skip)]
50    #[get_mut(pub(crate))]
51    #[set(pub(crate))]
52    pub(crate) value: Option<Box<dyn Any>>,
53    /// Type-erased recomputation closure returning `(new_value, changed)`.
54    #[debug(skip)]
55    #[get(skip)]
56    #[get_mut(pub(crate))]
57    #[set(pub(crate))]
58    pub(crate) compute: Option<MemoComputeFn>,
59    /// Upstream dependencies tracked during the previous evaluation.
60    #[get(pub(crate))]
61    #[get_mut(pub(crate))]
62    #[set(pub(crate))]
63    pub(crate) dependencies: Vec<DependencyId>,
64    /// Downstream subscribers depending on this memo.
65    #[get(pub(crate))]
66    #[get_mut(pub(crate))]
67    #[set(pub(crate))]
68    pub(crate) subscribers: Vec<SubscriberId>,
69    /// Topological depth level used to evaluate memos before effects in order.
70    #[get(pub(crate), type(copy))]
71    #[get_mut(pub(crate))]
72    #[set(pub(crate))]
73    pub(crate) depth: u32,
74}
75
76/// Storage entry for a reactive side effect inside the reactive arena.
77#[derive(CustomDebug, Data)]
78pub(crate) struct EffectEntry {
79    /// Generation counter for this slot.
80    #[get(pub(crate), type(copy))]
81    #[get_mut(pub(crate))]
82    #[set(pub(crate))]
83    pub(crate) generation: u32,
84    /// Whether this effect is currently alive.
85    #[get(pub(crate), type(copy))]
86    #[get_mut(pub(crate))]
87    #[set(pub(crate))]
88    pub(crate) alive: bool,
89    /// Whether this effect is queued for execution.
90    #[get(pub(crate), type(copy))]
91    #[get_mut(pub(crate))]
92    #[set(pub(crate))]
93    pub(crate) scheduled: bool,
94    /// Type-erased side-effect closure.
95    #[debug(skip)]
96    #[get(skip)]
97    #[get_mut(pub(crate))]
98    #[set(pub(crate))]
99    pub(crate) callback: Option<Rc<RefCell<dyn FnMut()>>>,
100    /// Upstream dependencies tracked during the previous execution.
101    #[get(pub(crate))]
102    #[get_mut(pub(crate))]
103    #[set(pub(crate))]
104    pub(crate) dependencies: Vec<DependencyId>,
105    /// Execution priority depth so parent effects run before nested ones.
106    #[get(pub(crate), type(copy))]
107    #[get_mut(pub(crate))]
108    #[set(pub(crate))]
109    pub(crate) depth: u32,
110    /// Active freeze nesting depth (`> 0` suspends execution while tracking dirty notifications).
111    #[get(pub(crate), type(copy))]
112    #[get_mut(pub(crate))]
113    #[set(pub(crate))]
114    pub(crate) frozen_depth: u32,
115    /// Whether upstream signals or memos dirtied this effect while frozen.
116    #[get(pub(crate), type(copy))]
117    #[get_mut(pub(crate))]
118    #[set(pub(crate))]
119    pub(crate) pending_while_frozen: bool,
120}
121
122/// Ownership scope entry that disposes all owned reactive resources together.
123#[derive(CustomDebug, Data)]
124pub(crate) struct ScopeEntry {
125    /// Generation counter for this scope slot.
126    #[get(pub(crate), type(copy))]
127    #[get_mut(pub(crate))]
128    #[set(pub(crate))]
129    pub(crate) generation: u32,
130    /// Whether this scope is alive.
131    #[get(pub(crate), type(copy))]
132    #[get_mut(pub(crate))]
133    #[set(pub(crate))]
134    pub(crate) alive: bool,
135    /// Whether this scope is currently frozen (`PageRetention::Freeze`).
136    #[get(pub(crate), type(copy))]
137    #[get_mut(pub(crate))]
138    #[set(pub(crate))]
139    pub(crate) frozen: bool,
140    /// Signals owned by this scope.
141    #[get(pub(crate))]
142    #[get_mut(pub(crate))]
143    #[set(pub(crate))]
144    pub(crate) signals: Vec<SignalId>,
145    /// Memos owned by this scope.
146    #[get(pub(crate))]
147    #[get_mut(pub(crate))]
148    #[set(pub(crate))]
149    pub(crate) memos: Vec<MemoId>,
150    /// Effects owned by this scope.
151    #[get(pub(crate))]
152    #[get_mut(pub(crate))]
153    #[set(pub(crate))]
154    pub(crate) effects: Vec<EffectId>,
155    /// Child scopes owned by this scope.
156    #[get(pub(crate))]
157    #[get_mut(pub(crate))]
158    #[set(pub(crate))]
159    pub(crate) children: Vec<ScopeId>,
160    /// Cleanup callbacks invoked when this scope is disposed.
161    #[debug(skip)]
162    #[get(skip)]
163    #[get_mut(pub(crate))]
164    #[set(pub(crate))]
165    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
166}
167
168/// Thread-local arena storing all reactive signals, memos, effects, and scopes.
169#[derive(CustomDebug, Data)]
170pub struct ReactiveRuntime {
171    /// Signal slot arena.
172    #[get(pub(crate))]
173    #[get_mut(pub(crate))]
174    #[set(pub(crate))]
175    pub(crate) signals: Vec<SignalEntry>,
176    /// Free-list indices for recycled signal slots.
177    #[get(pub(crate))]
178    #[get_mut(pub(crate))]
179    #[set(pub(crate))]
180    pub(crate) free_signals: Vec<u32>,
181    /// Memo slot arena.
182    #[get(pub(crate))]
183    #[get_mut(pub(crate))]
184    #[set(pub(crate))]
185    pub(crate) memos: Vec<MemoEntry>,
186    /// Free-list indices for recycled memo slots.
187    #[get(pub(crate))]
188    #[get_mut(pub(crate))]
189    #[set(pub(crate))]
190    pub(crate) free_memos: Vec<u32>,
191    /// Effect slot arena.
192    #[get(pub(crate))]
193    #[get_mut(pub(crate))]
194    #[set(pub(crate))]
195    pub(crate) effects: Vec<EffectEntry>,
196    /// Free-list indices for recycled effect slots.
197    #[get(pub(crate))]
198    #[get_mut(pub(crate))]
199    #[set(pub(crate))]
200    pub(crate) free_effects: Vec<u32>,
201    /// Scope slot arena.
202    #[get(pub(crate))]
203    #[get_mut(pub(crate))]
204    #[set(pub(crate))]
205    pub(crate) scopes: Vec<ScopeEntry>,
206    /// Free-list indices for recycled scope slots.
207    #[get(pub(crate))]
208    #[get_mut(pub(crate))]
209    #[set(pub(crate))]
210    pub(crate) free_scopes: Vec<u32>,
211    /// Stack of currently executing observers for automatic dependency tracking.
212    #[get(pub(crate))]
213    #[get_mut(pub(crate))]
214    #[set(pub(crate))]
215    pub(crate) observer_stack: Vec<Option<SubscriberId>>,
216    /// Stack of active ownership scopes.
217    #[get(pub(crate))]
218    #[get_mut(pub(crate))]
219    #[set(pub(crate))]
220    pub(crate) scope_stack: Vec<ScopeId>,
221    /// Pending effects awaiting flush.
222    #[get(pub(crate))]
223    #[get_mut(pub(crate))]
224    #[set(pub(crate))]
225    pub(crate) pending_effects: Vec<EffectId>,
226    /// Reusable scratch queue for zero-allocation dirty propagation.
227    #[get(pub(crate))]
228    #[get_mut(pub(crate))]
229    #[set(pub(crate))]
230    pub(crate) dirty_queue: Vec<(SubscriberId, bool)>,
231    /// Active batch nesting depth; flush executes only when returning to zero.
232    #[get(pub(crate), type(copy))]
233    #[get_mut(pub(crate))]
234    #[set(pub(crate))]
235    pub(crate) batch_depth: u32,
236    /// Guard flag preventing recursive flush loops.
237    #[get(pub(crate), type(copy))]
238    #[get_mut(pub(crate))]
239    #[set(pub(crate))]
240    pub(crate) flushing: bool,
241    /// Global version clock incremented on every reactive write.
242    #[get(pub(crate), type(copy))]
243    #[get_mut(pub(crate))]
244    #[set(pub(crate))]
245    pub(crate) clock: u64,
246    /// Type-keyed root-lifetime global state signal registry.
247    #[get(pub(crate))]
248    #[get_mut(pub(crate))]
249    #[set(pub(crate))]
250    pub(crate) global_states: HashMap<TypeId, SignalId>,
251    /// String-keyed root-lifetime named global state signal registry.
252    #[get(pub(crate))]
253    #[get_mut(pub(crate))]
254    #[set(pub(crate))]
255    pub(crate) named_global_states: HashMap<String, SignalId>,
256    /// Type-keyed cross-component custom event channel signals.
257    #[get(pub(crate))]
258    #[get_mut(pub(crate))]
259    #[set(pub(crate))]
260    pub(crate) event_channels: HashMap<TypeId, SignalId>,
261}
262
263/// Snapshot of runtime diagnostic statistics for telemetry and benchmarking.
264#[derive(Clone, Copy, Debug, Default, Eq, Getter, Hash, New, Ord, PartialEq, PartialOrd)]
265pub struct ReactiveStats {
266    /// Total live signals in the current thread runtime.
267    #[get(type(copy))]
268    pub(crate) live_signals: usize,
269    /// Total live memos in the current thread runtime.
270    #[get(type(copy))]
271    pub(crate) live_memos: usize,
272    /// Total live effects in the current thread runtime.
273    #[get(type(copy))]
274    pub(crate) live_effects: usize,
275    /// Total live ownership scopes in the current thread runtime.
276    #[get(type(copy))]
277    pub(crate) live_scopes: usize,
278    /// Global reactive write clock.
279    #[get(type(copy))]
280    pub(crate) clock: u64,
281}