smudge-core 0.1.0

Widget tree, reactive bindings, layout, paint and damage tracking for the Smudge GUI library
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! The signal core: one thread-local runtime holding every reactive node.
//!
//! This module is crate-private. The public types in the parent module are
//! thin handles over what is defined here. A node in the arena holds only
//! bookkeeping, and the typed value of a signal or a memo lives in the handle
//! that names it, so a steady write and flush allocates nothing once the
//! arena and its lists have grown.
//!
//! No closure is ever called while the runtime is borrowed. Every step that
//! touches the runtime is a short re-entrant call, so an effect body can
//! write signals and read memos freely.

use std::any::{Any, TypeId};
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::rc::{Rc, Weak};

use slotmap::{SlotMap, new_key_type};

use crate::reactive::Owner;

new_key_type! {
    /// Identifies one node of the arena: a signal, a memo or an effect.
    ///
    /// The key is generational, so an id whose node was disposed never names
    /// another node. Every access checks liveness first.
    pub(crate) struct NodeId;
}

/// How many times one effect may run in one flush before it counts as a cycle.
const MAX_RUNS_PER_FLUSH: u32 = 16;

/// The wake callback of one root owner tree.
///
/// Every effect records the scope it was created under, so a write wakes the
/// redraw request of the app whose tree it belongs to and not another app's.
pub(crate) struct Scope {
    /// The callback that the first wake between flushes runs.
    pub(crate) wake: RefCell<Option<Rc<dyn Fn()>>>,
    /// Whether the callback ran since the last flush ended.
    ///
    /// Kept per scope, so one app's pending redraw never hides another app's
    /// wake.
    woken: Cell<bool>,
    /// Whether wakes go unreported, because the app this scope belongs to is
    /// running a frame that flushes until no effect is left.
    pub(crate) quiet: Cell<bool>,
}

impl Scope {
    /// Creates an empty scope.
    pub(crate) fn new() -> Scope {
        Scope {
            wake: RefCell::new(None),
            woken: Cell::new(false),
            quiet: Cell::new(false),
        }
    }
}

/// What an owner holds.
#[derive(Default)]
pub(crate) struct OwnerState {
    /// The owners created under this one.
    pub(crate) children: Vec<Weak<OwnerData>>,
    /// The functions registered to run at cleanup.
    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
    /// The nodes created under this owner, disposed at cleanup.
    pub(crate) nodes: Vec<NodeId>,
    /// The context values, one per type.
    pub(crate) contexts: Vec<(TypeId, Rc<dyn Any>)>,
}

/// The data of one owner: its parent, its scope and everything it holds.
pub(crate) struct OwnerData {
    /// The owner this one was created under, if any.
    pub(crate) parent: Option<Weak<OwnerData>>,
    /// The root scope this owner tree belongs to.
    pub(crate) scope: Rc<Scope>,
    /// Whether [`cleanup`](OwnerData::cleanup) already ran.
    pub(crate) cleaned: Cell<bool>,
    /// What the owner holds, taken out while cleanup runs user code.
    pub(crate) state: RefCell<OwnerState>,
    /// The timer book of the app this owner tree belongs to, set on the root
    /// owner when the app is created. A timer registers into the book of the
    /// root its owner chain leads to.
    pub(crate) timer_book: RefCell<Option<Weak<RefCell<crate::timer::TimerBook>>>>,
    /// The clipboard of the app this owner tree belongs to, set on the root
    /// owner when the app installs one. A handler reaches the clipboard of
    /// the root its owner chain leads to.
    pub(crate) clipboard: RefCell<Option<crate::clipboard::WeakSlot>>,
}

impl OwnerData {
    /// Links `child` under this owner, so cleaning this owner up cleans the
    /// child up first.
    pub(crate) fn adopt(&self, child: &Rc<OwnerData>) {
        let mut state = self.state.borrow_mut();
        // Dead links would keep every previous child's block alive, so a new
        // child reclaims the ones that already went.
        state.children.retain(|child| child.strong_count() > 0);
        state.children.push(Rc::downgrade(child));
    }

    /// Stops the owner's effects and disposes its nodes, children first.
    ///
    /// Running it twice does nothing. Cleanup functions run after the owner is
    /// marked cleaned, so a cleanup that registers another cleanup on the same
    /// owner registers it on an owner that will not run it again.
    pub(crate) fn cleanup(self: &Rc<Self>) {
        if self.cleaned.get() {
            return;
        }
        self.cleaned.set(true);
        let state = std::mem::take(&mut *self.state.borrow_mut());
        for child in &state.children {
            if let Some(child) = child.upgrade() {
                child.cleanup();
            }
        }
        for cleanup in state.cleanups {
            cleanup();
        }
        for node in state.nodes {
            dispose(node);
        }
    }

    /// Creates an owner under this one and returns it as a handle.
    ///
    /// The child inherits this owner's root scope, so its effects wake the
    /// same redraw requests.
    pub(crate) fn child(self: &Rc<Self>) -> Owner {
        let inner = Rc::new(OwnerData {
            parent: Some(Rc::downgrade(self)),
            scope: Rc::clone(&self.scope),
            cleaned: Cell::new(false),
            state: RefCell::new(OwnerState::default()),
            timer_book: RefCell::new(None),
            clipboard: RefCell::new(None),
        });
        self.adopt(&inner);
        Owner { inner }
    }
}

/// The typed value of one signal, shared by every handle that names it.
pub(crate) struct SignalData<T> {
    /// The current value.
    pub(crate) value: RefCell<T>,
    /// The node that carries the version and the subscribers.
    pub(crate) node: Cell<NodeId>,
}

/// The typed value of one memo, shared by every handle that names it.
pub(crate) struct MemoData<T> {
    /// The computed value, `None` until the first computation ran.
    pub(crate) value: RefCell<Option<T>>,
    /// The node that carries the version and the subscribers.
    pub(crate) node: Cell<NodeId>,
}

/// The body of one effect, shared between the arena node and the handle.
pub(crate) struct EffectCell {
    /// The user's body. Never borrowed while it runs, so the body can enter
    /// the runtime freely.
    pub(crate) run: RefCell<Option<Box<dyn FnMut()>>>,
}

/// One node of the arena.
pub(crate) enum Node {
    /// A value that notifies its subscribers when it changes.
    Signal {
        /// The memos and effects that read this signal.
        subscribers: Vec<NodeId>,
        /// Bumped on every write, so subscribers compare versions.
        version: u32,
    },
    /// A value computed from other nodes and recomputed when a read needs it.
    Memo {
        /// Computes the value into the typed handle and notifies on change.
        run: Option<Rc<dyn Fn()>>,
        /// The memos and effects that read this memo.
        subscribers: Vec<NodeId>,
        /// The nodes read at the last computation, with the versions seen.
        sources: Vec<(NodeId, u32)>,
        /// How far behind the value is.
        state: MemoState,
        /// Bumped when a recomputation changes the value.
        version: u32,
    },
    /// A function that reruns when a source it read changes.
    Effect {
        /// The body, shared with the handle.
        cell: Rc<EffectCell>,
        /// The nodes read at the last run, with the versions seen.
        sources: Vec<(NodeId, u32)>,
        /// Whether the effect waits in the flush queue.
        queued: bool,
        /// How many times the effect ran in the flush `generation`.
        runs: u32,
        /// The flush the run counter belongs to.
        generation: u64,
        /// The root scope a wake of this effect reports to.
        scope: Rc<Scope>,
    },
}

impl Node {
    /// The subscriber list of a node that others can subscribe to.
    fn subscribers_mut(&mut self) -> Option<&mut Vec<NodeId>> {
        match self {
            Node::Signal { subscribers, .. } | Node::Memo { subscribers, .. } => Some(subscribers),
            Node::Effect { .. } => None,
        }
    }

    /// The subscriber list of a node that others can subscribe to.
    fn subscribers(&self) -> Option<&Vec<NodeId>> {
        match self {
            Node::Signal { subscribers, .. } | Node::Memo { subscribers, .. } => Some(subscribers),
            Node::Effect { .. } => None,
        }
    }

    /// The source list of a node that reads others.
    fn sources_mut(&mut self) -> Option<&mut Vec<(NodeId, u32)>> {
        match self {
            Node::Memo { sources, .. } | Node::Effect { sources, .. } => Some(sources),
            Node::Signal { .. } => None,
        }
    }

    /// The source list of a node that reads others.
    fn sources(&self) -> Option<&Vec<(NodeId, u32)>> {
        match self {
            Node::Memo { sources, .. } | Node::Effect { sources, .. } => Some(sources),
            Node::Signal { .. } => None,
        }
    }

    /// The version of a node that others compare against.
    fn version(&self) -> Option<u32> {
        match self {
            Node::Signal { version, .. } | Node::Memo { version, .. } => Some(*version),
            Node::Effect { .. } => None,
        }
    }

    /// Removes one subscriber, keeping the list's allocation.
    fn remove_subscriber(&mut self, id: NodeId) {
        if let Some(subscribers) = self.subscribers_mut()
            && let Some(position) = subscribers.iter().position(|sub| *sub == id)
        {
            subscribers.swap_remove(position);
        }
    }
}

/// How far behind a memo's value is.
#[derive(Copy, Clone, PartialEq, Eq)]
pub(crate) enum MemoState {
    /// The value is current.
    Clean,
    /// A source changed directly, so the memo must recompute.
    Dirty,
    /// A source may have changed, so the memo must check before it reads.
    Check,
}

/// The per-thread runtime: the arena, the flush queue and the current owner.
pub(crate) struct Runtime {
    /// Every node of this thread.
    pub(crate) arena: SlotMap<NodeId, Node>,
    /// The effects woken since the last flush started draining.
    queue: VecDeque<NodeId>,
    /// Whether a flush is draining the queue.
    flushing: bool,
    /// Whether any effect ran in the current flush.
    ran: bool,
    /// The scopes woken since the last flush ended, cleared when it ends.
    woken: Vec<Rc<Scope>>,
    /// How many flushes this thread has started.
    generation: u64,
    /// The owner that `Owner::new` and the context functions see.
    current: Weak<OwnerData>,
    /// The scope of nodes and owners created with no current owner.
    default_scope: Rc<Scope>,
}

thread_local! {
    /// The runtime of this thread.
    static RUNTIME: RefCell<Runtime> = RefCell::new(Runtime::new());
    /// The effect or memo whose run tracks reads right now.
    static OBSERVER: Cell<Option<NodeId>> = const { Cell::new(None) };
}

/// Runs `f` with the thread's runtime.
pub(crate) fn with_runtime<R>(f: impl FnOnce(&mut Runtime) -> R) -> R {
    RUNTIME.with_borrow_mut(f)
}

impl Runtime {
    fn new() -> Runtime {
        Runtime {
            arena: SlotMap::with_key(),
            queue: VecDeque::new(),
            flushing: false,
            ran: false,
            woken: Vec::new(),
            generation: 0,
            current: Weak::default(),
            default_scope: Rc::new(Scope::new()),
        }
    }

    /// Returns the node, or `None` when the id was disposed.
    fn node(&self, id: NodeId) -> Option<&Node> {
        self.arena.get(id)
    }

    /// Returns the node's version, or `None` when it was disposed.
    fn version_of(&self, id: NodeId) -> Option<u32> {
        self.node(id).and_then(Node::version)
    }

    /// Returns whether the node is a memo.
    fn is_memo(&self, id: NodeId) -> bool {
        matches!(self.node(id), Some(Node::Memo { .. }))
    }

    /// Returns the current owner, when one is set and still alive.
    pub(crate) fn current_owner(&self) -> Option<Rc<OwnerData>> {
        self.current.upgrade()
    }

    /// Replaces the current owner. The previous one is restored by
    /// [`CurrentOwnerGuard`] when a `with` block ends.
    pub(crate) fn set_current(&mut self, owner: &Rc<OwnerData>) {
        self.current = Rc::downgrade(owner);
    }

    /// Returns the scope new nodes and owners belong to: the current owner's
    /// scope, or the default one when no owner is current.
    pub(crate) fn current_scope(&self) -> Rc<Scope> {
        self.current_owner().map_or_else(
            || Rc::clone(&self.default_scope),
            |owner| Rc::clone(&owner.scope),
        )
    }

    /// Returns the scope a new owner belongs to: its parent's, or a fresh
    /// root scope with its own wake callback when it starts a new tree.
    pub(crate) fn owner_scope(&self, parent: Option<&OwnerData>) -> Rc<Scope> {
        match parent {
            Some(parent) => Rc::clone(&parent.scope),
            None => Rc::new(Scope::new()),
        }
    }

    /// Restores the current owner that a `with` block replaced.
    pub(crate) fn restore_current_owner(&mut self, previous: Option<Weak<OwnerData>>) {
        self.current = previous.unwrap_or_default();
    }

    /// Registers a fresh node under the current owner, if there is one.
    fn register_node(&mut self, id: NodeId) {
        let Some(owner) = self.current_owner() else {
            return;
        };
        owner.state.borrow_mut().nodes.push(id);
    }

    /// Creates a signal node and returns its id.
    pub(crate) fn new_signal(&mut self) -> NodeId {
        let id = self.arena.insert(Node::Signal {
            subscribers: Vec::new(),
            version: 0,
        });
        self.register_node(id);
        id
    }

    /// Creates a memo node and returns its id.
    pub(crate) fn new_memo(&mut self, run: Rc<dyn Fn()>) -> NodeId {
        let id = self.arena.insert(Node::Memo {
            run: Some(run),
            subscribers: Vec::new(),
            sources: Vec::new(),
            state: MemoState::Dirty,
            version: 0,
        });
        self.register_node(id);
        id
    }

    /// Creates an effect node and returns its id.
    pub(crate) fn new_effect(&mut self, cell: Rc<EffectCell>, scope: Rc<Scope>) -> NodeId {
        let id = self.arena.insert(Node::Effect {
            cell,
            sources: Vec::new(),
            queued: false,
            runs: 0,
            generation: u64::MAX,
            scope,
        });
        self.register_node(id);
        id
    }

    /// Removes a node and unlinks its edges, returning the node. A stale id
    /// does nothing.
    ///
    /// Call it through [`dispose`], which drops the node after the runtime is
    /// released.
    fn dispose_node(&mut self, id: NodeId) -> Option<Node> {
        let count = self.node(id).and_then(Node::sources).map_or(0, Vec::len);
        for index in 0..count {
            let Some(source) = self
                .node(id)
                .and_then(Node::sources)
                .and_then(|sources| sources.get(index))
                .map(|entry| entry.0)
            else {
                break;
            };
            if let Some(node) = self.arena.get_mut(source) {
                node.remove_subscriber(id);
            }
        }
        self.arena.remove(id)
    }

    /// Bumps the version of a signal or memo and marks its subscribers.
    ///
    /// Returns where the scopes this write woke start in the woken list, for
    /// [`fire_wakes`] to run their callbacks once the runtime is released.
    pub(crate) fn notify(&mut self, id: NodeId) -> Wakes {
        let wakes = Wakes(self.woken.len());
        let count = {
            let Some(node) = self.arena.get_mut(id) else {
                return wakes;
            };
            match node {
                Node::Signal { version, .. } | Node::Memo { version, .. } => {
                    *version = version.wrapping_add(1);
                    node.subscribers().map_or(0, Vec::len)
                }
                Node::Effect { .. } => return wakes,
            }
        };
        for index in 0..count {
            let subscriber = {
                let Some(subscribers) = self.node(id).and_then(Node::subscribers) else {
                    break;
                };
                match subscribers.get(index) {
                    Some(subscriber) => *subscriber,
                    None => break,
                }
            };
            self.mark_dirty(subscriber);
        }
        wakes
    }

    /// Marks a node whose source certainly changed.
    ///
    /// A memo must recompute when next read, and its own subscribers may
    /// change, so they are marked in turn. An effect is queued.
    fn mark_dirty(&mut self, id: NodeId) {
        let propagated = match self.arena.get_mut(id) {
            Some(Node::Memo { state, .. }) => {
                let fresh = *state == MemoState::Clean;
                *state = MemoState::Dirty;
                fresh
            }
            Some(Node::Effect { .. }) => false,
            _ => return,
        };
        if propagated {
            self.mark_subscribers(id);
        } else {
            self.queue_effect(id);
        }
    }

    /// Marks a node whose source may have changed.
    ///
    /// A memo that is already behind stays as it is, because it will check its
    /// sources anyway. A clean memo is set to check and its subscribers are
    /// marked in turn. An effect is queued: it compares versions when it runs.
    fn mark_check(&mut self, id: NodeId) {
        let propagated = match self.arena.get_mut(id) {
            Some(Node::Memo { state, .. }) => {
                let fresh = *state == MemoState::Clean;
                if fresh {
                    *state = MemoState::Check;
                }
                fresh
            }
            Some(Node::Effect { .. }) => false,
            _ => return,
        };
        if propagated {
            self.mark_subscribers(id);
        } else {
            self.queue_effect(id);
        }
    }

    /// Marks every subscriber of `id` as maybe changed.
    fn mark_subscribers(&mut self, id: NodeId) {
        let count = self
            .node(id)
            .and_then(Node::subscribers)
            .map_or(0, Vec::len);
        for index in 0..count {
            let subscriber = {
                let Some(subscribers) = self.node(id).and_then(Node::subscribers) else {
                    break;
                };
                match subscribers.get(index) {
                    Some(subscriber) => *subscriber,
                    None => break,
                }
            };
            self.mark_check(subscriber);
        }
    }

    /// Queues an effect for the next flush, once, and reports the wake when
    /// the effect's scope was not woken since the last flush ended.
    ///
    /// Every effect a flush runs woke its scope when it was queued, so a write
    /// made inside a flush stays silent for the scopes that flush already
    /// serves and still wakes a scope it does not, whose app needs a frame to
    /// apply what the effect produced.
    fn queue_effect(&mut self, id: NodeId) {
        let Some(Node::Effect { queued, scope, .. }) = self.arena.get_mut(id) else {
            return;
        };
        // An effect that an unwound flush left queued has a scope whose wake
        // was cleared, so the wake is checked even when the effect is queued.
        // A quiet scope leaves its wake state alone, so the first write after
        // its frame still asks for a redraw.
        let first_wake =
            (!scope.quiet.get() && !scope.woken.replace(true)).then(|| Rc::clone(scope));
        if !*queued {
            *queued = true;
            self.queue.push_back(id);
        }
        if let Some(scope) = first_wake {
            self.woken.push(scope);
        }
    }

    /// Returns how far behind the memo is.
    fn memo_state(&self, id: NodeId) -> MemoState {
        match self.node(id) {
            Some(Node::Memo { state, .. }) => *state,
            _ => MemoState::Clean,
        }
    }

    /// Returns the reader's source count.
    fn source_count(&self, id: NodeId) -> usize {
        self.node(id).and_then(Node::sources).map_or(0, Vec::len)
    }

    /// Returns the reader's source at `index`, with the version last seen.
    fn source_at(&self, id: NodeId, index: usize) -> Option<(NodeId, u32)> {
        let sources = self.node(id).and_then(Node::sources)?;
        sources.get(index).copied()
    }

    /// Marks the memo clean without recomputing, because no source changed.
    fn memo_set_clean(&mut self, id: NodeId) {
        if let Some(Node::Memo { state, .. }) = self.arena.get_mut(id) {
            *state = MemoState::Clean;
        }
    }

    /// Returns the memo's computation, if the memo is still live.
    fn memo_run(&self, id: NodeId) -> Option<Rc<dyn Fn()>> {
        match self.node(id) {
            Some(Node::Memo { run, .. }) => run.clone(),
            _ => None,
        }
    }

    /// Prepares a memo for recomputation: it unsubscribes from its old
    /// sources and counts as clean while it runs.
    fn begin_memo(&mut self, id: NodeId) {
        if let Some(Node::Memo { state, .. }) = self.arena.get_mut(id) {
            *state = MemoState::Clean;
        }
        self.clear_sources(id);
    }

    /// Counts one effect run against this flush's limit. Returns whether the
    /// effect may run.
    ///
    /// The effect stays marked as queued while it pulls its memos up to date,
    /// so a memo that changes during the pull does not queue it a second
    /// time. [`Runtime::release_effect`] clears the mark before the body runs.
    fn begin_effect(&mut self, id: NodeId) -> bool {
        let runs = {
            let Some(Node::Effect {
                queued,
                runs,
                generation,
                ..
            }) = self.arena.get_mut(id)
            else {
                return false;
            };
            if !*queued {
                return false;
            }
            if *generation != self.generation {
                *generation = self.generation;
                *runs = 1;
            } else {
                *runs += 1;
            }
            *runs
        };
        if runs > MAX_RUNS_PER_FLUSH {
            self.release_effect(id);
            debug_assert!(
                false,
                "a binding ran {runs} times in one flush, so it is part of a \
                 cycle: it writes a signal that wakes it again, directly or \
                 through another binding"
            );
            return false;
        }
        true
    }

    /// Clears the queued mark of an effect taken off the queue, so a later
    /// write can queue it again.
    fn release_effect(&mut self, id: NodeId) {
        if let Some(Node::Effect { queued, .. }) = self.arena.get_mut(id) {
            *queued = false;
        }
    }

    /// Returns the effect's body, if the effect is still live.
    fn effect_cell(&self, id: NodeId) -> Option<Rc<EffectCell>> {
        match self.node(id) {
            Some(Node::Effect { cell, .. }) => Some(Rc::clone(cell)),
            _ => None,
        }
    }

    /// Unsubscribes the reader from its old sources, keeping the list's
    /// allocation, so the run ahead records the sources it reaches.
    fn clear_sources(&mut self, id: NodeId) {
        let count = self.source_count(id);
        for index in 0..count {
            let Some((source, _)) = self.source_at(id, index) else {
                break;
            };
            if let Some(node) = self.arena.get_mut(source) {
                node.remove_subscriber(id);
            }
        }
        if let Some(node) = self.arena.get_mut(id)
            && let Some(sources) = node.sources_mut()
        {
            sources.clear();
        }
    }

    /// Records that `reader` read `source` at `version`, linking the two nodes
    /// when they are not linked yet.
    pub(crate) fn track(&mut self, reader: NodeId, source: NodeId, version: u32) {
        let already = match self.node(reader) {
            Some(node) => node
                .sources()
                .is_some_and(|sources| sources.iter().any(|(seen, _)| *seen == source)),
            None => return,
        };
        if already {
            return;
        }
        if let Some(node) = self.arena.get_mut(source)
            && let Some(subscribers) = node.subscribers_mut()
        {
            subscribers.push(reader);
        }
        if let Some(node) = self.arena.get_mut(reader)
            && let Some(sources) = node.sources_mut()
        {
            sources.push((source, version));
        }
    }

    /// Returns whether the reader's source at `index` moved since it was read.
    ///
    /// A disposed source never counts as moved.
    fn source_changed(&self, id: NodeId, index: usize) -> bool {
        self.source_at(id, index).is_some_and(|(source, seen)| {
            self.version_of(source)
                .is_some_and(|version| version != seen)
        })
    }

    /// Pops the next effect to run, if the queue holds one.
    fn pop_effect(&mut self) -> Option<NodeId> {
        self.queue.pop_front()
    }

    /// Reports that an effect was processed by the current flush.
    fn set_ran(&mut self) {
        self.ran = true;
    }

    /// Starts a flush, unless one is already running.
    fn begin_flush(&mut self) -> bool {
        if self.flushing {
            return false;
        }
        self.flushing = true;
        self.ran = false;
        self.generation += 1;
        true
    }

    /// Returns whether any effect ran in the current flush.
    fn flush_ran(&self) -> bool {
        self.ran
    }

    /// Ends a flush, or cleans up after one that unwound.
    ///
    /// The wake state is cleared, so the next write between frames requests a
    /// redraw. A flush that unwound leaves the effects it did not reach in the
    /// queue, so they still run, in the next flush.
    fn end_flush(&mut self) {
        self.flushing = false;
        for scope in self.woken.drain(..) {
            scope.woken.set(false);
        }
    }
}

/// Guards the observer while a memo or an effect runs, restoring the previous
/// one even when the body unwinds.
struct ObserverGuard {
    previous: Option<NodeId>,
}

impl ObserverGuard {
    /// Makes `observer` the node that reads are tracked into.
    fn enter(observer: Option<NodeId>) -> ObserverGuard {
        let previous = OBSERVER.with(|cell| cell.replace(observer));
        ObserverGuard { previous }
    }
}

impl Drop for ObserverGuard {
    fn drop(&mut self) {
        OBSERVER.with(|cell| cell.replace(self.previous));
    }
}

/// Guards the current owner while an `Owner::with` block runs.
pub(crate) struct CurrentOwnerGuard {
    previous: Option<Weak<OwnerData>>,
}

impl CurrentOwnerGuard {
    /// Makes `owner` the current one for the block the guard lives in.
    pub(crate) fn enter(owner: &Rc<OwnerData>) -> CurrentOwnerGuard {
        let previous = with_runtime(|runtime| {
            let previous = runtime
                .current_owner()
                .map(|current| Rc::downgrade(&current));
            runtime.current = Rc::downgrade(owner);
            previous
        });
        CurrentOwnerGuard { previous }
    }
}

impl Drop for CurrentOwnerGuard {
    fn drop(&mut self) {
        with_runtime(|runtime| runtime.restore_current_owner(self.previous.take()));
    }
}

/// Ends a flush when the call returns or unwinds.
struct FlushGuard;

impl Drop for FlushGuard {
    fn drop(&mut self) {
        with_runtime(|runtime| runtime.end_flush());
    }
}

/// Disposes a fresh node when creation unwinds, so an effect whose first run
/// panics with no owner around leaves nothing behind.
pub(crate) struct DisposeOnUnwind {
    /// The node to dispose.
    pub(crate) node: NodeId,
    /// Whether the node still needs disposing.
    pub(crate) armed: bool,
}

impl Drop for DisposeOnUnwind {
    fn drop(&mut self) {
        if self.armed {
            dispose(self.node);
        }
    }
}

/// Removes a node and unlinks its edges. A stale id does nothing.
///
/// The node is dropped after the runtime is released, because dropping a memo
/// or an effect drops the user's closure, and what it captured may enter the
/// runtime as it drops, as an effect handle or an owner does.
pub(crate) fn dispose(id: NodeId) {
    let node = with_runtime(|runtime| runtime.dispose_node(id));
    drop(node);
}

/// Records a tracked read of `source` into the running observer.
///
/// The read is silently untracked when no effect or memo runs, or when the
/// source was disposed.
pub(crate) fn track_read(source: NodeId) {
    let Some(reader) = OBSERVER.with(Cell::get) else {
        return;
    };
    with_runtime(|runtime| {
        if let Some(version) = runtime.version_of(source) {
            runtime.track(reader, source, version);
        }
    });
}

/// Runs the flush loop and returns whether any effect ran.
///
/// Effects queued by writes made while the loop drains run in the same call.
/// A call from inside a running flush does nothing and returns `false`.
pub(crate) fn flush() -> bool {
    if !with_runtime(|runtime| runtime.begin_flush()) {
        return false;
    }
    let _guard = FlushGuard;
    while let Some(id) = with_runtime(|runtime| runtime.pop_effect()) {
        run_effect(id);
    }
    with_runtime(|runtime| runtime.flush_ran())
}

/// Runs one queued effect: it verifies its sources, pulling memos up to date,
/// and skips its body when nothing changed.
///
/// The sources are checked in the order the last run read them, and the
/// check stops at the first one that moved. The body then runs and reads
/// what it needs, so a memo that only a branch no longer taken reads is not
/// computed.
fn run_effect(id: NodeId) {
    if !with_runtime(|runtime| runtime.begin_effect(id)) {
        return;
    }
    let changed = sources_changed(id);
    with_runtime(|runtime| runtime.release_effect(id));
    if !changed {
        return;
    }
    let Some(cell) = with_runtime(|runtime| runtime.effect_cell(id)) else {
        return;
    };
    with_runtime(|runtime| {
        runtime.clear_sources(id);
        runtime.set_ran();
    });
    run_body(&cell, id);
}

/// Runs an effect body under tracking, as a flush step or the first run at
/// creation does.
pub(crate) fn run_body(cell: &Rc<EffectCell>, node: NodeId) {
    let _observer = ObserverGuard::enter(Some(node));
    let mut borrow = cell
        .run
        .try_borrow_mut()
        .expect("an effect is never called while it is running");
    if let Some(run) = &mut *borrow {
        run();
    }
}

/// Pulls the reader's sources up to date in the order it read them, and
/// returns whether one of them moved, stopping at the first that did.
fn sources_changed(id: NodeId) -> bool {
    let count = with_runtime(|runtime| runtime.source_count(id));
    for index in 0..count {
        let Some((source, _)) = with_runtime(|runtime| runtime.source_at(id, index)) else {
            break;
        };
        update_source(source);
        if with_runtime(|runtime| runtime.source_changed(id, index)) {
            return true;
        }
    }
    false
}

/// Brings a source up to date before its version is compared.
fn update_source(source: NodeId) {
    if with_runtime(|runtime| runtime.is_memo(source)) {
        update_memo(source);
    }
}

/// Recomputes a memo when its value is behind its sources.
///
/// A dirty memo recomputes at once. A memo whose source may have changed
/// brings its sources up to date in the order it read them, and recomputes
/// as soon as one of them moved, without computing the ones after it.
pub(crate) fn update_memo(id: NodeId) {
    match with_runtime(|runtime| runtime.memo_state(id)) {
        MemoState::Clean => {}
        MemoState::Dirty => recompute_memo(id),
        MemoState::Check => {
            if sources_changed(id) {
                recompute_memo(id);
            } else {
                with_runtime(|runtime| runtime.memo_set_clean(id));
            }
        }
    }
}

/// Recomputes a memo: it unsubscribes from its old sources and runs its
/// computation, which compares and stores the value and notifies on change.
fn recompute_memo(id: NodeId) {
    let Some(run) = with_runtime(|runtime| runtime.memo_run(id)) else {
        return;
    };
    with_runtime(|runtime| runtime.begin_memo(id));
    let _observer = ObserverGuard::enter(Some(id));
    run();
}

/// Runs the first computation of a fresh memo under the current observer.
pub(crate) fn compute_new_memo(id: NodeId) {
    recompute_memo(id);
}

/// Where the scopes one write woke start in the runtime's woken list.
#[must_use = "the wakes must be fired once the runtime is released"]
pub(crate) struct Wakes(usize);

/// Runs the wake callbacks of the scopes a write woke, outside any runtime
/// borrow.
///
/// A callback may write signals and wake more scopes, which land after the
/// ones this call fires and are fired by the write that woke them.
pub(crate) fn fire_wakes(wakes: Wakes) {
    let end = with_runtime(|runtime| runtime.woken.len());
    for index in wakes.0..end {
        let callback = with_runtime(|runtime| {
            runtime
                .woken
                .get(index)
                .and_then(|scope| scope.wake.borrow().clone())
        });
        if let Some(callback) = callback {
            callback();
        }
    }
}