whisker-runtime 0.16.0

Host-independent Whisker runtime: reactivity, retained elements, layout, and frames.
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! Component scoping, lifecycle, and hot-reload owner registry.
//!
//! Users normally interact with this module through the
//! `#[component]` proc-macro, which expands a function definition
//! into a body that:
//!
//! 1. Creates a fresh owner with [`mount_component`].
//! 2. Runs the user's body inside that owner.
//! 3. Returns the resulting view, leaving the owner alive (the parent
//!    keeps the handle; disposing the parent will cascade).
//!
//! The macro also passes its own fn pointer into the mount registry so the
//! hot-reload path can map subsecond-patched fn pointers back to live owners.
//!
//! Lifecycle hooks:
//!
//! - [`on_mount`] — registered against the current owner; fires once
//!   on the next [`flush_mounts`], which runs after the component's
//!   view has been appended to its parent.
//! - `on_cleanup` lives in `owner.rs` — symmetric LIFO callback that
//!   fires when the owner is disposed.

use std::rc::Rc;

use super::runtime::Owner;
use super::{untrack, with_runtime};
use crate::view::Element;

/// Mount a component: create a fresh child owner, register `fn_ptr`
/// against it for hot reload, run `body` inside that owner, and
/// return both the owner id and the body's result.
///
/// The caller is responsible for keeping the returned `Owner` alive
/// (e.g. attaching it to the parent component's owner-children list
/// via the renderer) and for disposing it when the component
/// unmounts. The owner is already linked as a child of the
/// current-owner-at-call-time, so calling [`Owner::dispose`] on an
/// ancestor will cascade.
pub fn mount_component<R>(fn_ptr: *const (), body: impl FnOnce() -> R) -> (Owner, R) {
    let owner = Owner::new(None);
    with_runtime(|rt| {
        if let Some(o) = rt.owners.get_mut(owner) {
            o.mount_fn = Some(fn_ptr);
        }
        rt.component_owners.entry(fn_ptr).or_default().push(owner);
    });
    // A component's reactive dependencies must come from the explicit
    // `effect` / `computed` calls inside its body. Without `untrack`, a
    // bare `signal.get()` in the body subscribes whatever outer node is
    // constructing us (a parent's `Show` effect, a route mount).
    let result = untrack(|| owner.with(body));
    (owner, result)
}

/// Dispose a component owner *and* deregister it from
/// `component_owners`. Use this instead of plain `Owner::dispose` for
/// owners created via `mount_component`.
pub fn unmount_component(owner: Owner) {
    let fn_ptr = with_runtime(|rt| rt.owners.get(owner).and_then(|o| o.mount_fn));
    if let Some(fp) = fn_ptr {
        with_runtime(|rt| {
            if let Some(list) = rt.component_owners.get_mut(&fp) {
                list.retain(|o| *o != owner);
                if list.is_empty() {
                    rt.component_owners.remove(&fp);
                }
            }
        });
    }
    owner.dispose();
}

/// Register `f` as a post-mount callback for the current owner. Fires
/// once on the next [`flush_mounts`] call, which runs after the
/// component's view is appended to its parent.
/// Disposing the Owner before delivery cancels the callback.
///
/// No-op (with debug-build warning) if there is no current owner.
pub fn on_mount(f: impl FnOnce() + 'static) {
    if Owner::current().is_none() {
        super::warn_no_owner("on_mount");
        return;
    }
    let callback = crate::lifetime::Scoped::new(Some(f));
    with_runtime(|rt| {
        rt.pending_mounts.push(Box::new(move || {
            callback.with_mut(|f| f.take().expect("mount callback runs once")());
        }))
    });
}

/// Run all queued on_mount callbacks in registration order. Called
/// after a batch of component views has been appended to the tree.
/// Safe to call when the queue is empty (no-op).
pub fn flush_mounts() {
    // Drained under a short borrow so callbacks that register their own
    // `on_mount` land in a fresh queue.
    let queue: Vec<Box<dyn FnOnce()>> = with_runtime(|rt| std::mem::take(&mut rt.pending_mounts));
    for cb in queue {
        // An `on_mount` may read signals to inspect post-mount state
        // but must never subscribe whatever node is on the stack at
        // drain time — an integration is free to call `flush_mounts`
        // from inside a reactive scope, so the queue enforces it.
        untrack(cb);
    }
}

/// Look up the owners currently associated with `fn_ptr` — which live
/// owners need disposal + remount when subsecond patches a component
/// function body. Returns a snapshot; later mutations of the runtime's
/// `component_owners` don't affect the returned `Vec`.
#[doc(hidden)]
pub fn owners_for_fn(fn_ptr: *const ()) -> Vec<Owner> {
    with_runtime(|rt| {
        rt.component_owners
            .get(&fn_ptr)
            .cloned()
            .unwrap_or_default()
    })
}

// Per-component remount. `mount_component_remountable` returns the
// body's root element directly — no wrapper element sits between a
// component body and its parent, so the Whisker component tree maps
// 1:1 onto the Host element tree.
//
// With no wrapper to serve as a stable placeholder, each mount's
// `(parent, previous_sibling)` is captured lazily: the mount stashes
// its `MountId` + body_root in `PENDING_MOUNTS`, and `view::append_child`
// calls back through [`on_component_root_attached`] once that root is
// attached.
//
// Known limitations:
// - The anchor must outlive the remount. A sibling-managed component
//   that disposed itself between mount and patch leaves a stale anchor,
//   and remount falls back to the previous numeric position.
// - Component-local signal state is lost on remount; context-stored
//   state survives, its owners being above the disposed scope.
// - Props must implement `Clone` so the body closure can hand user code
//   fresh owned values on each invocation.

use std::cell::RefCell;

pub(crate) type PendingMounts = Vec<(MountId, Element)>;

thread_local! {
    /// Mounts whose body_root has not been attached yet, consumed by the
    /// matching `view::append_child`. Several are pending at once because
    /// `render!` builds every sibling before attaching any of them.
    static PENDING_MOUNTS: RefCell<PendingMounts> = const { RefCell::new(Vec::new()) };
}

pub(crate) fn swap_pending_mounts(pending: &mut PendingMounts) {
    PENDING_MOUNTS.with_borrow_mut(|active| std::mem::swap(active, pending));
}

/// Drops pending mounts that will never be attached, such as the
/// application root installed through `view::set_root`.
pub(crate) fn discard_pending_mounts() {
    PENDING_MOUNTS.with_borrow_mut(Vec::clear);
}

#[cfg(test)]
pub(crate) fn pending_mount_count() -> usize {
    PENDING_MOUNTS.with_borrow(Vec::len)
}

pub(crate) fn forget_pending_mounts(ids: &[MountId]) {
    if ids.is_empty() {
        return;
    }
    PENDING_MOUNTS.with_borrow_mut(|pending| pending.retain(|(id, _)| !ids.contains(id)));
}

/// Stable identifier for a remountable mount site. Generationless on
/// purpose — entries are removed when the site is torn down, so the
/// monotonic counter never collides for live entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MountId(pub(crate) u64);

type SourceHashFn = Rc<dyn Fn() -> u64 + 'static>;

/// One live remountable component mount.
pub(crate) struct MountSite {
    /// Function pointer of the component fn that produced this mount.
    /// Used for the patched-fn lookup at hot-reload time.
    pub fn_ptr: *const (),
    /// User body closure. `Rc` so the remount path can clone the
    /// handle out of the runtime borrow before invoking it (the body
    /// re-enters the runtime via `view::*` / `signal()` / etc., so
    /// holding the runtime borrow across the call would deadlock).
    pub body: Rc<dyn Fn() -> Element + 'static>,
    /// Current owner — `Some` between mounts, `None` during the
    /// dispose-then-remount window.
    pub owner: Option<Owner>,
    /// Element handle the body returned for its outermost element.
    /// Detached from the parent at the start of each remount, then
    /// replaced by the new body's root inserted at the same slot.
    pub body_root: Option<Element>,
    /// Parent element this component is attached to. `None` until
    /// `view::append_child` fires for the body_root for the first
    /// time. `Some(_)` thereafter, kept up to date across remounts.
    pub parent: Option<Element>,
    /// Element handle that was the body_root's immediate predecessor
    /// in `parent`'s child list at attach time. `None` if the body
    /// was the first child of parent. Stable across remounts unless
    /// the anchor itself is removed by some other code path.
    pub anchor: Option<Element>,
    /// Reads the component's props-layout hash *through subsecond
    /// dispatch* (the `#[component]` macro bakes the hash of the
    /// props signature into a generated fn). After a patch this
    /// returns the patch dylib's value.
    pub props_hash_fn: Rc<dyn Fn() -> u64 + 'static>,
    /// The layout hash as of when this site's `body` closure was
    /// created. If `props_hash_fn()` disagrees after a patch, the
    /// stored closure's captured environment no longer matches what
    /// the patched body code expects — re-running it would transmute
    /// across mismatched layouts (garbage props / UB), so the site
    /// must not be remounted in place.
    pub props_hash: u64,
    /// Reads the component's source hash through subsecond dispatch, so
    /// after a patch it returns the hash compiled into the newest code.
    pub body_hash_fn: SourceHashFn,
    /// Source hash of the code this site's current subtree was built from.
    pub body_hash: u64,
}

/// Called by `view::append_child` after every successful attach.
/// If there's a pending component mount whose body_root matches the
/// just-attached `child`, finalise its MountSite by recording the
/// parent + previous-sibling anchor.
///
/// Every pending mount whose body_root is `child` is finalised: a
/// component whose body is exactly another component shares its root.
/// No-op if none match.
pub fn on_component_root_attached(parent: Element, child: Element) {
    let mount_ids: Vec<MountId> = PENDING_MOUNTS.with_borrow_mut(|pending| {
        let mut attached = Vec::new();
        pending.retain(|(mount_id, root)| {
            let matches = *root == child;
            if matches {
                attached.push(*mount_id);
            }
            !matches
        });
        attached
    });
    if mount_ids.is_empty() {
        return;
    }
    let anchor = crate::view::previous_sibling(parent, child);
    super::with_runtime(|rt| {
        for mount_id in &mount_ids {
            if let Some(site) = rt.mount_sites.get_mut(mount_id) {
                site.parent = Some(parent);
                site.anchor = anchor;
            }
        }
    });
}

/// Test/internal: clear the pending-mount slot. Use between
/// scenarios that share a thread.
#[doc(hidden)]
pub fn __reset_pending_mount_for_tests() {
    discard_pending_mounts();
}

/// Mount a component with full remount support — wrapper-less.
///
/// Runs `body` inside a fresh owner and returns the body's root
/// element directly to the caller. No wrapper element is created, so
/// the Whisker component tree maps 1:1 with the Host element tree.
///
/// To make remount work without a stable wrapper handle in the
/// parent's child list, the function stashes a pending-mount entry
/// in a thread-local just before returning. The next
/// next `view::append_child` call that sees this body root being
/// attached finalises the MountSite (recording parent + previous
/// sibling). The [`on_component_root_attached`] callback handles
/// that side of the handshake.
///
/// On a subsecond patch matching `fn_ptr`, the runtime calls
/// [`remount_components_for`] which disposes the current owner,
/// re-invokes `body` in a new owner, removes the old body_root
/// from its parent, and inserts the new body_root at the same slot
/// (using the recorded anchor).
///
/// `props_hash_fn` reads the component's props-layout hash through
/// subsecond dispatch (through the internal `MountSite::props_hash_fn`); the value
/// it returns *now* is recorded as the layout this site's `body`
/// closure was built against. `body_hash_fn` reads the component's
/// source hash the same way; [`remount_changed_components`] remounts the
/// site when it moves. Non-hot-reload callers (tests) can pass
/// `Box::new(|| 0)` for both.
pub fn mount_component_remountable<F>(
    fn_ptr: *const (),
    body: F,
    props_hash_fn: Box<dyn Fn() -> u64 + 'static>,
    body_hash_fn: Box<dyn Fn() -> u64 + 'static>,
) -> Element
where
    F: Fn() -> Element + 'static,
{
    let body: Rc<dyn Fn() -> Element + 'static> = Rc::new(body);
    let props_hash_fn: Rc<dyn Fn() -> u64 + 'static> = Rc::from(props_hash_fn);
    let props_hash = props_hash_fn();
    let body_hash_fn: SourceHashFn = Rc::from(body_hash_fn);
    let body_hash = body_hash_fn();

    let body_for_first = body.clone();
    let owner = Owner::new(None);
    with_runtime(|rt| {
        if let Some(o) = rt.owners.get_mut(owner) {
            o.mount_fn = Some(fn_ptr);
        }
        rt.component_owners.entry(fn_ptr).or_default().push(owner);
    });
    // See `mount_component` for why the body runs untracked.
    let body_root = mount_body(owner, || (*body_for_first)());

    // `parent` / `anchor` stay `None` until the next
    // `view::append_child` attaches `body_root` and calls back through
    // `on_component_root_attached`.
    let mount_id = with_runtime(|rt| {
        rt.mount_id_counter += 1;
        let id = MountId(rt.mount_id_counter);
        rt.mount_sites.insert(
            id,
            MountSite {
                fn_ptr,
                body,
                owner: Some(owner),
                body_root: Some(body_root),
                parent: None,
                anchor: None,
                props_hash_fn,
                props_hash,
                body_hash_fn,
                body_hash,
            },
        );
        rt.fn_ptr_mounts.entry(fn_ptr).or_default().push(id);
        id
    });

    // The caller's `view::append_child(parent, body_root)` consumes
    // this and binds parent + anchor.
    PENDING_MOUNTS.with_borrow_mut(|pending| pending.push((mount_id, body_root)));

    body_root
}

/// Runs a component body in `owner`; a panic routed to a boundary leaves
/// an empty placeholder so the parent's render continues.
fn mount_body(owner: Owner, body: impl FnOnce() -> Element) -> Element {
    super::error::catch(Some(owner), super::error::ErrorPhase::Render, || {
        untrack(|| owner.with(body))
    })
    .unwrap_or_else(|| owner.with(crate::view::create_phantom_element))
}

/// Re-mount every remountable site whose `fn_ptr` is in the given
/// list.
///
/// The whole list is remounted as one batch: every old body root is
/// detached first, then each site's owner is disposed and its body
/// re-run, and finally the new roots are inserted at the indices their
/// old roots held. Non-remounted siblings never move, so sibling order
/// and their native state survive the patch.
///
/// Returns [`RemountStats`]: how many sites were remounted, and how
/// many were *refused* because their props layout changed. The
/// bootstrap escalates on either — `remounted == 0` means the patch had
/// no attached component to reflect through, and `layout_changed > 0`
/// means at least one stored body closure can no longer be re-run
/// safely.
pub fn remount_components_for(patched_fns: &[*const ()]) -> RemountStats {
    if patched_fns.is_empty() {
        return RemountStats::default();
    }
    // A site whose ancestor component is in the same batch is skipped:
    // remounting the ancestor rebuilds the whole subtree, so handling
    // the descendant separately either works on stale parent state or
    // no-ops against a cascade-disposed owner.
    let patched_set: std::collections::HashSet<*const ()> = patched_fns.iter().copied().collect();
    let ids: Vec<MountId> = with_runtime(|rt| {
        let mut candidates: Vec<MountId> = Vec::new();
        for fp in patched_fns {
            if let Some(list) = rt.fn_ptr_mounts.get(fp) {
                for id in list {
                    if !candidates.contains(id) {
                        candidates.push(*id);
                    }
                }
            }
        }
        candidates
            .into_iter()
            .filter(|mount_id| {
                let site = match rt.mount_sites.get(mount_id) {
                    Some(s) => s,
                    None => return false,
                };
                let mut cursor = match site.owner {
                    Some(o) => o,
                    None => return false,
                };
                while let Some(parent) = rt.owners.get(cursor).and_then(|o| o.parent) {
                    if let Some(mf) = rt.owners.get(parent).and_then(|o| o.mount_fn)
                        && patched_set.contains(&mf)
                    {
                        return false;
                    }
                    cursor = parent;
                }
                true
            })
            .collect()
    });
    remount_sites(ids)
}

/// Re-mount every remountable site whose component source changed since
/// its subtree was built. Called by the hot-reload integrations after a
/// successful subsecond patch.
///
/// A patch rebuilds the whole crate, so the patched-function list names
/// every component in it; comparing each site's source hash is what
/// narrows the remount to the components the edit actually touched. A
/// changed site whose ancestor also changed is covered by the ancestor's
/// remount. `remounted == 0` means the edit reached no component body
/// (e.g. a helper function) and the caller must fall back to a root
/// remount.
pub fn remount_changed_components() -> RemountStats {
    // Collected under the borrow, evaluated outside it: the hash getter
    // re-enters subsecond dispatch.
    let sites: Vec<(MountId, SourceHashFn, u64)> = with_runtime(|rt| {
        rt.mount_sites
            .iter()
            .filter(|(_, site)| site.owner.is_some())
            .map(|(id, site)| (*id, site.body_hash_fn.clone(), site.body_hash))
            .collect()
    });
    let changed: Vec<MountId> = sites
        .into_iter()
        .filter(|(_, body_hash_fn, body_hash)| body_hash_fn() != *body_hash)
        .map(|(id, _, _)| id)
        .collect();
    if changed.is_empty() {
        return RemountStats::default();
    }

    let mut ids: Vec<MountId> = with_runtime(|rt| {
        let changed_owners: std::collections::HashSet<Owner> = changed
            .iter()
            .filter_map(|id| rt.mount_sites.get(id).and_then(|site| site.owner))
            .collect();
        changed
            .into_iter()
            .filter(|mount_id| {
                let Some(mut cursor) = rt.mount_sites.get(mount_id).and_then(|site| site.owner)
                else {
                    return false;
                };
                while let Some(parent) = rt.owners.get(cursor).and_then(|o| o.parent) {
                    if changed_owners.contains(&parent) {
                        return false;
                    }
                    cursor = parent;
                }
                true
            })
            .collect()
    });
    ids.sort_by_key(|id| id.0);
    remount_sites(ids)
}

fn remount_sites(ids: Vec<MountId>) -> RemountStats {
    if ids.is_empty() {
        return RemountStats::default();
    }

    // The batch must be one operation rather than a per-site loop:
    // a site's `anchor` is a sibling's body_root, so remounting siblings
    // one at a time detaches the anchors the later ones depend on and
    // they all fall back to index 0, clumping at the top of the parent
    // in hash-iteration order.

    struct RemountInfo {
        mount_id: MountId,
        parent: Element,
        old_body_root: Element,
        body: Rc<dyn Fn() -> Element + 'static>,
        fn_ptr: *const (),
        props_hash_fn: Rc<dyn Fn() -> u64 + 'static>,
        props_hash: u64,
        body_hash_fn: SourceHashFn,
    }

    let infos: Vec<RemountInfo> = with_runtime(|rt| {
        ids.iter()
            .filter_map(|mid| {
                let site = rt.mount_sites.get(mid)?;
                Some(RemountInfo {
                    mount_id: *mid,
                    parent: site.parent?,
                    old_body_root: site.body_root?,
                    body: site.body.clone(),
                    fn_ptr: site.fn_ptr,
                    props_hash_fn: site.props_hash_fn.clone(),
                    props_hash: site.props_hash,
                    body_hash_fn: site.body_hash_fn.clone(),
                })
            })
            .collect()
    });

    // `props_hash_fn()` dispatches into the freshly applied patch and
    // returns the layout the *new* code was compiled for. A mismatch
    // against the site's stored hash means the patch changed the props
    // signature, and re-invoking the old closure would transmute across
    // mismatched capture layouts (garbage props / UB); the caller
    // escalates those to a full remount instead. Evaluated OUTSIDE
    // `with_runtime` — the hash getter re-enters subsecond dispatch.
    let (infos, layout_changed): (Vec<RemountInfo>, Vec<RemountInfo>) = infos
        .into_iter()
        .partition(|info| (info.props_hash_fn)() == info.props_hash);
    let layout_changed = layout_changed.len();

    if infos.is_empty() {
        return RemountStats {
            remounted: 0,
            layout_changed,
        };
    }

    // Snapshot each unique parent's child list before anything mutates.
    let mut parent_snapshot: std::collections::HashMap<Element, Vec<Element>> =
        std::collections::HashMap::new();
    for info in &infos {
        parent_snapshot
            .entry(info.parent)
            .or_insert_with(|| crate::view::children_of(info.parent));
    }

    // Detach every old body_root *before* any dispose runs.
    // `Owner::dispose` invalidates element handles, after which
    // `remove_child` silently no-ops after the handle is invalid and the stale subtree
    // stays on screen.
    let mut by_parent: std::collections::HashMap<Element, Vec<(Element, Option<Element>)>> =
        std::collections::HashMap::new();
    for info in &infos {
        crate::view::remove_child(info.parent, info.old_body_root);
        by_parent
            .entry(info.parent)
            .or_default()
            .push((info.old_body_root, None));
    }

    let mut results: Vec<(MountId, Element, Element, Element, Owner, u64)> =
        Vec::with_capacity(infos.len());
    for info in infos {
        let old_owner = with_runtime(|rt| {
            let site = rt.mount_sites.get_mut(&info.mount_id)?;
            site.body_root.take();
            site.owner.take()
        });
        // Remount runs with an empty owner stack; inherit the old
        // owner's parent or app-root contexts are lost.
        let parent_owner =
            old_owner.and_then(|o| with_runtime(|rt| rt.owners.get(o).and_then(|s| s.parent)));
        if let Some(o) = old_owner {
            o.dispose();
        }

        let new_owner = Owner::new(parent_owner);
        with_runtime(|rt| {
            if let Some(o) = rt.owners.get_mut(new_owner) {
                o.mount_fn = Some(info.fn_ptr);
            }
            rt.component_owners
                .entry(info.fn_ptr)
                .or_default()
                .push(new_owner);
        });
        // `untrack` so the body's signal reads register against its own
        // nested `effect`/`computed`s, not against whatever scheduler
        // context was active when the tick called into us.
        let new_body_root = mount_body(new_owner, || (*info.body)());

        if let Some(list) = by_parent.get_mut(&info.parent)
            && let Some(entry) = list
                .iter_mut()
                .find(|(o, n)| *o == info.old_body_root && n.is_none())
        {
            entry.1 = Some(new_body_root);
        }

        results.push((
            info.mount_id,
            info.parent,
            info.old_body_root,
            new_body_root,
            new_owner,
            (info.body_hash_fn)(),
        ));
    }

    for (parent, pairs) in &by_parent {
        let snapshot = parent_snapshot.get(parent).cloned().unwrap_or_default();
        let old_to_new: std::collections::HashMap<Element, Element> = pairs
            .iter()
            .filter_map(|(o, n)| n.map(|new_root| (*o, new_root)))
            .collect();

        // Snapshot with each old root replaced by its matching new one,
        // leaving non-replaced siblings untouched.
        let desired: Vec<Element> = snapshot
            .iter()
            .map(|c| old_to_new.get(c).copied().unwrap_or(*c))
            .collect();

        // Ascending order matters: inserting at index `i` shifts only
        // the elements from `i` onwards, so earlier placements stay put.
        let new_set: std::collections::HashSet<Element> =
            pairs.iter().filter_map(|(_, n)| *n).collect();
        for (idx, child) in desired.iter().enumerate() {
            if new_set.contains(child) {
                crate::view::insert_child_at(*parent, *child, idx);
            }
        }
    }

    for (mount_id, _, old_root, new_root, new_owner, body_hash) in &results {
        with_runtime(|rt| {
            // Enclosing components that render this one directly share its root.
            for site in rt.mount_sites.values_mut() {
                if site.body_root == Some(*old_root) {
                    site.body_root = Some(*new_root);
                }
            }
            if let Some(site) = rt.mount_sites.get_mut(mount_id) {
                site.owner = Some(*new_owner);
                site.body_root = Some(*new_root);
                site.body_hash = *body_hash;
            }
        });
    }

    // Refresh anchors from the now-final child order, or a future solo
    // patch of one of these siblings inherits a stale anchor and falls
    // back to index 0.
    for (mount_id, parent, _, new_root, _, _) in &results {
        let new_anchor = crate::view::previous_sibling(*parent, *new_root);
        with_runtime(|rt| {
            if let Some(site) = rt.mount_sites.get_mut(mount_id) {
                site.anchor = new_anchor;
            }
        });
    }

    RemountStats {
        remounted: results.len(),
        layout_changed,
    }
}

/// What [`remount_components_for`] did with one patch — the
/// bootstrap's full-remount escalation input.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RemountStats {
    /// Mount sites disposed and re-mounted in place.
    pub remounted: usize,
    /// Mount sites *refused* because their props layout changed
    /// between the stored body closure and the patched code (see the internal
    /// `MountSite::props_hash`). Non-zero means the on-screen
    /// subtree for those sites is stale and only a full remount can
    /// safely rebuild it.
    pub layout_changed: usize,
}