euv_core/reactive/signal/impl.rs
1use super::*;
2
3/// Implementation of reactive signal operations.
4impl<T> Signal<T>
5where
6 T: Clone + PartialEq + 'static,
7{
8 /// Returns a shared reference to the global typed signal slab.
9 ///
10 /// # Returns
11 ///
12 /// - `&'static SignalSlab` - A shared reference to the global signal slab.
13 fn slab() -> &'static SignalSlab {
14 unsafe { &*(*std::ptr::addr_of!(SIGNAL_SLAB)).deref().get() }
15 }
16
17 /// Returns a mutable reference to the global typed signal slab.
18 ///
19 /// # Returns
20 ///
21 /// - `&'static mut SignalSlab` - A mutable reference to the global signal slab.
22 fn slab_mut() -> &'static mut SignalSlab {
23 unsafe { &mut *(*std::ptr::addr_of_mut!(SIGNAL_SLAB)).deref().get() }
24 }
25
26 /// Creates a new `Signal` with the given initial value.
27 ///
28 /// Stores the `SignalInner<T>` in the global slab ([`SIGNAL_SLAB`]) and
29 /// returns a `Signal<T>` handle carrying the slot index. The slab is
30 /// append-only: a slot always belongs to the `Signal` that created it,
31 /// so stale handles can never observe a recycled slot of a different
32 /// type.
33 ///
34 /// # Arguments
35 ///
36 /// - `T: Clone + PartialEq + 'static` - The initial value of the signal.
37 ///
38 /// # Returns
39 ///
40 /// - `Self` - A handle to the newly created reactive signal.
41 pub fn create(value: T) -> Self {
42 let inner: SignalInner<T> = SignalInner::new(value, Vec::new(), true);
43 let idx: usize = Self::slab_mut().insert(inner);
44 let mut signal: Self = Self::new(0, PhantomData);
45 signal.set_inner(idx);
46 signal
47 }
48
49 /// Returns the current value of the signal.
50 ///
51 /// Directly reads the value from the slot stored in the global slab.
52 ///
53 /// If the signal has been marked inactive (`alive == false`), returns the
54 /// last stored value without registering tracking dependencies. This
55 /// ensures that stale async callbacks (e.g., orphaned `setInterval`)
56 /// holding a `Signal` copy can still call `.get()` safely without
57 /// triggering side effects or panics.
58 ///
59 /// If a tracking context is active (i.e., a DynamicNode is being rendered),
60 /// automatically registers the current dynamic node as a dependent of
61 /// this signal for precise reactive updates.
62 ///
63 /// # Returns
64 ///
65 /// - `T: Clone + PartialEq + 'static` - The current value of the signal.
66 pub fn get(&self) -> T {
67 let idx: usize = self.get_inner();
68 let Some(inner) = Self::slab_mut().get_mut::<T>(idx) else {
69 // Unresolvable handle: the slot index was never issued by this
70 // slab or belongs to a different concrete `T` (a corrupted or
71 // forged handle). Slots are never freed or recycled, so any
72 // handle produced by `Signal::create` always resolves; a `None`
73 // here is a program bug, and panicking is strictly better than
74 // vending a zero-initialized `T` (unsound for non-zeroable
75 // types such as `String` / `Vec`).
76 unreachable!("Signal handle does not resolve to a slab slot");
77 };
78 if !inner.get_alive() {
79 return inner.get_value().clone();
80 }
81 let tracking_id: usize = CURRENT_TRACKING_DYNAMIC_ID.load(Ordering::Relaxed);
82 if tracking_id != usize::MAX {
83 Self::push_dependent(inner, tracking_id);
84 }
85 inner.get_value().clone()
86 }
87
88 /// Read-only access to the signal value without cloning.
89 ///
90 /// OPT 17: callers that only need to inspect the value (e.g. format!, eq
91 /// check, debug print, length) can borrow via `with(|v| ...)` and avoid
92 /// one `T::clone` per call. The closure runs under the same tracking
93 /// rules as `get` (still registers `CURRENT_TRACKING_DYNAMIC_ID` if a
94 /// DynamicNode is rendering). The `T: Clone` bound stays on the impl
95 /// because `get` is required by the existing public API; `with` is the
96 /// zero-copy alternative for new code.
97 ///
98 /// # Arguments
99 ///
100 /// - `F: FnOnce(&T) -> R` - Closure receiving `&T`.
101 ///
102 /// # Returns
103 ///
104 /// - `R` - Whatever the closure returns.
105 pub fn with<F, R>(&self, f: F) -> R
106 where
107 F: FnOnce(&T) -> R,
108 {
109 let idx: usize = self.get_inner();
110 let Some(inner) = Self::slab_mut().get_mut::<T>(idx) else {
111 // Unresolvable handle: unreachable for slab-issued handles (see
112 // `get`). Panic instead of vending a zero-initialized `R`, which
113 // would be unsound for non-zeroable return types.
114 unreachable!("Signal handle does not resolve to a slab slot");
115 };
116 if !inner.get_alive() {
117 return f(inner.get_value());
118 }
119 let tracking_id: usize = CURRENT_TRACKING_DYNAMIC_ID.load(Ordering::Relaxed);
120 if tracking_id != usize::MAX {
121 Self::push_dependent(inner, tracking_id);
122 }
123 f(inner.get_value())
124 }
125
126 /// Subscribes a callback to be invoked when the signal changes.
127 ///
128 /// Returns the subscription id, which can later be passed to
129 /// [`Signal::unsubscribe`] to detach exactly this listener. Framework
130 /// DOM bindings use the id to tear down a binding when its element is
131 /// removed; macro-generated `watch!` / `computed!` subscriptions keep
132 /// the id unused because their lifetime is the enclosing hook context.
133 ///
134 /// # Arguments
135 ///
136 /// - `FnMut() + 'static` - The callback to invoke when the signal changes.
137 ///
138 /// # Returns
139 ///
140 /// - `usize` - The subscription id. `usize::MAX` when the handle is stale
141 /// (out-of-bounds slot); such an id is a safe no-op for `unsubscribe`.
142 pub fn subscribe<F>(&self, callback: F) -> usize
143 where
144 F: FnMut() + 'static,
145 {
146 let Some(inner) = Self::slab_mut().get_mut::<T>(self.get_inner()) else {
147 // Stale handle: no slot to register against — the subscription
148 // is silently dropped, matching the previous no-op semantics.
149 return usize::MAX;
150 };
151 let id: usize = inner.get_next_listener_id();
152 inner.set_next_listener_id(id.wrapping_add(1));
153 inner.get_mut_listeners().push((id, Box::new(callback)));
154 id
155 }
156
157 /// Detaches a single listener previously registered by [`Signal::subscribe`].
158 ///
159 /// When called while the signal is mid-notification (a listener callback
160 /// triggered this call re-entrantly), the removal is deferred: the id is
161 /// recorded and filtered out during `update`'s merge-back pass, so the
162 /// detached listener cannot be resurrected into the live list.
163 ///
164 /// # Arguments
165 ///
166 /// - `usize` - The subscription id returned by `subscribe`.
167 pub fn unsubscribe(&self, id: usize) {
168 let Some(inner) = Self::slab_mut().get_mut::<T>(self.get_inner()) else {
169 return;
170 };
171 if inner.get_notifying() {
172 inner.get_mut_removed_listener_ids().push(id);
173 return;
174 }
175 inner
176 .get_mut_listeners()
177 .retain(|(listener_id, _): &ListenerEntry| *listener_id != id);
178 }
179
180 /// Detaches this signal from the reactive system without freeing memory.
181 ///
182 /// Marks the signal inactive and clears its listeners and dependents, but
183 /// intentionally keeps the slab slot alive.
184 ///
185 /// This is the only supported teardown path for a signal, and is used by
186 /// the `use_signal` hook cleanup (when a component unmounts or a `match`
187 /// arm switches). The slot is deliberately never freed or recycled because
188 /// `Signal<T>` is `Copy` (just a `usize` slot index): async callbacks
189 /// (`spawn_local` futures, `setTimeout` / `setInterval` closures, Promise
190 /// continuations) may still hold copies of the signal, and recycling would
191 /// turn their later `.get()` / `.set()` calls into reads of an unrelated
192 /// signal. Deactivating instead makes those stale calls safe no-ops.
193 pub(crate) fn deactivate(&self) {
194 let idx: usize = self.get_inner();
195 let Some(inner) = Self::slab_mut().get_mut::<T>(idx) else {
196 // Out-of-bounds handle — treat as no-op. Mirrors the
197 // "deactivate on already-deactivated signal is a safe no-op"
198 // semantic.
199 return;
200 };
201 inner.set_alive(false);
202 inner.get_mut_listeners().clear();
203 inner.get_mut_dependents().clear();
204 inner.get_mut_removed_listener_ids().clear();
205 }
206
207 /// Core implementation of value update and listener notification.
208 ///
209 /// Returns `true` if the value was updated and listeners were notified.
210 /// Returns `false` if the signal is inactive or the value is unchanged.
211 ///
212 /// Uses a swap-out pattern for listeners: moves all listeners into a local
213 /// `Vec`, drops the mutable reference to inner state, then invokes each
214 /// listener. After invocation, listeners are moved back. This prevents
215 /// issues with re-entrant access during listener callbacks. Listeners
216 /// detached via `unsubscribe` mid-notification are filtered out during
217 /// the merge-back pass via `removed_listener_ids`.
218 ///
219 /// # Arguments
220 ///
221 /// - `T: Clone + PartialEq + 'static` - A generic type parameter.
222 ///
223 /// # Returns
224 ///
225 /// - `bool` - A boolean.
226 fn update(&self, value: T) -> bool {
227 let idx: usize = self.get_inner();
228 let Some(inner) = Self::slab_mut().get_mut::<T>(idx) else {
229 // Stale handle — treat as no-op.
230 return false;
231 };
232 if !inner.get_alive() {
233 return false;
234 }
235 if *inner.get_value() == value {
236 return false;
237 }
238 inner.set_value(value);
239 inner.set_notifying(true);
240 let mut listeners: Vec<ListenerEntry> = Vec::new();
241 swap(inner.get_mut_listeners(), &mut listeners);
242 for (_id, listener) in listeners.iter_mut() {
243 listener();
244 }
245 if !Self::is_alive(self.get_inner()) {
246 // The signal was deactivated by a listener mid-notification.
247 // Nothing should be merged back into a dead slot; clear the
248 // notification state so a later `unsubscribe` cannot pile up
249 // deferred removals that will never be drained.
250 if let Some(inner) = Self::slab_mut().get_mut::<T>(idx) {
251 inner.set_notifying(false);
252 inner.get_mut_removed_listener_ids().clear();
253 }
254 return true;
255 }
256 if let Some(inner) = Self::slab_mut().get_mut::<T>(idx)
257 && inner.get_alive()
258 {
259 let removed: Vec<usize> = take(inner.get_mut_removed_listener_ids());
260 if !removed.is_empty() {
261 listeners.retain(|(listener_id, _): &ListenerEntry| !removed.contains(listener_id));
262 }
263 let new_listeners: &mut Vec<ListenerEntry> = inner.get_mut_listeners();
264 if new_listeners.is_empty() {
265 swap(new_listeners, &mut listeners);
266 } else {
267 listeners.append(new_listeners);
268 swap(new_listeners, &mut listeners);
269 }
270 inner.set_notifying(false);
271 }
272 true
273 }
274
275 /// Registers a dynamic node ID as a dependent of the signal whose inner
276 /// state is already mutably borrowed by the caller.
277 ///
278 /// Fused form of the former `add_dependent`: `get` / `with` already hold
279 /// the slab borrow for the value read, so the dependent push happens on
280 /// the same borrow instead of resolving the slot a second time.
281 ///
282 /// OPT 9: the common rendering case is "this dependent was just added
283 /// (last element of the list)". A `deps.last() == Some(&dynamic_id)`
284 /// check short-circuits the `Vec::contains` linear scan, turning the
285 /// typical append-into-existing-list call from O(N) to O(1). Only the
286 /// rare cases (first add, or `dynamic_id` re-added after a previous
287 /// unsubscription) fall back to the full scan + push.
288 fn push_dependent(inner: &mut SignalInner<T>, dynamic_id: usize) {
289 let deps: &mut Vec<usize> = inner.get_mut_dependents();
290 if let Some(last) = deps.last() {
291 if *last == dynamic_id {
292 return;
293 }
294 if !deps.contains(&dynamic_id) {
295 deps.push(dynamic_id);
296 }
297 } else {
298 deps.push(dynamic_id);
299 }
300 }
301
302 /// Takes the dependent dynamic node ID list out of the slot, leaving an
303 /// empty list behind.
304 ///
305 /// Move semantics are sound here because every dependent re-registers
306 /// itself via `get` / `with` when its dynamic node re-renders, and the
307 /// dirty marking of the taken IDs has already happened by the time the
308 /// list is drained (see `set`). Stale IDs of unmounted nodes are dropped
309 /// instead of accumulating in the slot.
310 ///
311 /// # Returns
312 ///
313 /// - `Vec<usize>` - The drained dependents list.
314 pub(crate) fn take_dependents(&self) -> Vec<usize> {
315 Self::slab_mut()
316 .get_mut::<T>(self.get_inner())
317 .map(|inner: &mut SignalInner<T>| take(inner.get_mut_dependents()))
318 .unwrap_or_default()
319 }
320
321 /// Sets the value of the signal and notifies listeners.
322 ///
323 /// Uses precise dirty marking: only dynamic nodes that depend on
324 /// this signal are marked dirty, avoiding full broadcast.
325 ///
326 /// When called inside `batch`, the dispatch is
327 /// deferred (dirty slots are still marked precisely), and the
328 /// outermost `set()` call outside the suppressed scope will
329 /// trigger the actual dispatch cycle.
330 ///
331 /// # Arguments
332 ///
333 /// - `T: Clone + PartialEq + 'static` - The new value to assign to the signal.
334 pub fn set(&self, value: T) {
335 if self.update(value) {
336 let dependents: Vec<usize> = self.take_dependents();
337 App::schedule_update(&dependents);
338 }
339 }
340
341 /// Returns whether the signal slot at `idx` is still alive
342 /// (i.e. has not been deactivated).
343 ///
344 /// # Arguments
345 ///
346 /// - `usize` - Slab index to test.
347 ///
348 /// # Returns
349 ///
350 /// - `bool` - `true` when the slot refers to a live signal.
351 pub(crate) fn is_alive(idx: usize) -> bool {
352 Self::slab().is_alive(idx)
353 }
354}
355
356/// Provides a safe default for `Signal<T>` by creating a valid signal
357/// initialized with `T::default()`.
358///
359/// This prevents the creation of invalid signals with `inner = 0` (null
360/// pointer), which would cause a panic when `.get()` is called.
361///
362/// # Returns
363///
364/// - `Self` - A valid signal initialized with `T::default()`.
365impl<T> Default for Signal<T>
366where
367 T: Clone + Default + PartialEq + 'static,
368{
369 /// Constructs a default [`Signal`] value.
370 fn default() -> Self {
371 Self::create(T::default())
372 }
373}
374
375/// Clones the signal, sharing the same inner state.
376///
377/// Since `Signal` is `Copy`, this simply returns `*self`.
378///
379/// # Returns
380///
381/// - `Self` - A copy of the signal handle sharing the same inner state.
382impl<T> Clone for Signal<T>
383where
384 T: Clone + PartialEq + 'static,
385{
386 /// Clones the [`Signal`] by reusing shared, cheap-to-clone state where possible.
387 fn clone(&self) -> Self {
388 *self
389 }
390}
391
392/// Copies the signal, sharing the same inner state.
393///
394/// Safe because only the inner address (a `usize`) is copied;
395/// the actual heap allocation is owned by the global signal registry.
396impl<T> Copy for Signal<T> where T: Clone + PartialEq + 'static {}
397
398/// Marks `SignalCell` as `Sync` for single-threaded WASM contexts.
399///
400/// SAFETY: `SignalCell` is only used in single-threaded WASM contexts.
401/// Concurrent access from multiple threads would be undefined behavior.
402unsafe impl<T> Sync for SignalCell<T> where T: Clone + PartialEq + 'static {}
403
404/// Implementation of SignalCell construction and access.
405impl<T> SignalCell<T>
406where
407 T: Clone + PartialEq + 'static,
408{
409 /// Creates a new `SignalCell` with no signal stored.
410 ///
411 /// # Returns
412 ///
413 /// - `Self` - An empty `SignalCell` with `None` stored in the inner `UnsafeCell`.
414 pub const fn none() -> Self {
415 Self {
416 inner: UnsafeCell::new(None),
417 }
418 }
419
420 /// Stores a signal into the cell.
421 ///
422 /// First write wins: if a signal has already been stored, the new
423 /// signal is dropped and the existing one is kept.
424 ///
425 /// # Arguments
426 ///
427 /// - `Signal<T>` - The signal to store.
428 pub fn set(&self, signal: Signal<T>) {
429 unsafe {
430 let ptr: &mut Option<Signal<T>> = &mut *self.get_inner().get();
431 if ptr.is_none() {
432 *ptr = Some(signal);
433 }
434 }
435 }
436
437 /// Returns the signal stored in the cell, if any.
438 ///
439 /// # Returns
440 ///
441 /// - `Option<Signal<T>>` - The stored signal, or `None` when no signal
442 /// has been stored via `set` yet.
443 pub fn loaded(&self) -> Option<Signal<T>> {
444 unsafe {
445 let ptr: &Option<Signal<T>> = &*self.get_inner().get();
446 *ptr
447 }
448 }
449}
450
451/// Provides a default empty `SignalCell`.
452///
453/// Creates a `SignalCell` with `None` stored in the inner `UnsafeCell`.
454///
455/// # Returns
456///
457/// - `Self` - An empty `SignalCell` with no signal stored.
458impl<T> Default for SignalCell<T>
459where
460 T: Clone + PartialEq + 'static,
461{
462 /// Constructs a default [`SignalCell`] value.
463 fn default() -> Self {
464 Self::new(UnsafeCell::new(None))
465 }
466}
467
468/// Implementation of `FireHandle` construction, invocation, and conversions.
469impl FireHandle {
470 /// Leaks the given closure and returns a handle pointing to its heap address.
471 ///
472 /// The closure is double-boxed (`Box<Box<dyn FnMut()>>`) and leaked so the
473 /// inner box's address remains stable for the lifetime of the program.
474 /// The address is captured as a `usize` and wrapped in a `FireHandle`.
475 ///
476 /// # Arguments
477 ///
478 /// - `F: FnMut() + 'static` - The fire closure to leak.
479 ///
480 /// # Returns
481 ///
482 /// - `Self` - A handle holding the leaked closure's address.
483 pub fn new<F>(fire: F) -> Self
484 where
485 F: FnMut() + 'static,
486 {
487 let leaked: &'static mut Box<dyn FnMut()> =
488 Box::leak(Box::new(Box::new(fire) as Box<dyn FnMut()>));
489 let addr: usize = leaked as *mut Box<dyn FnMut()> as usize;
490 let mut handle: Self = Self { inner: 0 };
491 handle.set_inner(addr);
492 handle
493 }
494
495 /// Invokes the closure pointed to by this handle.
496 ///
497 /// Takes `self` by value because `FireHandle: Copy` — repeated invocations
498 /// on a single captured handle each copy the address and operate on the
499 /// same underlying closure.
500 ///
501 /// # Safety
502 ///
503 /// The handle must come from `FireHandle::new` (or `From`) and the
504 /// underlying boxed closure must still be live.
505 pub unsafe fn fire(self) {
506 unsafe { Self::fire_at(self.get_inner()) };
507 }
508
509 /// Invokes the closure stored at the given address.
510 ///
511 /// This is the static counterpart of `fire` for call sites that have
512 /// only the raw `usize` address (e.g., macro-generated code that
513 /// captures the address by `move` into a subscribe closure).
514 ///
515 /// # Arguments
516 ///
517 /// - `usize` - The address of a leaked `Box<dyn FnMut()>`.
518 ///
519 /// # Safety
520 ///
521 /// `addr` must come from a valid `FireHandle` produced by `new` (or
522 /// `From`) and the underlying boxed closure must still be live.
523 pub unsafe fn fire_at(addr: usize) {
524 let ptr: *mut Box<dyn FnMut()> = addr as *mut Box<dyn FnMut()>;
525 unsafe { (&mut *ptr)() };
526 }
527}
528
529/// Leaks a fire closure into a `FireHandle`.
530///
531/// This is the canonical `Into` path used by `watch!`/`computed!` macros
532/// and the virtual list component to obtain a `FireHandle` from a closure.
533impl<F> From<F> for FireHandle
534where
535 F: FnMut() + 'static,
536{
537 /// Leaks this closure and stores its address in the returned handle.
538 ///
539 /// # Returns
540 ///
541 /// - `FireHandle` - A handle holding the leaked closure's address.
542 ///
543 /// # Arguments
544 ///
545 /// - `F` - Input value to convert from.
546 fn from(fire: F) -> Self {
547 Self::new(fire)
548 }
549}
550
551/// Extracts the raw address from a `FireHandle`.
552///
553/// This is used by macro-generated code that needs to capture the address
554/// (a `Copy` type) into `FnMut() + 'static` subscribe closures.
555impl From<FireHandle> for usize {
556 /// Returns the leaked closure's heap address.
557 ///
558 /// # Returns
559 ///
560 /// - `usize` - The address held by this handle.
561 ///
562 /// # Arguments
563 ///
564 /// - `FireHandle` - Input value to convert from.
565 fn from(handle: FireHandle) -> Self {
566 handle.get_inner()
567 }
568}
569
570/// Implementation of the typed signal slab allocator.
571impl SignalSlab {
572 /// Creates an empty slab.
573 pub(crate) fn new() -> Self {
574 Self {
575 entries: Vec::new(),
576 }
577 }
578
579 /// Inserts a new typed `SignalInner<T>` and returns its slot index.
580 ///
581 /// Append-only: the slot index issued here is never reused for another
582 /// signal, which is what makes stale-handle reads sound (they always
583 /// resolve to this slot's original, possibly deactivated, inner state).
584 pub(crate) fn insert<T>(&mut self, inner: SignalInner<T>) -> usize
585 where
586 T: Clone + PartialEq + 'static,
587 {
588 let boxed: Box<dyn AnySignalInner> = Box::new(inner);
589 let idx: usize = self.entries.len();
590 self.entries.push(boxed);
591 idx
592 }
593
594 /// Returns a typed `&mut SignalInner<T>` view of the slot at `idx`.
595 ///
596 /// Returns `None` when the index is out of bounds or was issued for a
597 /// different concrete `T` (defensive TypeId check). Slots are never
598 /// freed, so `None` means the caller is holding a corrupted handle —
599 /// surfaced as `None` rather than panicking so that stale handles
600 /// degrade into safe no-ops (matching the `alive == false` semantics).
601 pub(crate) fn get_mut<T>(&mut self, idx: usize) -> Option<&mut SignalInner<T>>
602 where
603 T: Clone + PartialEq + 'static,
604 {
605 self.entries
606 .get_mut(idx)?
607 .as_any_mut()
608 .downcast_mut::<SignalInner<T>>()
609 }
610
611 /// Returns `true` when the slot at `idx` exists AND its inner signal is
612 /// still marked `alive`. Used by `Signal::is_alive`.
613 pub(crate) fn is_alive(&self, idx: usize) -> bool {
614 match self.entries.get(idx) {
615 Some(inner) => inner.alive(),
616 None => false,
617 }
618 }
619}