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: SmallSubList<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: SmallSubList<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: SmallSubList<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: SmallSubList<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    /// Optional parent scope identifier for O(depth) zero-allocation ancestry and freeze queries.
141    #[get(pub(crate), type(copy))]
142    #[get_mut(pub(crate))]
143    #[set(pub(crate))]
144    pub(crate) parent: Option<ScopeId>,
145    /// Signals owned by this scope.
146    #[get(pub(crate))]
147    #[get_mut(pub(crate))]
148    #[set(pub(crate))]
149    pub(crate) signals: Vec<SignalId>,
150    /// Memos owned by this scope.
151    #[get(pub(crate))]
152    #[get_mut(pub(crate))]
153    #[set(pub(crate))]
154    pub(crate) memos: Vec<MemoId>,
155    /// Effects owned by this scope.
156    #[get(pub(crate))]
157    #[get_mut(pub(crate))]
158    #[set(pub(crate))]
159    pub(crate) effects: Vec<EffectId>,
160    /// Child scopes owned by this scope.
161    #[get(pub(crate))]
162    #[get_mut(pub(crate))]
163    #[set(pub(crate))]
164    pub(crate) children: Vec<ScopeId>,
165    /// Cleanup callbacks invoked when this scope is disposed.
166    #[debug(skip)]
167    #[get(skip)]
168    #[get_mut(pub(crate))]
169    #[set(pub(crate))]
170    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
171}
172
173/// Thread-local arena storing all reactive signals, memos, effects, and scopes.
174#[derive(CustomDebug, Data)]
175pub struct ReactiveRuntime {
176    /// Signal slot arena.
177    #[get(pub(crate))]
178    #[get_mut(pub(crate))]
179    #[set(pub(crate))]
180    pub(crate) signals: Vec<SignalEntry>,
181    /// Free-list indices for recycled signal slots.
182    #[get(pub(crate))]
183    #[get_mut(pub(crate))]
184    #[set(pub(crate))]
185    pub(crate) free_signals: Vec<u32>,
186    /// Memo slot arena.
187    #[get(pub(crate))]
188    #[get_mut(pub(crate))]
189    #[set(pub(crate))]
190    pub(crate) memos: Vec<MemoEntry>,
191    /// Free-list indices for recycled memo slots.
192    #[get(pub(crate))]
193    #[get_mut(pub(crate))]
194    #[set(pub(crate))]
195    pub(crate) free_memos: Vec<u32>,
196    /// Effect slot arena.
197    #[get(pub(crate))]
198    #[get_mut(pub(crate))]
199    #[set(pub(crate))]
200    pub(crate) effects: Vec<EffectEntry>,
201    /// Free-list indices for recycled effect slots.
202    #[get(pub(crate))]
203    #[get_mut(pub(crate))]
204    #[set(pub(crate))]
205    pub(crate) free_effects: Vec<u32>,
206    /// Scope slot arena.
207    #[get(pub(crate))]
208    #[get_mut(pub(crate))]
209    #[set(pub(crate))]
210    pub(crate) scopes: Vec<ScopeEntry>,
211    /// Free-list indices for recycled scope slots.
212    #[get(pub(crate))]
213    #[get_mut(pub(crate))]
214    #[set(pub(crate))]
215    pub(crate) free_scopes: Vec<u32>,
216    /// Stack of currently executing observers and their dependency-reuse cursors.
217    #[get(pub(crate))]
218    #[get_mut(pub(crate))]
219    #[set(pub(crate))]
220    pub(crate) observer_stack: Vec<Option<(SubscriberId, usize)>>,
221    /// Stack of active ownership scopes.
222    #[get(pub(crate))]
223    #[get_mut(pub(crate))]
224    #[set(pub(crate))]
225    pub(crate) scope_stack: Vec<ScopeId>,
226    /// Pending effects awaiting flush.
227    #[get(pub(crate))]
228    #[get_mut(pub(crate))]
229    #[set(pub(crate))]
230    pub(crate) pending_effects: Vec<EffectId>,
231    /// Reusable scratch queue storing `(subscriber, target_state)` for zero-allocation dirty propagation.
232    #[get(pub(crate))]
233    #[get_mut(pub(crate))]
234    #[set(pub(crate))]
235    pub(crate) dirty_queue: Vec<(SubscriberId, NodeState)>,
236    /// Reusable cursor-sliced scratch stack for zero-allocation `NodeState::Check` verification of >4 memo deps.
237    #[get(pub(crate))]
238    #[get_mut(pub(crate))]
239    #[set(pub(crate))]
240    pub(crate) check_scratch: Vec<MemoId>,
241    /// Reusable scratch buffer holding `(depth, EffectId)` batches across `flush_effects` runs.
242    #[get(pub(crate))]
243    #[get_mut(pub(crate))]
244    #[set(pub(crate))]
245    pub(crate) flush_batch_scratch: Vec<(u32, EffectId)>,
246    /// Reusable DFS stack for zero-allocation scope disposal and freeze/resume traversals.
247    #[get(pub(crate))]
248    #[get_mut(pub(crate))]
249    #[set(pub(crate))]
250    pub(crate) scope_stack_scratch: Vec<ScopeId>,
251    /// Reusable scratch list of effects collected during `dispose_scope_collect`.
252    #[get(pub(crate))]
253    #[get_mut(pub(crate))]
254    #[set(pub(crate))]
255    pub(crate) dispose_effects_scratch: Vec<EffectId>,
256    /// Reusable scratch list of memos collected during `dispose_scope_collect`.
257    #[get(pub(crate))]
258    #[get_mut(pub(crate))]
259    #[set(pub(crate))]
260    pub(crate) dispose_memos_scratch: Vec<MemoId>,
261    /// Reusable scratch list of signals collected during `dispose_scope_collect`.
262    #[get(pub(crate))]
263    #[get_mut(pub(crate))]
264    #[set(pub(crate))]
265    pub(crate) dispose_signals_scratch: Vec<SignalId>,
266    /// Active batch nesting depth; flush executes only when returning to zero.
267    #[get(pub(crate), type(copy))]
268    #[get_mut(pub(crate))]
269    #[set(pub(crate))]
270    pub(crate) batch_depth: u32,
271    /// Guard flag preventing recursive flush loops.
272    #[get(pub(crate), type(copy))]
273    #[get_mut(pub(crate))]
274    #[set(pub(crate))]
275    pub(crate) flushing: bool,
276    /// Global version clock incremented on every reactive write.
277    #[get(pub(crate), type(copy))]
278    #[get_mut(pub(crate))]
279    #[set(pub(crate))]
280    pub(crate) clock: u64,
281    /// Type-keyed root-lifetime global state signal registry.
282    #[get(pub(crate))]
283    #[get_mut(pub(crate))]
284    #[set(pub(crate))]
285    pub(crate) global_states: HashMap<TypeId, SignalId>,
286    /// String-keyed root-lifetime named global state signal registry.
287    #[get(pub(crate))]
288    #[get_mut(pub(crate))]
289    #[set(pub(crate))]
290    pub(crate) named_global_states: HashMap<String, SignalId>,
291    /// Type-keyed cross-component custom event channel signals.
292    #[get(pub(crate))]
293    #[get_mut(pub(crate))]
294    #[set(pub(crate))]
295    pub(crate) event_channels: HashMap<TypeId, SignalId>,
296}
297
298/// Snapshot of runtime diagnostic statistics for telemetry and benchmarking.
299#[derive(Clone, Copy, Debug, Default, Eq, Getter, Hash, New, Ord, PartialEq, PartialOrd)]
300pub struct ReactiveStats {
301    /// Total live signals in the current thread runtime.
302    #[get(type(copy))]
303    pub(crate) live_signals: usize,
304    /// Total live memos in the current thread runtime.
305    #[get(type(copy))]
306    pub(crate) live_memos: usize,
307    /// Total live effects in the current thread runtime.
308    #[get(type(copy))]
309    pub(crate) live_effects: usize,
310    /// Total live ownership scopes in the current thread runtime.
311    #[get(type(copy))]
312    pub(crate) live_scopes: usize,
313    /// Global reactive write clock.
314    #[get(type(copy))]
315    pub(crate) clock: u64,
316}
317
318/// Panic-safe RAII guard that pops an active tracking frame from `observer_stack` on unwind.
319#[derive(Debug)]
320pub(crate) struct TrackingGuard {
321    /// Remains `true` until normal completion disarms the guard before committing.
322    pub(crate) active: bool,
323}