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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
#[cfg(debug_assertions)]
use crate::SpecialNonReactiveZone;
use crate::{
    hydration::SharedContext,
    node::{
        Disposer, NodeId, ReactiveNode, ReactiveNodeState, ReactiveNodeType,
    },
    AnyComputation, AnyResource, EffectState, Memo, MemoState, ReadSignal,
    ResourceId, ResourceState, RwSignal, SerializableResource, StoredValueId,
    Trigger, UnserializableResource, WriteSignal,
};
use cfg_if::cfg_if;
use core::hash::BuildHasherDefault;
use futures::stream::FuturesUnordered;
use indexmap::IndexSet;
use pin_project::pin_project;
use rustc_hash::{FxHashMap, FxHasher};
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
use std::{
    any::{Any, TypeId},
    cell::{Cell, RefCell},
    fmt::Debug,
    future::Future,
    marker::PhantomData,
    pin::Pin,
    rc::Rc,
    task::Poll,
};
use thiserror::Error;

pub(crate) type PinnedFuture<T> = Pin<Box<dyn Future<Output = T>>>;

cfg_if! {
    if #[cfg(any(feature = "csr", feature = "hydrate"))] {
        thread_local! {
            pub(crate) static RUNTIME: Runtime = Runtime::new();
        }
    } else {
        thread_local! {
            pub(crate) static RUNTIMES: RefCell<SlotMap<RuntimeId, Runtime>> = Default::default();

            pub(crate) static CURRENT_RUNTIME: Cell<Option<RuntimeId>> = Default::default();
        }
    }
}

// Stores the reactive runtime associated with the current Tokio task
#[cfg(feature = "ssr")]
tokio::task_local! {
    pub(crate) static TASK_RUNTIME: Option<RuntimeId>;
}

type FxIndexSet<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;

// The data structure that owns all the signals, memos, effects,
// and other data included in the reactive system.
#[derive(Default)]
pub(crate) struct Runtime {
    pub shared_context: RefCell<SharedContext>,
    pub owner: Cell<Option<NodeId>>,
    pub observer: Cell<Option<NodeId>>,
    #[allow(clippy::type_complexity)]
    pub on_cleanups:
        RefCell<SparseSecondaryMap<NodeId, Vec<Box<dyn FnOnce()>>>>,
    pub stored_values: RefCell<SlotMap<StoredValueId, Rc<RefCell<dyn Any>>>>,
    pub nodes: RefCell<SlotMap<NodeId, ReactiveNode>>,
    pub node_subscribers:
        RefCell<SecondaryMap<NodeId, RefCell<FxIndexSet<NodeId>>>>,
    pub node_sources:
        RefCell<SecondaryMap<NodeId, RefCell<FxIndexSet<NodeId>>>>,
    pub node_owners: RefCell<SecondaryMap<NodeId, NodeId>>,
    pub node_properties:
        RefCell<SparseSecondaryMap<NodeId, Vec<ScopeProperty>>>,
    #[allow(clippy::type_complexity)]
    pub contexts:
        RefCell<SparseSecondaryMap<NodeId, FxHashMap<TypeId, Box<dyn Any>>>>,
    pub pending_effects: RefCell<Vec<NodeId>>,
    pub resources: RefCell<SlotMap<ResourceId, AnyResource>>,
    pub batching: Cell<bool>,
}

/// The current reactive runtime.
pub fn current_runtime() -> RuntimeId {
    Runtime::current()
}

/// Sets the current reactive runtime.
#[inline(always)]
#[allow(unused_variables)]
pub fn set_current_runtime(runtime: RuntimeId) {
    #[cfg(not(any(feature = "csr", feature = "hydrate")))]
    Runtime::set_runtime(Some(runtime));
}

/// A reactive owner.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Owner(pub(crate) NodeId);

impl Owner {
    /// Returns the current reactive owner.
    pub fn current() -> Option<Owner> {
        with_runtime(|runtime| runtime.owner.get())
            .ok()
            .flatten()
            .map(Owner)
    }

    /// Returns a unique handle for this owner for FFI purposes.
    pub fn as_ffi(&self) -> u64 {
        use slotmap::Key;

        self.0.data().as_ffi()
    }

    /// Parses a unique handler back into an owner.
    ///
    /// Iff `value` is value received from `k.as_ffi()`, returns a key equal to `k`.
    /// Otherwise the behavior is safe but unspecified.
    pub fn from_ffi(ffi: u64) -> Self {
        use slotmap::KeyData;

        Self(NodeId::from(KeyData::from_ffi(ffi)))
    }
}

// This core Runtime impl block handles all the work of marking and updating
// the reactive graph.
//
// In terms of concept and algorithm, this reactive-system implementation
// is significantly inspired by Reactively (https://github.com/modderme123/reactively)
impl Runtime {
    #[inline(always)]
    pub fn current() -> RuntimeId {
        cfg_if! {
            if #[cfg(any(feature = "csr", feature = "hydrate"))] {
                Default::default()
            } else if #[cfg(feature = "ssr")] {
                // either use the runtime associated with the current task,
                // or the current runtime
                TASK_RUNTIME.try_with(|trt| *trt)
                    .ok()
                    .flatten()
                    .unwrap_or_else(|| CURRENT_RUNTIME.with(|id| id.get()).unwrap_or_default())
            } else {
                CURRENT_RUNTIME.with(|id| id.get()).unwrap_or_default()
            }
        }
    }

    #[cfg(not(any(feature = "csr", feature = "hydrate")))]
    #[inline(always)]
    pub(crate) fn set_runtime(id: Option<RuntimeId>) {
        CURRENT_RUNTIME.with(|curr| curr.set(id))
    }

    pub(crate) fn update_if_necessary(&self, node_id: NodeId) {
        if self.current_state(node_id) == ReactiveNodeState::Check {
            let sources = {
                let sources = self.node_sources.borrow();

                // rather than cloning the entire FxIndexSet, only allocate a `Vec` for the node ids
                sources.get(node_id).map(|n| {
                    let sources = n.borrow();
                    // in case Vec::from_iterator specialization doesn't work, do it manually
                    let mut sources_vec = Vec::with_capacity(sources.len());
                    sources_vec.extend(sources.iter().cloned());
                    sources_vec
                })
            };

            for source in sources.into_iter().flatten() {
                self.update_if_necessary(source);
                if self.current_state(node_id) >= ReactiveNodeState::Dirty {
                    // as soon as a single parent has marked us dirty, we can
                    // stop checking them to avoid over-re-running
                    break;
                }
            }
        }

        // if we're dirty at this point, update
        if self.current_state(node_id) >= ReactiveNodeState::Dirty {
            self.cleanup_node(node_id);

            // now, update the value
            self.update(node_id);
        }

        // now we're clean
        self.mark_clean(node_id);
    }

    pub(crate) fn update(&self, node_id: NodeId) {
        let node = {
            let nodes = self.nodes.borrow();
            nodes.get(node_id).cloned()
        };

        if let Some(node) = node {
            // memos and effects rerun
            // signals simply have their value
            let changed = match node.node_type {
                ReactiveNodeType::Signal | ReactiveNodeType::Trigger => true,
                ReactiveNodeType::Memo { ref f }
                | ReactiveNodeType::Effect { ref f } => {
                    let value = node.value();
                    // set this node as the observer
                    self.with_observer(node_id, move || {
                        // clean up sources of this memo/effect
                        self.cleanup_sources(node_id);

                        f.run(value)
                    })
                }
            };

            // mark children dirty
            if changed {
                let subs = self.node_subscribers.borrow();

                if let Some(subs) = subs.get(node_id) {
                    let mut nodes = self.nodes.borrow_mut();
                    for sub_id in subs.borrow().iter() {
                        if let Some(sub) = nodes.get_mut(*sub_id) {
                            sub.state = ReactiveNodeState::Dirty;
                        }
                    }
                }
            }

            // mark clean
            self.mark_clean(node_id);
        }
    }

    pub(crate) fn dispose_node(&self, node_id: NodeId) {
        self.cleanup_node(node_id);

        // each of the subs needs to remove the node from its dependencies
        // so that it doesn't try to read the (now disposed) signal
        let subs = self.node_subscribers.borrow_mut().remove(node_id);
        if let Some(subs) = subs {
            let source_map = self.node_sources.borrow();
            for effect in subs.borrow().iter() {
                if let Some(effect_sources) = source_map.get(*effect) {
                    effect_sources.borrow_mut().swap_remove(&node_id);
                }
            }
        }

        self.node_sources.borrow_mut().remove(node_id);
        let node = { self.nodes.borrow_mut().remove(node_id) };
        drop(node);
    }
    fn cleanup_node(&self, node_id: NodeId) {
        self.run_on_cleanups(node_id);
        self.dispose_children(node_id);
    }
    /// Dispose of all of the children of the node recursively and completely.
    fn dispose_children(&self, node_id: NodeId) {
        let properties = { self.node_properties.borrow_mut().remove(node_id) };
        if let Some(properties) = properties {
            for property in properties {
                self.dispose_property(property);
            }
        }
    }
    fn dispose_property(&self, property: ScopeProperty) {
        // for signals, triggers, memos, effects, shared node cleanup
        match property {
            ScopeProperty::Signal(node)
            | ScopeProperty::Trigger(node)
            | ScopeProperty::Effect(node) => {
                self.dispose_node(node);
            }
            ScopeProperty::Resource(id) => {
                let value = self.resources.borrow_mut().remove(id);
                drop(value);
            }
            ScopeProperty::StoredValue(id) => {
                let value = self.stored_values.borrow_mut().remove(id);
                drop(value);
            }
        }
    }
    fn run_on_cleanups(&self, node_id: NodeId) {
        let c = { self.on_cleanups.borrow_mut().remove(node_id) };
        let prev_observer = self.observer.take(); // untrack around all cleanups
        if let Some(cleanups) = c {
            for cleanup in cleanups {
                cleanup();
            }
        }
        self.observer.set(prev_observer);
    }

    pub(crate) fn cleanup_sources(&self, node_id: NodeId) {
        let sources = self.node_sources.borrow();
        if let Some(sources) = sources.get(node_id) {
            let subs = self.node_subscribers.borrow();
            for source in sources.borrow().iter() {
                if let Some(source) = subs.get(*source) {
                    // Using `.shift_remove()` here guarantees that dependencies
                    // of a signal are always triggered in the same order.
                    // This is important for cases in which, for example, the first effect
                    // conditionally checks that the signal value is `Some(_)`, and the
                    // second one unwraps its value; if they maintain this order, then the check
                    // will always run first, and will cancel the unwrap if it is None. But if the
                    // order can be inverted (by using .swap_remove() here), the unwrap will
                    // run first on a subsequent run.
                    //
                    // Maintaining execution order is the intention of using an IndexSet here anyway,
                    // but using .swap_remove() would undermine that goal.
                    source.borrow_mut().shift_remove(&node_id);
                }
            }
        }
    }

    fn current_state(&self, node: NodeId) -> ReactiveNodeState {
        match self.nodes.borrow().get(node) {
            None => ReactiveNodeState::Clean,
            Some(node) => node.state,
        }
    }

    fn with_observer<T>(&self, observer: NodeId, f: impl FnOnce() -> T) -> T {
        // take previous observer and owner
        let prev_observer = self.observer.take();
        let prev_owner = self.owner.take();

        self.owner.set(Some(observer));
        self.observer.set(Some(observer));
        let v = f();
        self.observer.set(prev_observer);
        self.owner.set(prev_owner);
        v
    }

    fn mark_clean(&self, node: NodeId) {
        let mut nodes = self.nodes.borrow_mut();
        if let Some(node) = nodes.get_mut(node) {
            node.state = ReactiveNodeState::Clean;
        }
    }

    pub(crate) fn mark_dirty(&self, node: NodeId) {
        let mut nodes = self.nodes.borrow_mut();

        if let Some(current_node) = nodes.get_mut(node) {
            if current_node.state == ReactiveNodeState::DirtyMarked {
                return;
            }

            let mut pending_effects = self.pending_effects.borrow_mut();
            let subscribers = self.node_subscribers.borrow();
            let current_observer = self.observer.get();

            // mark self dirty
            Runtime::mark(
                node,
                current_node,
                ReactiveNodeState::Dirty,
                &mut pending_effects,
                current_observer,
            );

            /*
             * Depth-first DAG traversal that uses a stack of iterators instead of
             * buffering the entire to-visit list. Visited nodes are either marked as
             * `Check` or `DirtyMarked`.
             *
             * Because `RefCell`, borrowing the iterators all at once is difficult,
             * so a self-referential struct is used instead. self_cell produces safe
             * code, but it would not be recommended to use this outside of this
             * algorithm.
             */

            type Dependent<'a> = indexmap::set::Iter<'a, NodeId>;

            self_cell::self_cell! {
                struct RefIter<'a> {
                    owner: std::cell::Ref<'a, FxIndexSet<NodeId>>,

                    #[not_covariant] // avoids extra codegen, harmless to mark it as such
                    dependent: Dependent,
                }
            }

            /// Due to the limitations of self-referencing, we cannot borrow the
            /// stack and iter simultaneously within the closure or the loop,
            /// therefore this must be used to command the outside scope
            /// of what to do.
            enum IterResult<'a> {
                Continue,
                Empty,
                NewIter(RefIter<'a>),
            }

            let mut stack = Vec::new();

            if let Some(children) = subscribers.get(node) {
                stack.push(RefIter::new(children.borrow(), |children| {
                    children.iter()
                }));
            }

            while let Some(iter) = stack.last_mut() {
                let res = iter.with_dependent_mut(|_, iter| {
                    let Some(mut child) = iter.next().copied() else {
                        return IterResult::Empty;
                    };

                    while let Some(node) = nodes.get_mut(child) {
                        if node.state == ReactiveNodeState::Check
                            || node.state == ReactiveNodeState::DirtyMarked
                        {
                            return IterResult::Continue;
                        }

                        Runtime::mark(
                            child,
                            node,
                            ReactiveNodeState::Check,
                            &mut pending_effects,
                            current_observer,
                        );

                        if let Some(children) = subscribers.get(child) {
                            let children = children.borrow();

                            if !children.is_empty() {
                                // avoid going through an iterator in the simple pseudo-recursive case
                                if children.len() == 1 {
                                    child = children[0];
                                    continue;
                                }

                                return IterResult::NewIter(RefIter::new(
                                    children,
                                    |children| children.iter(),
                                ));
                            }
                        }

                        break;
                    }

                    IterResult::Continue
                });

                match res {
                    IterResult::Continue => continue,
                    IterResult::NewIter(iter) => stack.push(iter),
                    IterResult::Empty => {
                        stack.pop();
                    }
                }
            }
        }
    }

    #[inline(always)] // small function, used in hot loop
    fn mark(
        //nodes: &mut SlotMap<NodeId, ReactiveNode>,
        node_id: NodeId,
        node: &mut ReactiveNode,
        level: ReactiveNodeState,
        pending_effects: &mut Vec<NodeId>,
        current_observer: Option<NodeId>,
    ) {
        if level > node.state {
            node.state = level;
        }

        if matches!(node.node_type, ReactiveNodeType::Effect { .. } if current_observer != Some(node_id))
        {
            pending_effects.push(node_id)
        }

        if node.state == ReactiveNodeState::Dirty {
            node.state = ReactiveNodeState::DirtyMarked;
        }
    }

    pub(crate) fn run_effects(&self) {
        if !self.batching.get() {
            let effects = self.pending_effects.take();
            for effect_id in effects {
                self.update_if_necessary(effect_id);
            }
        }
    }

    #[track_caller]
    pub(crate) fn register_property(
        &self,
        property: ScopeProperty,
        #[cfg(debug_assertions)] defined_at: &'static std::panic::Location<
            'static,
        >,
    ) {
        let mut properties = self.node_properties.borrow_mut();
        if let Some(owner) = self.owner.get() {
            if let Some(entry) = properties.entry(owner) {
                let entry = entry.or_default();
                entry.push(property);
            }

            if let Some(node) = property.to_node_id() {
                let mut owners = self.node_owners.borrow_mut();
                owners.insert(node, owner);
            }
        } else {
            crate::macros::debug_warn!(
                "At {defined_at}, you are creating a reactive value outside \
                 the reactive root.",
            );
        }
    }

    pub(crate) fn get_context<T: Clone + 'static>(
        &self,
        node: NodeId,
        ty: TypeId,
    ) -> Option<T> {
        let contexts = self.contexts.borrow();

        let context = contexts.get(node);
        let local_value = context.and_then(|context| {
            context
                .get(&ty)
                .and_then(|val| val.downcast_ref::<T>())
                .cloned()
        });
        match local_value {
            Some(val) => Some(val),
            None => {
                #[cfg(all(
                    feature = "hydrate",
                    feature = "experimental-islands"
                ))]
                {
                    self.get_island_context(
                        self.shared_context
                            .borrow()
                            .islands
                            .get(&Owner(node))
                            .cloned(),
                        node,
                        ty,
                    )
                }
                #[cfg(not(all(
                    feature = "hydrate",
                    feature = "experimental-islands"
                )))]
                {
                    self.node_owners
                        .borrow()
                        .get(node)
                        .and_then(|parent| self.get_context(*parent, ty))
                }
            }
        }
    }

    #[cfg(all(feature = "hydrate", feature = "experimental-islands"))]
    pub(crate) fn get_island_context<T: Clone + 'static>(
        &self,
        el: Option<web_sys::HtmlElement>,
        node: NodeId,
        ty: TypeId,
    ) -> Option<T> {
        let contexts = self.contexts.borrow();

        let context = contexts.get(node);
        let local_value = context.and_then(|context| {
            context
                .get(&ty)
                .and_then(|val| val.downcast_ref::<T>())
                .cloned()
        });

        match (el, local_value) {
            (_, Some(val)) => Some(val),
            // if we're already in the island's scope, island-hop
            (Some(el), None) => {
                use js_sys::Reflect;
                use wasm_bindgen::{intern, JsCast, JsValue};
                let parent_el = el
                    .parent_element()
                    .expect("to have parent")
                    .unchecked_ref::<web_sys::HtmlElement>()
                    .closest("leptos-children")
                    .expect("to find island")
                    //.flatten()
                    .and_then(|el| el.dyn_into::<web_sys::HtmlElement>().ok());
                match parent_el
                    .clone()
                    .and_then(|el| {
                        Reflect::get(&el, &JsValue::from_str(intern("$$owner")))
                            .ok()
                    })
                    .and_then(|value| u64::try_from(value).ok())
                    .map(Owner::from_ffi)
                {
                    Some(owner) => {
                        self.get_island_context(parent_el, owner.0, ty)
                    }
                    None => None,
                }
            }
            // otherwise, check for a parent scope
            (None, None) => {
                self.node_owners.borrow().get(node).and_then(|parent| {
                    self.get_island_context(
                        self.shared_context
                            .borrow()
                            .islands
                            .get(&Owner(*parent))
                            .cloned(),
                        *parent,
                        ty,
                    )
                })
            }
        }
    }

    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", skip_all,)
    )]
    #[track_caller]
    pub(crate) fn push_scope_property(&self, prop: ScopeProperty) {
        #[cfg(debug_assertions)]
        let defined_at = std::panic::Location::caller();
        self.register_property(
            prop,
            #[cfg(debug_assertions)]
            defined_at,
        );
    }

    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", skip_all,)
    )]
    #[track_caller]
    pub(crate) fn remove_scope_property(
        &self,
        owner: NodeId,
        property: ScopeProperty,
    ) {
        let mut properties = self.node_properties.borrow_mut();
        if let Some(properties) = properties.get_mut(owner) {
            // remove this property from the list, if found
            if let Some(index) = properties.iter().position(|p| p == &property)
            {
                // order of properties doesn't matter so swap_remove
                // is the most efficient way to remove
                properties.swap_remove(index);
            }
        }

        if let Some(node) = property.to_node_id() {
            let mut owners = self.node_owners.borrow_mut();
            owners.remove(node);
        }
    }
}

impl Debug for Runtime {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Runtime").finish()
    }
}

/// Get the selected runtime from the thread-local set of runtimes. On the server,
/// this will return the correct runtime. In the browser, there should only be one runtime.
#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
#[inline(always)] // it monomorphizes anyway
pub(crate) fn with_runtime<T>(
    f: impl FnOnce(&Runtime) -> T,
) -> Result<T, ReactiveSystemError> {
    // in the browser, everything should exist under one runtime
    cfg_if! {
        if #[cfg(any(feature = "csr", feature = "hydrate"))] {
            Ok(RUNTIME.with(|runtime| f(runtime)))
        } else {
            RUNTIMES.with(|runtimes| {
                let runtimes = runtimes.borrow();
                let rt = Runtime::current();
                match runtimes.get(rt) {
                    None => Err(ReactiveSystemError::RuntimeDisposed(rt)),
                    Some(runtime) => Ok(f(runtime))
                }
            })
        }
    }
}

#[must_use = "Runtime will leak memory if Runtime::dispose() is never called."]
/// Creates a new reactive runtime and sets it as the current runtime.
///
/// This should almost always be handled by the framework, not called directly in user code.
pub fn create_runtime() -> RuntimeId {
    cfg_if! {
        if #[cfg(any(feature = "csr", feature = "hydrate"))] {
            Default::default()
        } else {
            let id = RUNTIMES.with(|runtimes| runtimes.borrow_mut().insert(Runtime::new()));
            Runtime::set_runtime(Some(id));

            id
        }
    }
}

#[cfg(not(any(feature = "csr", feature = "hydrate")))]
slotmap::new_key_type! {
    /// Unique ID assigned to a Runtime.
    pub struct RuntimeId;
}

/// Unique ID assigned to a Runtime.
#[cfg(any(feature = "csr", feature = "hydrate"))]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RuntimeId;

/// Wraps the given function so that, whenever it is called, it creates
/// a child node owned by whichever reactive node was the owner
/// when it was created, runs the function, and returns a disposer that
/// can be used to dispose of the child later.
///
/// This can be used to hoist children created inside an effect up to
/// the level of a higher parent, to prevent each one from being disposed
/// every time the effect within which they're created is run.
///
/// For example, each row in a `<For/>` component could be created using this,
/// so that they are owned by the `<For/>` component itself, not an effect
/// running within it.
///
/// ## Panics
/// Panics if there is no current reactive runtime.
pub fn as_child_of_current_owner<T, U>(
    f: impl Fn(T) -> U,
) -> impl Fn(T) -> (U, Disposer)
where
    T: 'static,
{
    let owner = with_runtime(|runtime| runtime.owner.get())
        .expect("runtime should be alive when created");
    move |t| {
        with_runtime(|runtime| {
            let prev_observer = runtime.observer.take();
            let prev_owner = runtime.owner.take();

            runtime.owner.set(owner);
            runtime.observer.set(owner);

            let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
                value: None,
                state: ReactiveNodeState::Clean,
                node_type: ReactiveNodeType::Trigger,
            });
            runtime.push_scope_property(ScopeProperty::Trigger(id));
            let disposer = Disposer(id);

            runtime.owner.set(Some(id));
            runtime.observer.set(Some(id));

            let v = f(t);

            runtime.observer.set(prev_observer);
            runtime.owner.set(prev_owner);

            (v, disposer)
        })
        .expect("runtime should be alive when run")
    }
}

/// Wraps the given function so that, whenever it is called, it is run
/// in the reactive scope of whatever the reactive owner was when it was
/// created.
///
/// ## Panics
/// Panics if there is no current reactive runtime.
pub fn with_current_owner<T, U>(f: impl Fn(T) -> U + 'static) -> impl Fn(T) -> U
where
    T: 'static,
{
    let owner = with_runtime(|runtime| runtime.owner.get())
        .expect("runtime should be alive when created");
    move |t| {
        with_runtime(|runtime| {
            let prev_observer = runtime.observer.take();
            let prev_owner = runtime.owner.take();

            runtime.owner.set(owner);
            runtime.observer.set(owner);

            let v = f(t);

            runtime.observer.set(prev_observer);
            runtime.owner.set(prev_owner);

            v
        })
        .expect("runtime should be alive when run")
    }
}

/// Runs the given code with the given reactive owner.
///
/// ## Panics
/// Panics if there is no current reactive runtime.
pub fn with_owner<T>(owner: Owner, f: impl FnOnce() -> T) -> T {
    try_with_owner(owner, f).unwrap()
}

#[derive(Error, Debug)]
pub enum ReactiveSystemError {
    #[error("Runtime {0:?} has been disposed.")]
    RuntimeDisposed(RuntimeId),
    #[error("Owner {0:?} has been disposed.")]
    OwnerDisposed(Owner),
    #[error("Error borrowing runtime.nodes {0:?}")]
    Borrow(std::cell::BorrowError),
}

/// Runs the given code with the given reactive owner.
pub fn try_with_owner<T>(
    owner: Owner,
    f: impl FnOnce() -> T,
) -> Result<T, ReactiveSystemError> {
    with_runtime(|runtime| {
        let scope_exists = {
            let nodes = runtime
                .nodes
                .try_borrow()
                .map_err(ReactiveSystemError::Borrow)?;
            nodes.contains_key(owner.0)
        };
        if scope_exists {
            let prev_observer = runtime.observer.take();
            let prev_owner = runtime.owner.take();

            runtime.owner.set(Some(owner.0));
            runtime.observer.set(Some(owner.0));

            let v = f();

            runtime.observer.set(prev_observer);
            runtime.owner.set(prev_owner);

            Ok(v)
        } else {
            Err(ReactiveSystemError::OwnerDisposed(owner))
        }
    })?
}

/// Runs the given function as a child of the current Owner, once.
pub fn run_as_child<T>(f: impl FnOnce() -> T + 'static) -> T {
    let owner = with_runtime(|runtime| runtime.owner.get())
        .expect("runtime should be alive when created");
    let (value, disposer) = with_runtime(|runtime| {
        let prev_observer = runtime.observer.take();
        let prev_owner = runtime.owner.take();

        runtime.owner.set(owner);
        runtime.observer.set(owner);

        let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
            value: None,
            state: ReactiveNodeState::Clean,
            node_type: ReactiveNodeType::Trigger,
        });
        runtime.push_scope_property(ScopeProperty::Trigger(id));
        let disposer = Disposer(id);

        runtime.owner.set(Some(id));
        runtime.observer.set(Some(id));

        let v = f();

        runtime.observer.set(prev_observer);
        runtime.owner.set(prev_owner);

        (v, disposer)
    })
    .expect("runtime should be alive when run");
    on_cleanup(move || drop(disposer));
    value
}

impl RuntimeId {
    /// Removes the runtime, disposing of everything created in it.
    ///
    /// ## Panics
    /// Panics if the reactive runtime you’re trying to dispose is not found.
    /// This would suggest either that you’re trying to dispose of it twice, or
    /// that it was created in a different thread; panicking here indicates a
    /// memory leak.
    pub fn dispose(self) {
        cfg_if! {
            if #[cfg(not(any(feature = "csr", feature = "hydrate")))] {
                // remove this from the set of runtimes
                let runtime = RUNTIMES.with(move |runtimes| runtimes.borrow_mut().remove(self))
                    .expect("Attempted to dispose of a reactive runtime that was not found. This suggests \
                    a possible memory leak. Please open an issue with details at https://github.com/leptos-rs/leptos");

                // remove this from being the current runtime
                CURRENT_RUNTIME.with(|runtime| {
                    if runtime.get() == Some(self) {
                        runtime.take();
                    }
                });

                drop(runtime);
            }
        }
    }

    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", skip_all,)
    )]
    #[inline(always)]
    pub(crate) fn untrack<T>(
        self,
        f: impl FnOnce() -> T,
        #[allow(unused)] diagnostics: bool,
    ) -> T {
        with_runtime(|runtime| {
            let untracked_result;

            #[cfg(debug_assertions)]
            let prev = if !diagnostics {
                SpecialNonReactiveZone::enter()
            } else {
                false
            };

            let prev_observer = SetObserverOnDrop(runtime.observer.take());

            untracked_result = f();

            runtime.observer.set(prev_observer.0);
            std::mem::forget(prev_observer); // avoid Drop

            #[cfg(debug_assertions)]
            if !diagnostics {
                SpecialNonReactiveZone::exit(prev);
            }

            untracked_result
        })
        .expect(
            "tried to run untracked function in a runtime that has been \
             disposed",
        )
    }

    #[track_caller]
    #[inline(always)] // only because it's placed here to fit in with the other create methods
    pub(crate) fn create_trigger(self) -> Trigger {
        let id = with_runtime(|runtime| {
            let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
                value: None,
                state: ReactiveNodeState::Clean,
                node_type: ReactiveNodeType::Trigger,
            });
            runtime.push_scope_property(ScopeProperty::Trigger(id));
            id
        })
        .expect(
            "tried to create a trigger in a runtime that has been disposed",
        );

        Trigger {
            id,
            #[cfg(debug_assertions)]
            defined_at: std::panic::Location::caller(),
        }
    }

    pub(crate) fn create_concrete_signal(
        self,
        value: Rc<RefCell<dyn Any>>,
    ) -> NodeId {
        with_runtime(|runtime| {
            let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
                value: Some(value),
                state: ReactiveNodeState::Clean,
                node_type: ReactiveNodeType::Signal,
            });
            runtime.push_scope_property(ScopeProperty::Signal(id));
            id
        })
        .expect("tried to create a signal in a runtime that has been disposed")
    }

    #[track_caller]
    #[inline(always)]
    pub(crate) fn create_signal<T>(
        self,
        value: T,
    ) -> (ReadSignal<T>, WriteSignal<T>)
    where
        T: Any + 'static,
    {
        let id = self.create_concrete_signal(
            Rc::new(RefCell::new(value)) as Rc<RefCell<dyn Any>>
        );

        (
            ReadSignal {
                id,
                ty: PhantomData,
                #[cfg(any(debug_assertions, feature = "ssr"))]
                defined_at: std::panic::Location::caller(),
            },
            WriteSignal {
                id,
                ty: PhantomData,
                #[cfg(any(debug_assertions, feature = "ssr"))]
                defined_at: std::panic::Location::caller(),
            },
        )
    }

    #[track_caller]
    #[inline(always)]
    pub(crate) fn create_rw_signal<T>(self, value: T) -> RwSignal<T>
    where
        T: Any + 'static,
    {
        let id = self.create_concrete_signal(
            Rc::new(RefCell::new(value)) as Rc<RefCell<dyn Any>>
        );
        RwSignal {
            id,
            ty: PhantomData,
            #[cfg(any(debug_assertions, feature = "ssr"))]
            defined_at: std::panic::Location::caller(),
        }
    }

    pub(crate) fn create_concrete_effect(
        self,
        value: Rc<RefCell<dyn Any>>,
        effect: Rc<dyn AnyComputation>,
    ) -> NodeId {
        with_runtime(|runtime| {
            let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
                value: Some(Rc::clone(&value)),
                state: ReactiveNodeState::Dirty,
                node_type: ReactiveNodeType::Effect {
                    f: Rc::clone(&effect),
                },
            });
            runtime.push_scope_property(ScopeProperty::Effect(id));
            id
        })
        .expect("tried to create an effect in a runtime that has been disposed")
    }

    pub(crate) fn create_concrete_memo(
        self,
        value: Rc<RefCell<dyn Any>>,
        computation: Rc<dyn AnyComputation>,
    ) -> NodeId {
        with_runtime(|runtime| {
            let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
                value: Some(value),
                // memos are lazy, so are dirty when created
                // will be run the first time we ask for it
                state: ReactiveNodeState::Dirty,
                node_type: ReactiveNodeType::Memo { f: computation },
            });
            runtime.push_scope_property(ScopeProperty::Effect(id));
            id
        })
        .expect("tried to create a memo in a runtime that has been disposed")
    }

    #[track_caller]
    #[inline(always)]
    pub(crate) fn create_effect<T>(
        self,
        f: impl Fn(Option<T>) -> T + 'static,
    ) -> NodeId
    where
        T: Any + 'static,
    {
        self.create_concrete_effect(
            Rc::new(RefCell::new(None::<T>)),
            Rc::new(EffectState {
                f,
                ty: PhantomData,
                #[cfg(any(debug_assertions, feature = "ssr"))]
                defined_at: std::panic::Location::caller(),
            }),
        )
    }

    pub(crate) fn watch<W, T>(
        self,
        deps: impl Fn() -> W + 'static,
        callback: impl Fn(&W, Option<&W>, Option<T>) -> T + Clone + 'static,
        immediate: bool,
    ) -> (NodeId, impl Fn() + Clone)
    where
        W: Clone + 'static,
        T: 'static,
    {
        let cur_deps_value = Rc::new(RefCell::new(None::<W>));
        let prev_deps_value = Rc::new(RefCell::new(None::<W>));
        let prev_callback_value = Rc::new(RefCell::new(None::<T>));

        let wrapped_callback = {
            let cur_deps_value = Rc::clone(&cur_deps_value);
            let prev_deps_value = Rc::clone(&prev_deps_value);
            let prev_callback_value = Rc::clone(&prev_callback_value);

            move || {
                callback(
                    cur_deps_value.borrow().as_ref().expect(
                        "this will not be called before there is deps value",
                    ),
                    prev_deps_value.borrow().as_ref(),
                    prev_callback_value.take(),
                )
            }
        };

        let effect_fn = {
            let prev_callback_value = Rc::clone(&prev_callback_value);
            move |did_run_before: Option<()>| {
                let deps_value = deps();

                let did_run_before = did_run_before.is_some();

                if !immediate && !did_run_before {
                    prev_deps_value.replace(Some(deps_value));
                    return;
                }

                cur_deps_value.replace(Some(deps_value.clone()));

                let callback_value =
                    Some(self.untrack(wrapped_callback.clone(), false));

                prev_callback_value.replace(callback_value);

                prev_deps_value.replace(Some(deps_value));
            }
        };

        let id = self.create_concrete_effect(
            Rc::new(RefCell::new(None::<()>)),
            Rc::new(EffectState {
                f: effect_fn,
                ty: PhantomData,
                #[cfg(any(debug_assertions, feature = "ssr"))]
                defined_at: std::panic::Location::caller(),
            }),
        );

        (id, move || {
            with_runtime(|runtime| runtime.dispose_node(id)).expect(
                "tried to stop a watch in a runtime that has been disposed",
            );
        })
    }

    #[track_caller]
    #[inline(always)]
    pub(crate) fn create_owning_memo<T>(
        self,
        f: impl Fn(Option<T>) -> (T, bool) + 'static,
    ) -> Memo<T>
    where
        T: 'static,
    {
        Memo {
            id: self.create_concrete_memo(
                Rc::new(RefCell::new(None::<T>)),
                Rc::new(MemoState {
                    f,
                    t: PhantomData,
                    #[cfg(any(debug_assertions, feature = "ssr"))]
                    defined_at: std::panic::Location::caller(),
                }),
            ),
            ty: PhantomData,
            #[cfg(any(debug_assertions, feature = "ssr"))]
            defined_at: std::panic::Location::caller(),
        }
    }
}

impl Runtime {
    pub fn new() -> Self {
        let root = ReactiveNode {
            value: None,
            state: ReactiveNodeState::Clean,
            node_type: ReactiveNodeType::Trigger,
        };
        let mut nodes: SlotMap<NodeId, ReactiveNode> = SlotMap::default();
        let root_id = nodes.insert(root);

        Self {
            owner: Cell::new(Some(root_id)),
            nodes: RefCell::new(nodes),
            ..Self::default()
        }
    }

    pub(crate) fn create_unserializable_resource(
        &self,
        state: Rc<dyn UnserializableResource>,
    ) -> ResourceId {
        self.resources
            .borrow_mut()
            .insert(AnyResource::Unserializable(state))
    }

    pub(crate) fn create_serializable_resource(
        &self,
        state: Rc<dyn SerializableResource>,
    ) -> ResourceId {
        self.resources
            .borrow_mut()
            .insert(AnyResource::Serializable(state))
    }

    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", skip_all,)
    )]
    #[track_caller]
    pub(crate) fn resource<S, T, U>(
        &self,
        id: ResourceId,
        f: impl FnOnce(&ResourceState<S, T>) -> U,
    ) -> U
    where
        S: 'static,
        T: 'static,
    {
        self.try_resource(id, f).unwrap_or_else(|| {
            panic!(
                "couldn't locate {id:?} at {:?}",
                std::panic::Location::caller()
            );
        })
    }

    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", skip_all,)
    )]
    #[track_caller]
    pub(crate) fn try_resource<S, T, U>(
        &self,
        id: ResourceId,
        f: impl FnOnce(&ResourceState<S, T>) -> U,
    ) -> Option<U>
    where
        S: 'static,
        T: 'static,
    {
        let resources = { self.resources.borrow().clone() };
        let res = resources.get(id);
        res.map(|res| {
            let res_state = match res {
                AnyResource::Unserializable(res) => res.as_any(),
                AnyResource::Serializable(res) => res.as_any(),
            }
            .downcast_ref::<ResourceState<S, T>>();

            if let Some(n) = res_state {
                f(n)
            } else {
                panic!(
                    "couldn't convert {id:?} to ResourceState<{}, {}>",
                    std::any::type_name::<S>(),
                    std::any::type_name::<T>(),
                );
            }
        })
    }

    /// Returns IDs for all [`Resource`](crate::Resource)s found on any scope.
    pub(crate) fn all_resources(&self) -> Vec<ResourceId> {
        self.resources
            .borrow()
            .iter()
            .map(|(resource_id, _)| resource_id)
            .collect()
    }

    /// Returns IDs for all [`Resource`](crate::Resource)s found on any
    /// scope, pending from the server.
    pub(crate) fn pending_resources(&self) -> Vec<ResourceId> {
        self.resources
            .borrow()
            .iter()
            .filter_map(|(resource_id, res)| {
                if let AnyResource::Serializable(res) = res {
                    res.should_send_to_client().then_some(resource_id)
                } else {
                    None
                }
            })
            .collect()
    }

    pub(crate) fn serialization_resolvers(
        &self,
    ) -> FuturesUnordered<PinnedFuture<(ResourceId, String)>> {
        let f = FuturesUnordered::new();
        let resources = { self.resources.borrow().clone() };
        for (id, resource) in resources.iter() {
            if let AnyResource::Serializable(resource) = resource {
                if resource.should_send_to_client() {
                    f.push(resource.to_serialization_resolver(id));
                }
            }
        }
        f
    }

    /// Do not call on triggers
    pub(crate) fn get_value(
        &self,
        node_id: NodeId,
    ) -> Option<Rc<RefCell<dyn Any>>> {
        let signals = self.nodes.borrow();
        signals.get(node_id).map(|node| node.value())
    }
}

impl PartialEq for Runtime {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self, other)
    }
}

impl Eq for Runtime {}

impl std::hash::Hash for Runtime {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::ptr::hash(&self, state);
    }
}

struct SetObserverOnDrop(Option<NodeId>);

impl Drop for SetObserverOnDrop {
    fn drop(&mut self) {
        _ = with_runtime(|rt| {
            rt.observer.set(self.0);
        });
    }
}

/// Batches any reactive updates, preventing effects from running until the whole
/// function has run. This allows you to prevent rerunning effects if multiple
/// signal updates might cause the same effect to run.
///
/// # Panics
/// Panics if the runtime has already been disposed.
///
/// To avoid panicking under any circumstances, use [`try_batch`].
#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
#[inline(always)]
pub fn batch<T>(f: impl FnOnce() -> T) -> T {
    try_batch(f).expect(
        "tried to run a batched update in a runtime that has been disposed",
    )
}

/// Attempts to batch any reactive updates, preventing effects from running until the whole
/// function has run. This allows you to prevent rerunning effects if multiple
/// signal updates might cause the same effect to run.
///
/// Unlike [`batch`], this will not panic if the runtime has been disposed.
#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
#[inline(always)]
pub fn try_batch<T>(f: impl FnOnce() -> T) -> Result<T, ReactiveSystemError> {
    with_runtime(move |runtime| {
        let batching = SetBatchingOnDrop(runtime.batching.get());
        runtime.batching.set(true);

        let val = f();

        runtime.batching.set(batching.0);
        std::mem::forget(batching);

        runtime.run_effects();
        val
    })
}

struct SetBatchingOnDrop(bool);

impl Drop for SetBatchingOnDrop {
    fn drop(&mut self) {
        _ = with_runtime(|rt| {
            rt.batching.set(self.0);
        });
    }
}

/// Creates a cleanup function, which will be run when the current reactive owner is disposed.
///
/// It runs after child nodes have been disposed, but before signals, effects, and resources
/// are invalidated.
#[inline(always)]
pub fn on_cleanup(cleanup_fn: impl FnOnce() + 'static) {
    #[cfg(debug_assertions)]
    let cleanup_fn = move || {
        #[cfg(debug_assertions)]
        let prev = crate::SpecialNonReactiveZone::enter();
        cleanup_fn();
        #[cfg(debug_assertions)]
        {
            crate::SpecialNonReactiveZone::exit(prev);
        }
    };
    push_cleanup(Box::new(cleanup_fn))
}

#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
fn push_cleanup(cleanup_fn: Box<dyn FnOnce()>) {
    _ = with_runtime(|runtime| {
        if let Some(owner) = runtime.owner.get() {
            let mut cleanups = runtime.on_cleanups.borrow_mut();
            if let Some(entries) = cleanups.get_mut(owner) {
                entries.push(cleanup_fn);
            } else {
                cleanups.insert(owner, vec![cleanup_fn]);
            }
        }
    });
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ScopeProperty {
    Trigger(NodeId),
    Signal(NodeId),
    Effect(NodeId),
    Resource(ResourceId),
    StoredValue(StoredValueId),
}

impl ScopeProperty {
    pub fn to_node_id(self) -> Option<NodeId> {
        match self {
            Self::Trigger(node) | Self::Signal(node) | Self::Effect(node) => {
                Some(node)
            }
            _ => None,
        }
    }
}

/// Suspends reactive tracking while running the given function.
///
/// This can be used to isolate parts of the reactive graph from one another.
///
/// ```rust
/// # use leptos_reactive::*;
/// # let runtime = create_runtime();
/// let (a, set_a) = create_signal(0);
/// let (b, set_b) = create_signal(0);
/// let c = create_memo(move |_| {
///     // this memo will *only* update when `a` changes
///     a.get() + untrack(move || b.get())
/// });
///
/// assert_eq!(c.get(), 0);
/// set_a.set(1);
/// assert_eq!(c.get(), 1);
/// set_b.set(1);
/// // hasn't updated, because we untracked before reading b
/// assert_eq!(c.get(), 1);
/// set_a.set(2);
/// assert_eq!(c.get(), 3);
///
/// # runtime.dispose();
/// ```
#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
#[inline(always)]
pub fn untrack<T>(f: impl FnOnce() -> T) -> T {
    Runtime::current().untrack(f, false)
}

#[doc(hidden)]
#[cfg_attr(
    any(debug_assertions, feature = "ssr"),
    instrument(level = "trace", skip_all,)
)]
#[inline(always)]
pub fn untrack_with_diagnostics<T>(f: impl FnOnce() -> T) -> T {
    Runtime::current().untrack(f, true)
}

/// Allows running a future that has access to a given scope.
#[pin_project]
pub struct ScopedFuture<Fut: Future> {
    owner: Owner,
    #[pin]
    future: Fut,
}

/// Errors that can occur when trying to spawn a [`ScopedFuture`].
#[derive(Error, Debug, Clone)]
pub enum ScopedFutureError {
    #[error(
        "Tried to spawn a scoped Future without a current reactive Owner."
    )]
    NoCurrentOwner,
}

impl<Fut: Future + 'static> Future for ScopedFuture<Fut> {
    type Output = Option<Fut::Output>;

    fn poll(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Self::Output> {
        let this = self.project();

        if let Ok(poll) = try_with_owner(*this.owner, || this.future.poll(cx)) {
            match poll {
                Poll::Ready(res) => Poll::Ready(Some(res)),
                Poll::Pending => Poll::Pending,
            }
        } else {
            Poll::Ready(None)
        }
    }
}

impl<Fut: Future> ScopedFuture<Fut> {
    /// Creates a new future that will have access to the `[Owner]`'s
    /// scope context.
    pub fn new(owner: Owner, fut: Fut) -> Self {
        Self { owner, future: fut }
    }

    /// Runs the future in the current [`Owner`]'s scope context.
    #[track_caller]
    pub fn new_current(fut: Fut) -> Result<Self, ScopedFutureError> {
        Owner::current()
            .map(|owner| Self { owner, future: fut })
            .ok_or(ScopedFutureError::NoCurrentOwner)
    }
}

/// Runs a future that has access to the provided [`Owner`]'s
/// scope context.
#[track_caller]
pub fn spawn_local_with_owner(
    owner: Owner,
    fut: impl Future<Output = ()> + 'static,
) {
    let scoped_future = ScopedFuture::new(owner, fut);
    #[cfg(debug_assertions)]
    let loc = std::panic::Location::caller();

    crate::spawn_local(async move {
        if scoped_future.await.is_none() {
            crate::macros::debug_warn!(
                "`spawn_local_with_owner` called at {loc} returned `None`, \
                 i.e., its Owner was disposed before the `Future` resolved."
            );
        }
    });
}

/// Runs a future that has access to the provided [`Owner`]'s
/// scope context.
///
/// # Panics
/// Panics if there is no [`Owner`] context available.
#[track_caller]
pub fn spawn_local_with_current_owner(
    fut: impl Future<Output = ()> + 'static,
) -> Result<(), ScopedFutureError> {
    let scoped_future = ScopedFuture::new_current(fut)?;
    #[cfg(debug_assertions)]
    let loc = std::panic::Location::caller();

    crate::spawn_local(async move {
        if scoped_future.await.is_none() {
            crate::macros::debug_warn!(
                "`spawn_local_with_owner` called at {loc} returned `None`, \
                 i.e., its Owner was disposed before the `Future` resolved."
            );
        }
    });

    Ok(())
}

/// Runs a future that has access to the provided [`Owner`]'s
/// scope context.
///
/// Since futures run in the background, it is possible that
/// the scope has been cleaned up since the future started running.
/// If this happens, the future will not be completed.
///
/// The `on_cancelled` callback can be used to notify you that the
/// future was cancelled.
pub fn try_spawn_local_with_owner(
    owner: Owner,
    fut: impl Future<Output = ()> + 'static,
    on_cancelled: impl FnOnce() + 'static,
) {
    let scoped_future = ScopedFuture::new(owner, fut);

    crate::spawn_local(async move {
        if scoped_future.await.is_none() {
            on_cancelled();
        }
    });
}

/// Runs a future that has access to the provided [`Owner`]'s
/// scope context.
///
/// Since futures run in the background, it is possible that
/// the scope has been cleaned up since the future started running.
/// If this happens, the future will not be completed.
///
/// The `on_cancelled` callback can be used to notify you that the
/// future was cancelled.
///
/// # Panics
/// Panics if there is no [`Owner`] context available.
#[track_caller]
pub fn try_spawn_local_with_current_owner(
    fut: impl Future<Output = ()> + 'static,
    on_cancelled: impl FnOnce() + 'static,
) -> Result<(), ScopedFutureError> {
    let scoped_future = ScopedFuture::new_current(fut)?;

    crate::spawn_local(async move {
        if scoped_future.await.is_none() {
            on_cancelled();
        }
    });

    Ok(())
}