euv_core/reactive/signal/struct.rs
1use crate::*;
2
3/// Inner state of a signal, holding the value and subscribed listeners.
4///
5/// This struct is not exposed directly; use `Signal` instead.
6#[derive(CustomDebug, Data)]
7pub(crate) struct SignalInner<T>
8where
9 T: Clone,
10{
11 /// The current value of the signal.
12 #[debug(skip)]
13 #[get(pub(crate))]
14 #[set(pub(crate))]
15 pub(crate) value: T,
16 /// Callbacks to invoke when the value changes.
17 #[debug(skip)]
18 #[get(pub(crate))]
19 #[set(pub(crate))]
20 pub(crate) listeners: Vec<Box<dyn FnMut()>>,
21 /// Whether this signal is still active. Set to `false` by `clear_listeners()`
22 /// to make subsequent `set()` calls complete no-ops (no value update, no
23 /// listener invocation, no `schedule_signal_update()`), ensuring stale
24 /// closures like orphaned `setInterval` handlers become harmless.
25 #[get(pub(crate), type(copy))]
26 #[set(pub(crate))]
27 pub(crate) alive: bool,
28}
29
30/// A reactive signal handle.
31///
32/// Allows reading, writing, and subscribing to changes.
33/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
34///
35/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
36/// entire program. This is safe in single-threaded WASM contexts where no
37/// concurrent access can occur.
38#[derive(CustomDebug, Data)]
39pub struct Signal<T>
40where
41 T: Clone + PartialEq + 'static,
42{
43 /// Raw pointer to the heap-allocated signal inner state.
44 #[debug(skip)]
45 #[get(pub(crate))]
46 #[set(pub(crate))]
47 pub(crate) inner: *mut SignalInner<T>,
48}
49
50/// A `Sync` wrapper for single-threaded global `Signal` access.
51///
52/// SAFETY: This type is only safe to use in single-threaded contexts
53/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
54/// variable, but concurrent access from multiple threads would be
55/// undefined behavior.
56#[derive(CustomDebug)]
57pub struct SignalCell<T>
58where
59 T: Clone + PartialEq + 'static,
60{
61 /// Interior-mutable storage for an optional signal handle.
62 #[debug(skip)]
63 pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
64}