fusor-core 0.1.5

Signals, component ownership and the browser DOM runtime for fusor applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Optional, component-local JavaScript modules and explicit Rust inputs.
//!
//! Derive [`JsInputs`] and mark `Signal<T>` fields with `#[js]`. JavaScript gets
//! read-only `get()` and `subscribe(callback)` methods. Subscribe delivers the
//! current value immediately, then one snapshot after each Rust reactive batch.
//! Supported values are bool, String, f64, i32, u32, JsValue, Option and Vec.
//! Use native CustomEvent payloads and [`event_detail`] to send values to Rust.
use crate::{Effect, OwnerHandle, Signal, dom::Scope, effect, untrack};
use js_sys::{Array, Object, Reflect};
use std::{
    cell::{Cell, RefCell},
    rc::Rc,
};
use wasm_bindgen::{JsCast, JsValue, closure::Closure, prelude::wasm_bindgen};
use web_sys::{CustomEvent, Element, Event};

#[wasm_bindgen(module = "/src/js/runtime.js")]
extern "C" {
    fn inputs_create() -> Object;
    fn input_create(read: &JsValue, observe: &JsValue) -> JsValue;
    fn input_api(state: &JsValue) -> JsValue;
    fn input_publish(state: &JsValue, value: &JsValue);
    fn input_dispose(state: &JsValue);
    fn module_create(root: &Element, inputs: &JsValue, id: &str) -> JsValue;
    #[wasm_bindgen(catch)]
    fn module_activate(state: &JsValue) -> Result<(), JsValue>;
    fn module_dispose(state: &JsValue);
}

mod sealed {
    pub trait Sealed {}
}

/// Bounded, checked JavaScript value conversion. This trait is sealed so an
/// exposed input cannot silently acquire arbitrary serialization semantics.
pub trait JsInputValue: sealed::Sealed + Clone + 'static {
    #[doc(hidden)]
    fn to_js(&self) -> JsValue;
    #[doc(hidden)]
    fn from_js(value: &JsValue) -> Result<Self, JsValue>;
}
fn mismatch(expected: &str) -> JsValue {
    JsValue::from_str(&format!("fusor: event detail must be {expected}"))
}
macro_rules! primitive {
    ($ty:ty, $method:ident, $label:literal) => {
        impl sealed::Sealed for $ty {}
        impl JsInputValue for $ty {
            fn to_js(&self) -> JsValue {
                JsValue::from(self.clone())
            }
            fn from_js(value: &JsValue) -> Result<Self, JsValue> {
                value.$method().ok_or_else(|| mismatch($label))
            }
        }
    };
}
primitive!(bool, as_bool, "a boolean");
primitive!(String, as_string, "a string");
primitive!(f64, as_f64, "a number");
macro_rules! integer {
    ($ty:ty, $label:literal) => {
        impl sealed::Sealed for $ty {}
        impl JsInputValue for $ty {
            fn to_js(&self) -> JsValue {
                JsValue::from(*self)
            }
            fn from_js(value: &JsValue) -> Result<Self, JsValue> {
                value
                    .as_f64()
                    .filter(|value| {
                        value.is_finite()
                            && value.fract() == 0.0
                            && *value >= <$ty>::MIN as f64
                            && *value <= <$ty>::MAX as f64
                    })
                    .map(|value| value as $ty)
                    .ok_or_else(|| mismatch($label))
            }
        }
    };
}
integer!(i32, "an integer in the i32 range");
integer!(u32, "an integer in the u32 range");
impl sealed::Sealed for JsValue {}
impl JsInputValue for JsValue {
    fn to_js(&self) -> JsValue {
        self.clone()
    }
    fn from_js(value: &JsValue) -> Result<Self, JsValue> {
        Ok(value.clone())
    }
}
impl<T: JsInputValue> sealed::Sealed for Option<T> {}
impl<T: JsInputValue> JsInputValue for Option<T> {
    fn to_js(&self) -> JsValue {
        self.as_ref().map_or(JsValue::NULL, JsInputValue::to_js)
    }
    fn from_js(value: &JsValue) -> Result<Self, JsValue> {
        if value.is_null() {
            Ok(None)
        } else {
            T::from_js(value).map(Some)
        }
    }
}
impl<T: JsInputValue> sealed::Sealed for Vec<T> {}
impl<T: JsInputValue> JsInputValue for Vec<T> {
    fn to_js(&self) -> JsValue {
        self.iter()
            .map(JsInputValue::to_js)
            .collect::<Array>()
            .into()
    }
    fn from_js(value: &JsValue) -> Result<Self, JsValue> {
        if !Array::is_array(value) {
            return Err(mismatch("an array"));
        }
        Array::from(value)
            .iter()
            .map(|item| T::from_js(&item))
            .collect()
    }
}

/// Read a native CustomEvent payload, checking its entire supported value shape.
/// Null is accepted only by Option; integers reject fractional/out-of-range data.
pub fn event_detail<T: JsInputValue>(event: &Event) -> Result<T, JsValue> {
    let event = event
        .dyn_ref::<CustomEvent>()
        .ok_or_else(|| mismatch("a native CustomEvent"))?;
    T::from_js(&event.detail())
}

/// Fields explicitly exposed to the component's JavaScript module. Usually
/// implemented with `#[derive(fusor::JsInputs)]` and `#[js]` field markers.
pub trait JsInputs {
    #[doc(hidden)]
    fn js_inputs(&self) -> Inputs;
}

trait InputGuard {
    fn close(&self);
    fn set_owner(&self, owner: OwnerHandle);
}
struct InputState<T> {
    signal: RefCell<Option<Signal<T>>>,
    owner: RefCell<Option<OwnerHandle>>,
    observer: RefCell<Option<Effect>>,
    javascript: RefCell<Option<JsValue>>,
    pending: Cell<bool>,
    generation: Cell<u64>,
}
impl<T> InputState<T> {
    fn disposed(&self) -> bool {
        self.owner
            .borrow()
            .as_ref()
            .is_some_and(OwnerHandle::is_disposed)
    }
    fn close(&self) {
        let js = self.javascript.take();
        if let Some(js) = js {
            input_dispose(&js);
        }
        self.pending.set(false);
        let observer = self.observer.take();
        drop(observer);
        self.signal.take();
    }
}

impl<T: JsInputValue> InputState<T> {
    fn read(&self) -> Result<JsValue, JsValue> {
        if self.disposed() {
            return Err(JsValue::from_str(
                "fusor: input belongs to a disposed component",
            ));
        }
        let signal = self
            .signal
            .borrow()
            .clone()
            .ok_or_else(|| JsValue::from_str("fusor: input disposed"))?;
        Ok(signal.with_untracked(JsInputValue::to_js))
    }

    fn stop_observing(&self) {
        self.generation.set(self.generation.get().wrapping_add(1));
        self.pending.set(false);
        let observer = self.observer.take();
        drop(observer);
    }

    fn observe(self: &Rc<Self>) {
        if self.disposed() {
            self.close();
            return;
        }
        if self.observer.borrow().is_some() {
            return;
        }
        let Some(signal) = self.signal.borrow().clone() else {
            return;
        };
        let weak = Rc::downgrade(self);
        let mut first = true;
        let observer = effect(move || {
            // Track only this field; publication runs after ordinary Rust effects.
            signal.with(|_| ());
            if first {
                first = false;
                return;
            }
            if let Some(state) = weak.upgrade() {
                state.queue_publication();
            }
        });
        *self.observer.borrow_mut() = Some(observer);
    }

    fn queue_publication(self: &Rc<Self>) {
        if self.disposed() {
            self.close();
            return;
        }
        if self.pending.replace(true) {
            return;
        }
        let weak = Rc::downgrade(self);
        let generation = self.generation.get();
        crate::reactive::after_flush(move || {
            if let Some(state) = weak.upgrade() {
                state.publish(generation);
            }
        });
    }

    fn publish(&self, generation: u64) {
        if self.disposed() {
            self.close();
            return;
        }
        if generation != self.generation.get() {
            return;
        }
        self.pending.set(false);
        let signal = self.signal.borrow().clone();
        let js = self.javascript.borrow().clone();
        let (Some(signal), Some(js)) = (signal, js) else {
            return;
        };
        if self.observer.borrow().is_none() {
            return;
        }
        let value = signal.with_untracked(JsInputValue::to_js);
        untrack(|| input_publish(&js, &value));
    }
}

struct Input<T> {
    state: Rc<InputState<T>>,
    _read: Closure<dyn Fn() -> Result<JsValue, JsValue>>,
    _observe: Closure<dyn Fn(bool)>,
}
impl<T> InputGuard for Input<T> {
    fn close(&self) {
        self.state.close();
    }
    fn set_owner(&self, owner: OwnerHandle) {
        *self.state.owner.borrow_mut() = Some(owner);
    }
}
impl<T> Drop for Input<T> {
    fn drop(&mut self) {
        self.state.close();
    }
}

/// Generated input declarations; constructing these creates no reactive work.
#[doc(hidden)]
pub struct Inputs {
    object: Object,
    guards: Vec<Box<dyn InputGuard>>,
}
impl Default for Inputs {
    fn default() -> Self {
        Self {
            object: inputs_create(),
            guards: Vec::new(),
        }
    }
}
impl Inputs {
    pub fn add<T: JsInputValue>(&mut self, name: &str, signal: Signal<T>) {
        let state = Rc::new(InputState {
            signal: RefCell::new(Some(signal)),
            owner: RefCell::new(None),
            observer: RefCell::new(None),
            javascript: RefCell::new(None),
            pending: Cell::new(false),
            generation: Cell::new(0),
        });
        let weak = Rc::downgrade(&state);
        let read = Closure::wrap(Box::new(move || {
            let state = weak
                .upgrade()
                .ok_or_else(|| JsValue::from_str("fusor: input disposed"))?;
            state.read()
        }) as Box<dyn Fn() -> Result<JsValue, JsValue>>);
        let weak = Rc::downgrade(&state);
        let observe = Closure::wrap(Box::new(move |active: bool| {
            let Some(state) = weak.upgrade() else {
                return;
            };
            if active {
                state.observe();
            } else {
                state.stop_observing();
            }
        }) as Box<dyn Fn(bool)>);
        let javascript = input_create(read.as_ref(), observe.as_ref());
        Reflect::set(
            &self.object,
            &JsValue::from_str(name),
            &input_api(&javascript),
        )
        .expect("fresh input object");
        *state.javascript.borrow_mut() = Some(javascript);
        self.guards.push(Box::new(Input {
            state,
            _read: read,
            _observe: observe,
        }));
    }
    fn close(&self) {
        for guard in &self.guards {
            guard.close();
        }
    }
}

/// Autoref fallback lets modules without a JsInputs derive receive empty inputs.
#[doc(hidden)]
pub struct InputSource<'a, T>(pub &'a T);
#[doc(hidden)]
pub trait MaybeInputs {
    fn inputs(self) -> Inputs;
}
impl<T> MaybeInputs for &InputSource<'_, T> {
    fn inputs(self) -> Inputs {
        Inputs::default()
    }
}
impl<T: JsInputs> MaybeInputs for InputSource<'_, T> {
    fn inputs(self) -> Inputs {
        self.0.js_inputs()
    }
}

struct Module {
    inputs: Inputs,
    javascript: JsValue,
    closed: Cell<bool>,
}
impl Module {
    fn close(&self) {
        if self.closed.replace(true) {
            return;
        }
        self.inputs.close();
        module_dispose(&self.javascript);
    }
}
impl Drop for Module {
    fn drop(&mut self) {
        self.close();
    }
}

/// Compiler entry point: install once, after bindings but before activation.
#[doc(hidden)]
pub fn mount(scope: &mut Scope, id: &str, inputs: Inputs) -> Result<(), JsValue> {
    if scope.is_coherent() || scope.is_hydrating() {
        return Err(JsValue::from_str(
            "fusor: component JavaScript requires a browser component outside coherent Async and island/server delivery",
        ));
    }
    #[cfg(feature = "islands")]
    if crate::dom::delivery::enabled() {
        return Err(JsValue::from_str(
            "fusor: component JavaScript is unsupported in island delivery",
        ));
    }
    for guard in &inputs.guards {
        guard.set_owner(scope.owner());
    }
    let module = Rc::new(Module {
        javascript: module_create(scope.root(), &inputs.object, id),
        inputs,
        closed: Cell::new(false),
    });
    let weak = Rc::downgrade(&module);
    let cleanup = scope.owner().on_cleanup(move || {
        if let Some(module) = weak.upgrade() {
            untrack(|| module.close());
        }
    });
    let weak = Rc::downgrade(&module);
    let owner: OwnerHandle = scope.owner();
    let activate = owner.on_activate(move || {
        if let Some(module) = weak.upgrade() {
            if let Err(error) = untrack(|| module_activate(&module.javascript)) {
                module.close();
                web_sys::console::error_1(&error);
            }
        }
    });
    scope.retain((module, cleanup, activate));
    Ok(())
}