Skip to main content

euv_core/reactive/
fn.rs

1use crate::*;
2
3/// Dispatches the global `__euv_signal_update__` event on the window.
4///
5/// This triggers any `DynamicNode` listeners that are subscribed via
6/// `window.addEventListener("__euv_signal_update__", ...)` so that
7/// dynamic virtual DOM nodes can re-render when signal values change.
8///
9/// # Panics
10///
11/// Panics if `Event::new("__euv_signal_update__")` fails, which should
12/// never happen for a valid event type string.
13#[cfg(target_arch = "wasm32")]
14pub(crate) fn dispatch_signal_update() {
15    if let Some(win) = window() {
16        let event: Event = Event::new("__euv_signal_update__").unwrap();
17        let _ = win.dispatch_event(&event);
18    }
19}
20
21/// Schedules a deferred `__euv_signal_update__` event via a microtask.
22///
23/// Batches multiple signal updates within the same synchronous tick into
24/// a single dispatch that runs after all local listeners have completed,
25/// preventing DynamicNode re-renders from interfering with in-flight
26/// signal updates.
27///
28/// When `SUPPRESS_SCHEDULE` is `true`, this function is a no-op so that
29/// internal operations (such as `watch!` initialisation) can perform
30/// signal mutations without triggering premature DOM re-renders.
31///
32/// # Panics
33///
34/// Panics if `Promise::new()` or `Event::new()` fails.
35pub(crate) fn schedule_signal_update() {
36    if SCHEDULED.load(Ordering::Relaxed) || SUPPRESS_SCHEDULE.load(Ordering::Relaxed) {
37        return;
38    }
39    SCHEDULED.store(true, Ordering::Relaxed);
40    #[cfg(target_arch = "wasm32")]
41    {
42        let win: Option<Window> = window();
43        if win.is_none() {
44            SCHEDULED.store(false, Ordering::Relaxed);
45            return;
46        }
47        let promise: js_sys::Promise = js_sys::Promise::resolve(&wasm_bindgen::JsValue::NULL);
48        let closure: wasm_bindgen::closure::Closure<dyn FnMut(wasm_bindgen::JsValue)> =
49            wasm_bindgen::closure::Closure::wrap(Box::new(move |_value: wasm_bindgen::JsValue| {
50                SCHEDULED.store(false, Ordering::Relaxed);
51                dispatch_signal_update();
52            }));
53        let _ = promise.then(&closure);
54        closure.forget();
55    }
56    #[cfg(not(target_arch = "wasm32"))]
57    {
58        SCHEDULED.store(false, Ordering::Relaxed);
59    }
60}
61
62/// Executes a closure with signal update scheduling suppressed.
63///
64/// Any `schedule_signal_update()` calls that occur within the closure
65/// (including those triggered by `Signal::set()`) are silently ignored.
66/// After the closure returns, the suppress flag is restored to its
67/// previous value.
68///
69/// This is used internally by `watch!` to prevent its initial body
70/// execution from triggering unnecessary DynamicNode re-renders.
71///
72/// # Arguments
73///
74/// - `FnOnce() -> R`: The closure to execute with suppressed scheduling.
75///
76/// # Returns
77///
78/// - `R`: The return value of the closure.
79pub fn with_suppressed_updates<F, R>(f: F) -> R
80where
81    F: FnOnce() -> R,
82{
83    let previous: bool = SUPPRESS_SCHEDULE.load(Ordering::Relaxed);
84    SUPPRESS_SCHEDULE.store(true, Ordering::Relaxed);
85    let result: R = f();
86    SUPPRESS_SCHEDULE.store(previous, Ordering::Relaxed);
87    result
88}
89
90/// Subscribes an attribute signal to the global `__euv_signal_update__` event so that
91/// whenever any signal changes, the attribute value is recomputed and the attribute
92/// signal is updated. This enables reactive `if` conditions inside any HTML attribute,
93/// including `style`, `class`, and others.
94///
95/// Works identically to the DOM-level `if {expr} { children }` mechanism: both
96/// re-evaluate their condition expressions when any signal dispatches the global
97/// update event, then apply only the minimal diff to the DOM.
98///
99/// # Arguments
100///
101/// - `Signal<String>`: The attribute signal to update when signals change.
102/// - `Fn() -> String + 'static`: A closure that recomputes the attribute value string.
103///
104/// # Panics
105///
106/// Panics if `window()` is unavailable on the current platform.
107pub fn subscribe_attr_signal<F>(attr_signal: Signal<String>, compute: F)
108where
109    F: Fn() -> String + 'static,
110{
111    #[cfg(target_arch = "wasm32")]
112    {
113        let window: Window = window().expect("no global window exists");
114        let closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
115            let new_value: String = compute();
116            attr_signal.set(new_value);
117        }));
118        window
119            .add_event_listener_with_callback(
120                &NativeEventName::EuvSignalUpdate.to_string(),
121                closure.as_ref().unchecked_ref(),
122            )
123            .unwrap();
124        closure.forget();
125    }
126    #[cfg(not(target_arch = "wasm32"))]
127    {
128        let _ = attr_signal;
129        let _ = compute;
130    }
131}
132
133/// Returns the currently active `HookContext`.
134///
135/// When called outside a `with_hook_context` scope, returns a reference
136/// to the default empty context.
137///
138/// # Returns
139///
140/// - `HookContext`: The currently active hook context.
141fn get_current_hook_context() -> HookContext {
142    unsafe { HookContext::from_inner(CURRENT_HOOK_CONTEXT) }
143}
144
145/// Runs a closure with the given `HookContext` set as the active context.
146///
147/// This is called by the renderer before invoking a `DynamicNode`'s
148/// render function, enabling `use_signal` and other hooks to access
149/// and persist state across re-renders.
150///
151/// # Arguments
152///
153/// - `HookContext`: The hook context to set as active.
154/// - `FnOnce() -> R`: The closure to execute with the active context.
155///
156/// # Returns
157///
158/// - `R`: The return value of the closure.
159pub fn with_hook_context<F, R>(context: HookContext, f: F) -> R
160where
161    F: FnOnce() -> R,
162{
163    let previous: *mut HookContextInner = unsafe { CURRENT_HOOK_CONTEXT };
164    unsafe {
165        CURRENT_HOOK_CONTEXT = context.inner;
166    }
167    let result: R = f();
168    unsafe {
169        CURRENT_HOOK_CONTEXT = previous;
170    }
171    result
172}
173
174/// Creates a new `HookContext` allocated via `Box::leak`.
175///
176/// The allocated memory lives for the remainder of the program and will
177/// never be freed. This is acceptable for WASM single-threaded contexts
178/// where `DynamicNode` instances persist for the application lifetime.
179///
180/// # Returns
181///
182/// - `HookContext`: A handle to the newly allocated hook context.
183pub fn create_hook_context() -> HookContext {
184    let ctx: Box<HookContextInner> = Box::default();
185    HookContext::from_inner(Box::leak(ctx) as *mut HookContextInner)
186}
187
188/// Creates a new reactive signal with the given initial value.
189///
190/// When called inside a `DynamicNode` render function (within a
191/// `with_hook_context` scope), the signal state is persisted across
192/// re-renders by storing it in the active `HookContext`. Subsequent
193/// re-renders return the same signal handle, preserving its current value.
194///
195/// When called outside a hook context, a fresh signal is created each time.
196///
197/// # Arguments
198///
199/// - `FnOnce() -> T`: A closure that returns the initial value of the signal.
200///
201/// # Returns
202///
203/// - `Signal<T>`: A mutable handle to the newly created or persisted reactive signal.
204pub fn use_signal<T, F>(init: F) -> Signal<T>
205where
206    T: Clone + PartialEq + 'static,
207    F: FnOnce() -> T,
208{
209    let mut ctx: HookContext = get_current_hook_context();
210    let index: usize = ctx.get_hook_index();
211    ctx.set_hook_index(index + 1_usize);
212    if index < ctx.get_hooks().len()
213        && let Some(existing) = ctx.get_hooks()[index].downcast_ref::<Signal<T>>()
214    {
215        return *existing;
216    }
217    let signal: Signal<T> = {
218        let boxed: Box<SignalInner<T>> = Box::new(SignalInner::new(init()));
219        Signal::from_inner(Box::leak(boxed) as *mut SignalInner<T>)
220    };
221    if index < ctx.get_hooks().len() {
222        ctx.get_mut_hooks()[index] = Box::new(signal);
223    } else {
224        ctx.get_mut_hooks().push(Box::new(signal));
225    }
226    signal
227}
228
229/// Converts a bool signal into a reactive `Signal<String>` that
230/// yields `"true"` or `"false"`, enabling boolean attributes like `checked` to
231/// reactively update the DOM.
232///
233/// # Arguments
234///
235/// - `Signal<bool>`: The source boolean signal to convert.
236///
237/// # Returns
238///
239/// - `AttributeValue`: A signal-backed attribute value that reactively mirrors the boolean as a string.
240pub(crate) fn bool_signal_to_string_attribute_value(source: Signal<bool>) -> AttributeValue {
241    let initial: String = source.get().to_string();
242    let string_signal: Signal<String> = {
243        let inner: SignalInner<String> = SignalInner::new(initial);
244        let boxed: Box<SignalInner<String>> = Box::new(inner);
245        Signal::from_inner(Box::leak(boxed) as *mut SignalInner<String>)
246    };
247    let string_signal_clone: Signal<String> = string_signal;
248    source.subscribe({
249        let source_inner: Signal<bool> = source;
250        move || {
251            let new_value: String = source_inner.get().to_string();
252            string_signal_clone.set(new_value);
253        }
254    });
255    AttributeValue::Signal(string_signal)
256}