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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::{
    ElementTarget, JsValue, Scope, document, reconcile, remove_tree, strings, with_native_root,
};
use crate::{Signal, signal, untrack};
use std::collections::BTreeMap;
use wasm_bindgen::JsCast;
use web_sys::{Document, Element, HtmlElement, HtmlInputElement};

type EncodeKey<K> = dyn Fn(&K) -> Result<String, JsValue>;

/// Key extraction and serialization used to match server-rendered rows.
#[doc(hidden)]
pub struct HydratedKeys<F, E> {
    pub key: F,
    pub encode: E,
}

/// How a reconcile finds the previous rows whose keys are gone.
enum Removal<'a, K> {
    /// Merge every key against the ordered map.
    Sorted(reconcile::SortedKeys<'a, K>),
    /// The previous indices of the few removed keys.
    Direct(Vec<usize>),
}

struct Row<T> {
    // Held until the row drops, like the row's scope, even when the scope does
    // not retain its item.
    _state: Signal<T>,
    scope: Scope,
}

impl Scope {
    /// Reconcile a list by stable keys, retaining nodes, focus, and row scopes.
    /// `render` runs once per inserted key. The row signal delivers later values.
    /// This binding owns the container's children. Duplicate keys are errors.
    pub fn keyed<T, K>(
        &mut self,
        target: impl ElementTarget,
        items: impl Fn() -> Vec<T> + 'static,
        key: impl Fn(&T) -> K + 'static,
        render: impl Fn(Signal<T>) -> Result<Scope, JsValue> + 'static,
    ) -> Result<(), JsValue>
    where
        T: Clone + PartialEq + 'static,
        K: Ord + Clone + 'static,
    {
        self.keyed_inner(
            target,
            items,
            key,
            RowFactory {
                render,
                encode: None,
            },
        )
    }

    /// Generated shared templates carry serialized row identities in native HTML.
    #[doc(hidden)]
    pub fn keyed_hydrated<T, K>(
        &mut self,
        target: impl ElementTarget,
        items: impl Fn() -> Vec<T> + 'static,
        keys: HydratedKeys<
            impl Fn(&T) -> K + 'static,
            impl Fn(&K) -> Result<String, JsValue> + 'static,
        >,
        render: impl Fn(Signal<T>) -> Result<Scope, JsValue> + 'static,
    ) -> Result<(), JsValue>
    where
        T: Clone + PartialEq + 'static,
        K: Ord + Clone + 'static,
    {
        self.keyed_inner(
            target,
            items,
            keys.key,
            RowFactory {
                render,
                encode: Some(Box::new(keys.encode)),
            },
        )
    }

    fn keyed_inner<T, K, R>(
        &mut self,
        target: impl ElementTarget,
        items: impl Fn() -> Vec<T> + 'static,
        key: impl Fn(&T) -> K + 'static,
        factory: RowFactory<K, R>,
    ) -> Result<(), JsValue>
    where
        T: Clone + PartialEq + 'static,
        K: Ord + Clone + 'static,
        R: Fn(Signal<T>) -> Result<Scope, JsValue> + 'static,
    {
        let hydrating = self.is_hydrating();
        let mut list = KeyedRows {
            container: target.resolve(self)?,
            rows: BTreeMap::new(),
            order: Vec::new(),
            states: Vec::new(),
            initialized: false,
            settled: false,
            hydrating,
            factory,
            queue: self.mount_queue.clone(),
        };
        self.bind(move || {
            let items = items();
            untrack(|| {
                let keys = items.iter().map(&key).collect();
                list.update(items, keys)
            })
        })
    }
}

struct RowFactory<K, R> {
    render: R,
    encode: Option<Box<EncodeKey<K>>>,
}

struct KeyedRows<T, K, R> {
    container: Element,
    // The map orders cleanup by key; order and states mirror DOM order.
    rows: BTreeMap<K, Row<T>>,
    order: Vec<K>,
    states: Vec<Signal<T>>,
    initialized: bool,
    settled: bool,
    hydrating: bool,
    factory: RowFactory<K, R>,
    queue: Option<std::rc::Rc<super::commit::CommitQueue>>,
}

struct PreparedRows<T, K> {
    rows: BTreeMap<K, Row<T>>,
    positions: Vec<usize>,
    states: Vec<Signal<T>>,
}

impl<T, K, R> KeyedRows<T, K, R>
where
    T: Clone + PartialEq + 'static,
    K: Ord + Clone + 'static,
    R: Fn(Signal<T>) -> Result<Scope, JsValue>,
{
    fn update(&mut self, items: Vec<T>, keys: Vec<K>) -> Result<(), JsValue> {
        let (retained, removal) = self.plan(&keys)?;
        let native_rows = self.native_rows(&keys)?;
        let document = document()?;
        let focused = focused(&document, &self.container);
        let mut staged = self.prepare_rows(&items, &keys, &retained, &native_rows)?;
        self.initialize();
        let next = self.reorder_states(&retained, staged.states);
        self.remove_rows(removal);
        // Settled rows need committing again only when descendants queued setup.
        let fresh_keys = self.settled.then(|| staged.rows.keys().cloned().collect());
        self.settled = false;
        self.merge_rows(&mut staged.rows);
        let stationary = reconcile::stationary(&staged.positions);
        for (state, item) in next.iter().zip(items) {
            state.set(item);
        }
        (self.order, self.states) = (keys, next);
        self.position_rows(&stationary)?;
        self.commit_rows(fresh_keys);
        self.settled = true;
        restore_focus(&document, &self.container, focused)
    }

    fn plan<'a>(&self, keys: &'a [K]) -> Result<(Vec<usize>, Removal<'a, K>), JsValue> {
        let duplicate = || JsValue::from_str("fusor: duplicate key in list");
        // Small edits resolve their changed keys directly; other edits use a sorted merge.
        match reconcile::small_edit(&self.order, keys, |key| self.rows.contains_key(key)) {
            Some(plan) => {
                let reconcile::EditPlan { positions, removed } = plan.map_err(|()| duplicate())?;
                Ok((positions, Removal::Direct(removed)))
            }
            None => {
                let unique = reconcile::SortedKeys::new(keys).ok_or_else(duplicate)?;
                let positions = reconcile::previous_positions(&self.order, &unique);
                Ok((positions, Removal::Sorted(unique)))
            }
        }
    }

    fn native_rows(&self, keys: &[K]) -> Result<Vec<Element>, JsValue> {
        if !self.hydrating || self.initialized {
            return Ok(Vec::new());
        }
        let encode =
            self.factory.encode.as_ref().ok_or_else(|| {
                JsValue::from_str("hydrated lists require generated key metadata")
            })?;
        server_rows(&self.container, keys, encode)
    }

    fn prepare_rows(
        &self,
        items: &[T],
        keys: &[K],
        retained: &[usize],
        native_rows: &[Element],
    ) -> Result<PreparedRows<T, K>, JsValue> {
        // Stage and validate every scope before changing the visible list.
        let mut staged = PreparedRows {
            rows: BTreeMap::new(),
            positions: retained.to_vec(),
            states: Vec::new(),
        };
        for (index, (key, item)) in keys.iter().zip(items).enumerate() {
            if retained[index] != reconcile::NEW {
                continue;
            }
            let state = signal(item.clone());
            let native = native_rows.get(index);
            let scope = with_native_root(native, || (self.factory.render)(state.clone()))?;
            // Adopted rows already occupy their final positions.
            if native.is_some() {
                staged.positions[index] = index;
            }
            staged.states.push(state.clone());
            staged.rows.insert(
                key.clone(),
                Row {
                    _state: state,
                    scope,
                },
            );
        }
        for row in staged.rows.values() {
            row.scope.finish_prepare()?;
        }
        Ok(staged)
    }

    fn initialize(&mut self) {
        if self.initialized {
            return;
        }
        if !self.hydrating {
            #[cfg(feature = "islands")]
            super::delivery::dispose_tree(&self.container);
            self.container.set_text_content(None);
        }
        self.initialized = true;
    }

    fn reorder_states(&mut self, retained: &[usize], fresh: Vec<Signal<T>>) -> Vec<Signal<T>> {
        // Release the old order before removed rows drop their remaining references.
        let mut previous: Vec<_> = std::mem::take(&mut self.states)
            .into_iter()
            .map(Some)
            .collect();
        let mut fresh = fresh.into_iter();
        retained
            .iter()
            .map(|&position| match position {
                reconcile::NEW => fresh.next().expect("staged row"),
                position => previous[position].take().expect("retained row"),
            })
            .collect()
    }

    fn remove_rows(&mut self, mut removal: Removal<'_, K>) {
        match &mut removal {
            Removal::Sorted(unique) => self.rows.retain(|key, row| {
                let keep = unique.contains_next(key);
                if !keep {
                    remove_tree(&row.scope.root);
                }
                keep
            }),
            Removal::Direct(removed) => {
                // Match retain's ascending key order and detach before dropping the key.
                reconcile::sort_few(removed, &self.order);
                for &index in removed.iter() {
                    if let Some(entry) = self.rows.remove_entry(&self.order[index]) {
                        remove_tree(&entry.1.scope.root);
                        drop(entry);
                    }
                }
            }
        }
    }

    fn merge_rows(&mut self, staged: &mut BTreeMap<K, Row<T>>) {
        if self.rows.is_empty() {
            self.rows = std::mem::take(staged);
        } else if staged.len() <= self.rows.len() / (self.rows.len().ilog2() as usize + 1) {
            // Sparse insertions preserve the existing tree; dense insertions merge linearly.
            self.rows.extend(std::mem::take(staged));
        } else {
            self.rows.append(staged);
        }
    }

    fn position_rows(&self, stationary: &[bool]) -> Result<(), JsValue> {
        for (index, keep) in stationary.iter().enumerate().rev() {
            if !keep {
                let anchor = self
                    .order
                    .get(index + 1)
                    .map(|key| self.rows[key].scope.root.as_ref());
                strings::insert_before(
                    &self.container,
                    &self.rows[&self.order[index]].scope.root,
                    anchor,
                )?;
            }
        }
        Ok(())
    }

    fn commit_rows(&self, fresh_keys: Option<Vec<K>>) {
        let idle = self.queue.as_ref().is_none_or(|queue| queue.is_idle());
        match fresh_keys.filter(|_| idle) {
            Some(keys) => {
                for key in &keys {
                    self.rows[key].scope.commit();
                }
            }
            None => {
                for row in self.rows.values() {
                    row.scope.commit();
                }
            }
        }
    }
}

/// Adopt the server-rendered rows, which must match `keys` in order.
fn server_rows<K>(
    container: &Element,
    keys: &[K],
    encode: &EncodeKey<K>,
) -> Result<Vec<Element>, JsValue> {
    // Compare every row natively in one call. Keys encode in order: a failed
    // encoding is reported after the rows before it and its own row are
    // checked, as when comparing one row at a time.
    let mut encoded = String::new();
    let mut failure = None;
    let mut count = 0;
    for key in keys {
        match encode(key) {
            // A separator inside an encoding needs the one-at-a-time path.
            Ok(value) if value.contains('\n') => {
                return server_rows_one_by_one(container, keys, encode);
            }
            Ok(value) => {
                if count > 0 {
                    encoded.push('\n');
                }
                encoded.push_str(&value);
                count += 1;
            }
            Err(error) => {
                failure = Some(error);
                break;
            }
        }
    }
    let rows = strings::server_rows(container, &encoded, count as u32, failure.is_none())?;
    if let Some(error) = failure {
        return Err(error);
    }
    Ok((0..count as u32)
        .map(|index| rows.get(index).unchecked_into())
        .collect())
}

fn server_rows_one_by_one<K>(
    container: &Element,
    keys: &[K],
    encode: &EncodeKey<K>,
) -> Result<Vec<Element>, JsValue> {
    let mut node = container.first_element_child();
    let mut rows = Vec::with_capacity(keys.len());
    for key in keys {
        let row = node
            .take()
            .ok_or_else(|| JsValue::from_str("missing native row"))?;
        if strings::attribute(&row, strings::Name::Key).as_deref() != Some(encode(key)?.as_str()) {
            return Err(JsValue::from_str("native row key mismatch"));
        }
        node = row.next_element_sibling();
        rows.push(row);
    }
    if node.is_some() {
        return Err(JsValue::from_str("unexpected native row"));
    }
    Ok(rows)
}

type Selection = (u32, u32, String);

/// The focused descendant of `container` and, for an input, its selection.
fn focused(document: &Document, container: &Element) -> Option<(HtmlElement, Option<Selection>)> {
    let focused = document
        .active_element()
        .filter(|node| container.contains(Some(node)))?
        .dyn_into::<HtmlElement>()
        .ok()?;
    let selection = focused.dyn_ref::<HtmlInputElement>().and_then(|input| {
        Some((
            input.selection_start().ok()??,
            input.selection_end().ok()??,
            input.selection_direction().ok()??,
        ))
    });
    Some((focused, selection))
}

// insertBefore can blur a node even when moving it within the same list.
// Restore focus only if that original node survives.
fn restore_focus(
    document: &Document,
    container: &Element,
    focused: Option<(HtmlElement, Option<Selection>)>,
) -> Result<(), JsValue> {
    let Some((focused, selection)) = focused.filter(|(node, _)| container.contains(Some(node)))
    else {
        return Ok(());
    };
    if document
        .active_element()
        .is_some_and(|node| node.is_same_node(Some(&focused)))
    {
        return Ok(());
    }
    focused.focus()?;
    if let (Some(input), Some((start, end, direction))) =
        (focused.dyn_ref::<HtmlInputElement>(), selection)
    {
        input.set_selection_range_with_direction(start, end, &direction)?;
    }
    Ok(())
}