sigmut 0.0.2

a state management framework designed to be used as a foundation for UI frameworks.
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
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
use core::panic;
use std::{
    any::Any,
    cell::{Ref, RefCell},
    cmp::{max, min},
    future::{poll_fn, Future},
    mem::{replace, swap, take, transmute},
    ops::{BitOr, BitOrAssign},
    pin::Pin,
    ptr::null_mut,
    rc::{Rc, Weak},
    result::Result,
    sync::{Arc, Mutex, MutexGuard},
    task::{Context, Poll, Wake, Waker},
    thread::AccessError,
};

use bumpalo::Bump;
use derive_ex::{derive_ex, Ex};
use parse_display::Display;
use slabmap::SlabMap;

mod async_signal_context;
mod source_binder;
mod state_ref;
mod state_ref_builder;

pub use async_signal_context::*;
pub use source_binder::SourceBinder;
pub use state_ref::StateRef;
pub use state_ref_builder::StateRefBuilder;

use crate::utils::isize_map::ISizeMap;

thread_local! {
    static GLOBALS: RefCell<Globals> = RefCell::new(Globals::new());
}

struct Globals {
    is_runtime_exists: bool,
    unbinds: Vec<Vec<SourceBinding>>,
    actions: Vec<Action>,
    notifys: Vec<NotifyTask>,
    need_wake: bool,
    wakes: WakeTable,
    tasks: Tasks,
}
impl Globals {
    fn new() -> Self {
        Self {
            is_runtime_exists: false,
            unbinds: Vec::new(),
            actions: Vec::new(),
            notifys: Vec::new(),
            need_wake: false,
            wakes: WakeTable::default(),
            tasks: Tasks::new(),
        }
    }
    fn with<T>(f: impl FnOnce(&mut Self) -> T) -> T {
        GLOBALS.with(|g| f(&mut g.borrow_mut()))
    }
    fn try_with<T>(f: impl FnOnce(&mut Self) -> T) -> Result<T, AccessError> {
        GLOBALS.try_with(|g| f(&mut g.borrow_mut()))
    }
    fn schedule_task(kind: TaskKind, task: Task) {
        Self::with(|g| {
            g.tasks.push(kind, task);
            g.wake();
        })
    }
    fn get_notifys(notifys: &mut Vec<NotifyTask>) -> bool {
        Self::with(|g| {
            g.apply_wake();
            swap(notifys, &mut g.notifys);
        });
        !notifys.is_empty()
    }

    fn get_tasks(kind: Option<TaskKind>, tasks: &mut Vec<Task>) {
        Self::with(|g| {
            g.tasks.drain(kind, tasks);
        })
    }
    fn get_actions(actions: &mut Vec<Action>) -> bool {
        Self::with(|g| {
            g.apply_wake();
            swap(actions, &mut g.actions);
        });
        !actions.is_empty()
    }

    fn swap_vec<T>(f: impl FnOnce(&mut Self) -> &mut Vec<T>, values: &mut Vec<T>) -> bool {
        Self::with(|g| swap(f(g), values));
        !values.is_empty()
    }
    fn assert_exists(&self) {
        if !self.is_runtime_exists {
            panic!("`Runtime` is not created.");
        }
    }

    fn push_action(&mut self, action: Action) {
        self.assert_exists();
        self.actions.push(action);
        self.wake();
    }
    fn push_notify(&mut self, sink: Weak<dyn BindSink>, slot: Slot) {
        self.notifys.push(NotifyTask { sink, slot });
        self.wake();
    }
    fn apply_wake(&mut self) {
        let mut requests = self.wakes.requests.0.lock().unwrap();
        for key in requests.drops.drain(..) {
            self.wakes.tasks.remove(key);
        }
        for key in requests.wakes.drain(..) {
            if let Some(task) = self.wakes.tasks.get(key) {
                match task {
                    WakeTask::Notify(task) => {
                        self.notifys.push(task.clone());
                    }
                    WakeTask::AsyncAction(action) => self.actions.push(action.to_action()),
                }
            }
        }
    }
    fn wait_for_ready(&mut self, cx: &Context) -> Poll<()> {
        self.need_wake = false;
        if !self.notifys.is_empty()
            || !self.actions.is_empty()
            || !self.tasks.is_empty()
            || !self.unbinds.is_empty()
        {
            return Poll::Ready(());
        }
        let mut requests = self.wakes.requests.0.lock().unwrap();
        if !requests.drops.is_empty() || !requests.wakes.is_empty() {
            return Poll::Ready(());
        }
        requests.waker = Some(cx.waker().clone());
        self.need_wake = true;
        Poll::Pending
    }

    fn finish_runtime(&mut self) {
        self.is_runtime_exists = false;
    }

    fn wake(&mut self) {
        if !self.need_wake {
            return;
        }
        self.need_wake = false;
        self.wakes.requests.0.lock().unwrap().wake();
    }
}

/// Reactive runtime.
#[derive_ex(Default)]
#[default(Self::new())]
pub struct Runtime {
    rt: RawRuntime,
    bump: Bump,
    notifys_buffer: Vec<NotifyTask>,
    actions_buffer: Vec<Action>,
    tasks_buffer: Vec<Task>,
    unbinds_buffer: Vec<Vec<SourceBinding>>,
}
impl Runtime {
    pub fn new() -> Self {
        if Globals::with(|g| replace(&mut g.is_runtime_exists, true)) {
            panic!("Only one `Runtime` can exist in the same thread at the same time.");
        };
        Self {
            rt: RawRuntime::new(),
            bump: Bump::new(),
            notifys_buffer: Vec::new(),
            actions_buffer: Vec::new(),
            tasks_buffer: Vec::new(),
            unbinds_buffer: Vec::new(),
        }
    }

    pub fn ac(&mut self) -> &mut ActionContext {
        ActionContext::new(self)
    }
    fn nc(&mut self) -> &mut NotifyContext {
        self.ac().nc()
    }
    fn uc(&mut self) -> UpdateContext {
        UpdateContext(self.sc_raw())
    }
    pub fn sc(&mut self) -> SignalContext {
        self.apply_notify();
        let sc = self.sc_raw();
        sc
    }
    fn sc_raw(&mut self) -> SignalContext {
        SignalContext {
            rt: &mut self.rt,
            bump: &self.bump,
            sink: None,
        }
    }

    /// Perform scheduled actions.
    ///
    /// Returns `true` if any action was performed.
    pub fn run_actions(&mut self) -> bool {
        let mut handled = false;
        let mut actions = take(&mut self.actions_buffer);
        while Globals::get_actions(&mut actions) {
            for action in actions.drain(..) {
                action.call(self.ac());
                handled = true;
            }
        }
        self.actions_buffer = actions;
        handled
    }

    /// Perform scheduled tasks.
    ///
    /// If `kind` is `None`, all tasks are executed.
    ///
    /// Returns `true` if any task was performed.
    pub fn run_tasks(&mut self, kind: Option<TaskKind>) -> bool {
        self.apply_notify();
        let mut tasks = take(&mut self.tasks_buffer);
        Globals::get_tasks(kind, &mut tasks);
        let handled = !tasks.is_empty();
        for task in tasks.drain(..) {
            task.run(&mut self.uc());
        }
        self.tasks_buffer = tasks;
        handled
    }
    fn apply_unbind(&mut self) -> bool {
        let mut handled = false;
        let mut unbinds = take(&mut self.unbinds_buffer);
        while Globals::swap_vec(|g| &mut g.unbinds, &mut unbinds) {
            for unbind in unbinds.drain(..) {
                for sb in unbind {
                    sb.unbind(&mut self.uc());
                }
                handled = true;
            }
        }
        self.unbinds_buffer = unbinds;
        handled
    }
    fn apply_notify(&mut self) -> bool {
        let mut handled = self.apply_unbind();
        let mut notifys = take(&mut self.notifys_buffer);
        while Globals::get_notifys(&mut notifys) {
            for notify in notifys.drain(..) {
                notify.call_notify(self.nc());
                handled = true;
            }
        }
        self.notifys_buffer = notifys;
        handled
    }

    /// Perform scheduled discards.
    ///
    /// Returns `true` if any discard was performed.
    pub fn run_discards(&mut self) -> bool {
        let mut handled = false;
        loop {
            if let Some(task) = self.rt.discards.pop() {
                task.call_discard(&mut self.uc());
                handled = true;
                continue;
            }
            if self.apply_unbind() {
                handled = true;
                continue;
            }
            break;
        }
        handled
    }

    /// Repeat until there are no more processes to do
    /// [`run_actions`](Self::run_actions), [`run_tasks`](Self::run_tasks), or [`run_discards`](Self::run_discards).
    pub fn update(&mut self) {
        loop {
            if self.run_actions() {
                continue;
            }
            if self.run_tasks(None) {
                continue;
            }
            if self.run_discards() {
                continue;
            }
            break;
        }
    }

    /// Wait while there is no process to be executed by [`update`](Self::update).
    pub async fn wait_for_ready(&mut self) {
        poll_fn(|cx| Globals::with(|g| g.wait_for_ready(cx))).await
    }

    fn cancel_async_actions(&mut self) {
        let mut acts = Vec::new();
        while !self.rt.async_actions.is_empty() {
            acts.extend(self.rt.async_actions.values().cloned());
            for act in &acts {
                act.cancel(self.ac());
            }
            acts.clear();
        }
    }
}

impl Drop for Runtime {
    fn drop(&mut self) {
        self.cancel_async_actions();
        Globals::with(|g| g.finish_runtime());
    }
}

struct RawRuntime {
    discards: Vec<DiscardTask>,
    async_actions: SlabMap<Rc<AsyncAction>>,
}

impl RawRuntime {
    pub fn new() -> Self {
        Self {
            discards: Vec::new(),
            async_actions: SlabMap::new(),
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum Dirty {
    Clean,
    MaybeDirty,
    Dirty,
}
impl Dirty {
    pub fn from_is_dirty(is_dirty: bool) -> Self {
        if is_dirty {
            Dirty::Dirty
        } else {
            Dirty::Clean
        }
    }
    pub fn is_clean(self) -> bool {
        self == Dirty::Clean
    }
}

impl BitOr for Dirty {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        max(self, rhs)
    }
}
impl BitOrAssign for Dirty {
    fn bitor_assign(&mut self, rhs: Self) {
        *self = *self | rhs;
    }
}

impl BitOr<DirtyOrMaybeDirty> for Dirty {
    type Output = Self;
    fn bitor(self, rhs: DirtyOrMaybeDirty) -> Self {
        max(self, rhs.into())
    }
}
impl BitOrAssign<DirtyOrMaybeDirty> for Dirty {
    fn bitor_assign(&mut self, rhs: DirtyOrMaybeDirty) {
        *self = *self | rhs;
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum DirtyOrMaybeDirty {
    Dirty,
    MaybeDirty,
}
impl DirtyOrMaybeDirty {
    pub fn with_filter(self, filter: bool) -> Self {
        if filter {
            DirtyOrMaybeDirty::MaybeDirty
        } else {
            self
        }
    }
}

impl From<DirtyOrMaybeDirty> for Dirty {
    fn from(value: DirtyOrMaybeDirty) -> Self {
        match value {
            DirtyOrMaybeDirty::Dirty => Dirty::Dirty,
            DirtyOrMaybeDirty::MaybeDirty => Dirty::MaybeDirty,
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Slot(pub usize);

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct BindKey(usize);

struct SourceBinding {
    source: Rc<dyn BindSource>,
    slot: Slot,
    key: BindKey,
}
impl SourceBinding {
    fn is_same(&self, node: &Rc<dyn BindSource>, slot: Slot) -> bool {
        Rc::ptr_eq(&self.source, node) && self.slot == slot
    }
    fn check(&self, uc: &mut UpdateContext) -> bool {
        self.source.clone().check(self.slot, self.key, uc)
    }
    fn unbind(self, uc: &mut UpdateContext) {
        self.source.unbind(self.slot, self.key, uc);
    }
    fn rebind(self, sc: &mut SignalContext) {
        self.source.rebind(self.slot, self.key, sc);
    }
}

#[derive(Default)]
pub struct SourceBindings(Vec<SourceBinding>);

impl SourceBindings {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn check(&self, uc: &mut UpdateContext) -> bool {
        for source in &self.0 {
            if source.check(uc) {
                return true;
            }
        }
        false
    }
    fn check_with(&mut self, dirty: &mut Dirty, uc: &mut UpdateContext) -> bool {
        if *dirty == Dirty::MaybeDirty {
            *dirty = Dirty::from_is_dirty(self.check(uc));
        }
        *dirty == Dirty::Dirty
    }

    pub fn update<T>(
        &mut self,
        sink: Weak<dyn BindSink>,
        slot: Slot,
        reset: bool,
        f: impl FnOnce(&mut SignalContext) -> T,
        uc: &mut UpdateContext,
    ) -> T {
        let sources_len = if reset { 0 } else { self.0.len() };
        let mut sink = Sink {
            sink,
            slot,
            sources: take(self),
            sources_len,
        };
        let mut sc = SignalContext {
            rt: uc.0.rt,
            bump: uc.0.bump,
            sink: Some(&mut sink),
        };

        let ret = f(&mut sc);
        *self = sink.sources;
        for b in self.0.drain(sink.sources_len..) {
            b.unbind(uc);
        }
        ret
    }
    pub fn clear(&mut self, uc: &mut UpdateContext) {
        for b in self.0.drain(..) {
            b.unbind(uc)
        }
    }
}
impl Drop for SourceBindings {
    fn drop(&mut self) {
        if !self.0.is_empty() {
            let _ = Globals::try_with(|g| g.unbinds.push(take(&mut self.0)));
        }
    }
}

struct SinkBinding {
    sink: Weak<dyn BindSink>,
    slot: Slot,
    dirty: Dirty,
}

impl SinkBinding {
    fn notify(&self, dirty: DirtyOrMaybeDirty, nc: &mut NotifyContext) {
        if let Some(node) = self.sink.upgrade() {
            node.notify(self.slot, dirty, nc)
        }
    }
}

#[derive(Default)]
pub struct SinkBindings(SlabMap<SinkBinding>);

impl SinkBindings {
    pub fn new() -> Self {
        Self(SlabMap::new())
    }
    pub fn bind(&mut self, this: Rc<dyn BindSource>, this_slot: Slot, sc: &mut SignalContext) {
        let Some(sink) = &mut sc.sink else {
            return;
        };
        let sources_index = sink.sources_len;
        if let Some(source_old) = sink.sources.0.get(sources_index) {
            if source_old.is_same(&this, this_slot) {
                sink.sources_len += 1;
                self.0[source_old.key.0].dirty = Dirty::Clean;
                return;
            }
        }
        let key = BindKey(self.0.insert(SinkBinding {
            sink: sink.sink.clone(),
            slot: sink.slot,
            dirty: Dirty::Clean,
        }));
        if let Some(old) = sink.push(SourceBinding {
            source: this,
            slot: this_slot,
            key,
        }) {
            old.unbind(sc.uc());
        }
    }
    pub fn rebind(
        &mut self,
        this: Rc<dyn BindSource>,
        this_slot: Slot,
        key: BindKey,
        sc: &mut SignalContext,
    ) {
        if let Some(sink) = &mut sc.sink {
            self.0[key.0].slot = sink.slot;
            if let Some(old) = sink.push(SourceBinding {
                source: this,
                slot: this_slot,
                key,
            }) {
                old.unbind(sc.uc());
            }
        } else {
            self.unbind(key, sc.uc());
        }
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    pub fn is_dirty(&self, key: BindKey, _uc: &mut UpdateContext) -> bool {
        match self.0[key.0].dirty {
            Dirty::Clean => false,
            Dirty::MaybeDirty => panic!("`is_dirty` called before `update()`"),
            Dirty::Dirty => true,
        }
    }
    /// Unbinds the dependency identified by the given `key`.
    pub fn unbind(&mut self, key: BindKey, _uc: &mut UpdateContext) {
        self.0.remove(key.0);
    }

    pub fn notify(&mut self, dirty: DirtyOrMaybeDirty, nc: &mut NotifyContext) {
        self.0.optimize();
        for binding in self.0.values_mut() {
            if binding.dirty.is_clean() {
                binding.notify(dirty, nc);
            }
            binding.dirty |= dirty;
        }
    }
    pub fn update(&mut self, is_dirty: bool, _uc: &mut UpdateContext) {
        self.0.optimize();
        for binding in self.0.values_mut() {
            if binding.dirty == Dirty::MaybeDirty {
                binding.dirty = Dirty::from_is_dirty(is_dirty);
            }
        }
    }
}

struct Sink {
    sink: Weak<dyn BindSink>,
    slot: Slot,
    sources: SourceBindings,
    sources_len: usize,
}
impl Sink {
    #[must_use]
    fn push(&mut self, binding: SourceBinding) -> Option<SourceBinding> {
        let index = self.sources_len;
        self.sources_len += 1;
        if index < self.sources.0.len() {
            Some(replace(&mut self.sources.0[index], binding))
        } else {
            self.sources.0.push(binding);
            None
        }
    }
}

#[repr(transparent)]
pub struct UpdateContext<'s>(SignalContext<'s>);

impl<'s> UpdateContext<'s> {
    fn new<'a>(sc: &'a mut SignalContext<'s>) -> &'a mut Self {
        unsafe { transmute(sc) }
    }

    /// Register a task to discard the cache.
    ///
    /// Registered tasks are called when [`Runtime::run_discards`] is called.
    pub fn schedule_discard(&mut self, discard: Rc<dyn Discard>, slot: Slot) {
        self.0.rt.discards.push(DiscardTask {
            node: discard,
            slot,
        })
    }

    /// Call a function with a [`SignalContext`] that does not track dependencies.
    pub fn sc_with<T>(&mut self, f: impl FnOnce(&mut SignalContext<'s>) -> T) -> T {
        self.0.untrack(f)
    }

    /// Borrow a [`RefCell`] that succeeds in borrowing if there are no cyclic dependencies.
    pub fn borrow<'a, T>(&self, cell: &'a RefCell<T>) -> Ref<'a, T> {
        match cell.try_borrow() {
            Ok(b) => b,
            Err(_) => panic!("detect cyclic dependency"),
        }
    }
}

/// Context for state invalidation notification
#[repr(transparent)]
pub struct NotifyContext(ActionContext);

impl NotifyContext {
    fn new(ac: &mut ActionContext) -> &mut Self {
        unsafe { transmute(ac) }
    }
}

/// Schedules state invalidation notifications.
///
/// If [`NotifyContext`] is available, this function should not be called and update notification should be done directly.
pub fn schedule_notify(node: Weak<dyn BindSink>, slot: Slot) {
    let _ = Globals::try_with(|rg| rg.push_notify(node, slot));
}

/// Context for retrieving state and tracking dependencies.
pub struct SignalContext<'s> {
    rt: &'s mut RawRuntime,
    bump: &'s Bump,
    sink: Option<&'s mut Sink>,
}

impl<'s> SignalContext<'s> {
    pub fn uc(&mut self) -> &mut UpdateContext<'s> {
        UpdateContext::new(self)
    }

    /// Call a function with a [`SignalContext`] that does not track dependencies.
    pub fn untrack<T>(&mut self, f: impl FnOnce(&mut SignalContext<'s>) -> T) -> T {
        struct UntrackGuard<'s, 'a> {
            sc: &'a mut SignalContext<'s>,
            sink: Option<&'s mut Sink>,
        }
        impl Drop for UntrackGuard<'_, '_> {
            fn drop(&mut self) {
                self.sc.sink = self.sink.take();
            }
        }
        f(UntrackGuard {
            sink: self.sink.take(),
            sc: self,
        }
        .sc)
    }
    fn extend(&mut self, from: &mut SourceBindings) {
        for binding in from.0.drain(..) {
            binding.rebind(self);
        }
    }
}

pub trait BindSink: 'static {
    fn notify(self: Rc<Self>, slot: Slot, dirty: DirtyOrMaybeDirty, nc: &mut NotifyContext);
}

pub trait BindSource: 'static {
    fn check(self: Rc<Self>, slot: Slot, key: BindKey, uc: &mut UpdateContext) -> bool;
    fn unbind(self: Rc<Self>, slot: Slot, key: BindKey, uc: &mut UpdateContext);
    fn rebind(self: Rc<Self>, slot: Slot, key: BindKey, sc: &mut SignalContext);
}

#[derive(Clone)]
struct NotifyTask {
    sink: Weak<dyn BindSink>,
    slot: Slot,
}
impl NotifyTask {
    fn call_notify(&self, nc: &mut NotifyContext) {
        if let Some(sink) = self.sink.upgrade() {
            sink.notify(self.slot, DirtyOrMaybeDirty::Dirty, nc)
        }
    }
}

pub trait Discard {
    fn discard(self: Rc<Self>, slot: Slot, uc: &mut UpdateContext);
}
struct DiscardTask {
    node: Rc<dyn Discard>,
    slot: Slot,
}
impl DiscardTask {
    fn call_discard(self, uc: &mut UpdateContext) {
        self.node.discard(self.slot, uc)
    }
}

/// Context for changing state.
#[repr(transparent)]
pub struct ActionContext(Runtime);

impl ActionContext {
    fn new(rt: &mut Runtime) -> &mut Self {
        unsafe { transmute(rt) }
    }
    pub fn nc(&mut self) -> &mut NotifyContext {
        NotifyContext::new(self)
    }
    pub fn sc(&mut self) -> SignalContext {
        self.0.sc()
    }
}

/// Spawns a new action.
pub fn spawn_action(f: impl FnOnce(&mut ActionContext) + 'static) {
    Action::Box(Box::new(f)).schedule()
}

/// Spawns a new asynchronous action.
pub fn spawn_action_async<Fut>(f: impl FnOnce(AsyncActionContext) -> Fut + 'static)
where
    Fut: Future<Output = ()> + 'static,
{
    spawn_action(|ac| AsyncAction::start(ac, f))
}

/// Spawns a new action without heap allocation.
///
/// `f` should be of a zero-sized type.
/// If `f` is not a zero-sized type, heap allocation will occur.
pub fn spawn_action_rc<T: Any>(
    this: Rc<T>,
    f: impl Fn(Rc<T>, &mut ActionContext) + Copy + 'static,
) {
    Action::from_rc(this, f).schedule()
}

#[allow(clippy::type_complexity)]
enum Action {
    Box(Box<dyn FnOnce(&mut ActionContext)>),
    Rc {
        this: Rc<dyn Any>,
        f: Box<dyn Fn(Rc<dyn Any>, &mut ActionContext)>,
    },
}

impl Action {
    fn from_rc<T: Any>(this: Rc<T>, f: impl Fn(Rc<T>, &mut ActionContext) + 'static) -> Self {
        Action::Rc {
            this,
            f: Box::new(move |this, ac| f(this.downcast().unwrap(), ac)),
        }
    }
    fn call(self, ac: &mut ActionContext) {
        match self {
            Action::Box(f) => f(ac),
            Action::Rc { this, f } => f(this, ac),
        }
    }
    fn schedule(self) {
        let _ = Globals::try_with(|g| g.push_action(self));
    }
}
struct AsyncAction {
    aac_source: AsyncActionContextSource,
    data: RefCell<Option<AsyncActionData>>,
}
impl AsyncAction {
    fn start<Fut>(ac: &mut ActionContext, f: impl FnOnce(AsyncActionContext) -> Fut + 'static)
    where
        Fut: Future<Output = ()> + 'static,
    {
        let aac_source = AsyncActionContextSource::new();
        let aac = aac_source.context();
        let future = aac_source.call(ac, || f(aac));
        let action = Rc::new(Self {
            aac_source,
            data: RefCell::new(None),
        });
        let id = ac.0.rt.async_actions.insert(action.clone());
        *action.data.borrow_mut() = Some(AsyncActionData {
            id,
            waker: WakeTask::AsyncAction(action.clone()).into_waker(),
            future: Box::pin(future),
        });
        action.next(ac);
    }
    fn call(
        self: &Rc<Self>,
        ac: &mut ActionContext,
        f: impl FnOnce(&mut Option<AsyncActionData>) -> Option<usize>,
    ) {
        let id_remove = self.aac_source.call(ac, || f(&mut self.data.borrow_mut()));
        if let Some(id_remove) = id_remove {
            ac.0.rt.async_actions.remove(id_remove);
        }
    }

    fn cancel(self: &Rc<Self>, ac: &mut ActionContext) {
        self.call(ac, |data| Some(data.take()?.id))
    }
    fn next(self: Rc<Self>, ac: &mut ActionContext) {
        self.call(ac, |data| {
            let d = data.as_mut()?;
            let mut cx = Context::from_waker(&d.waker);
            if d.future.as_mut().poll(&mut cx).is_ready() {
                Some(data.take()?.id)
            } else {
                None
            }
        });
    }
    fn to_action(self: &Rc<Self>) -> Action {
        Action::from_rc(self.clone(), Self::next)
    }
}

struct AsyncActionData {
    future: Pin<Box<dyn Future<Output = ()>>>,
    waker: Waker,
    id: usize,
}

#[derive(Default)]
struct WakeTable {
    tasks: SlabMap<WakeTask>,
    requests: WakeRequests,
}

impl WakeTable {
    fn insert(&mut self, task: WakeTask) -> Arc<RawWake> {
        RawWake::new(&self.requests, self.tasks.insert(task))
    }
}
enum WakeTask {
    Notify(NotifyTask),
    AsyncAction(Rc<AsyncAction>),
}
impl WakeTask {
    fn into_waker(self) -> Waker {
        Globals::with(|sc| sc.wakes.insert(self)).into()
    }
}
pub fn waker_from_sink(sink: Weak<impl BindSink>, slot: Slot) -> Waker {
    WakeTask::Notify(NotifyTask { sink, slot }).into_waker()
}

#[derive(Clone, Default)]
struct WakeRequests(Arc<Mutex<RawWakeRequests>>);

#[derive(Default)]
struct RawWakeRequests {
    wakes: Vec<usize>,
    drops: Vec<usize>,
    waker: Option<Waker>,
}
impl RawWakeRequests {
    fn wake(&mut self) {
        if let Some(waker) = self.waker.take() {
            waker.wake();
        }
    }
}

struct RawWake {
    requests: WakeRequests,
    key: usize,
}
impl RawWake {
    fn new(requests: &WakeRequests, key: usize) -> Arc<Self> {
        Arc::new(RawWake {
            requests: requests.clone(),
            key,
        })
    }
    fn requests(&self) -> MutexGuard<RawWakeRequests> {
        self.requests.0.lock().unwrap()
    }
}

impl Wake for RawWake {
    fn wake(self: Arc<Self>) {
        let mut requests = self.requests();
        requests.wakes.push(self.key);
        requests.wake();
    }
}
impl Drop for RawWake {
    fn drop(&mut self) {
        self.requests().drops.push(self.key);
    }
}

struct AsyncActionContextSource(Rc<RefCell<*mut Runtime>>);

impl AsyncActionContextSource {
    fn new() -> Self {
        Self(Rc::new(RefCell::new(null_mut())))
    }
    fn call<T>(&self, ac: &mut ActionContext, f: impl FnOnce() -> T) -> T {
        let p: *mut Runtime = &mut ac.0;
        assert!(self.0.borrow().is_null());
        *self.0.borrow_mut() = p;
        let ret = f();
        assert!(*self.0.borrow() == p);
        *self.0.borrow_mut() = null_mut();
        ret
    }
    fn context(&self) -> AsyncActionContext {
        AsyncActionContext(self.0.clone())
    }
}

/// Context for asynchronous state change.
pub struct AsyncActionContext(Rc<RefCell<*mut Runtime>>);

impl AsyncActionContext {
    pub fn call<T>(&self, f: impl FnOnce(&mut ActionContext) -> T) -> T {
        let mut b = self.0.borrow_mut();
        assert!(
            !b.is_null(),
            "`AsyncActionContext` cannot be used after being moved."
        );
        unsafe { f((**b).ac()) }
    }
}

pub struct Task(RawTask);

impl Task {
    pub fn new(f: impl FnOnce(&mut UpdateContext) + 'static) -> Self {
        Task(RawTask::Box(Box::new(f)))
    }
    pub fn from_rc_fn<T: Any>(
        this: Rc<T>,
        f: impl Fn(Rc<T>, &mut UpdateContext) + Copy + 'static,
    ) -> Self {
        Task(RawTask::Rc {
            this,
            f: Box::new(move |this, uc| f(this.downcast().unwrap(), uc)),
        })
    }
    pub fn from_weak_fn<T: Any>(
        this: Weak<T>,
        f: impl Fn(Rc<T>, &mut UpdateContext) + Copy + 'static,
    ) -> Self {
        Task(RawTask::Weak {
            this,
            f: Box::new(move |this, uc| {
                if let Some(this) = this.upgrade() {
                    f(this.downcast().unwrap(), uc)
                }
            }),
        })
    }

    pub fn schedule_with(self, kind: TaskKind) {
        Globals::schedule_task(kind, self)
    }
    pub fn schedule(self) {
        self.schedule_with(TaskKind::default());
    }
    fn run(self, uc: &mut UpdateContext) {
        match self.0 {
            RawTask::Box(f) => f(uc),
            RawTask::Rc { this, f } => f(this, uc),
            RawTask::Weak { this, f } => f(this, uc),
        }
    }
}

enum RawTask {
    Box(Box<dyn FnOnce(&mut UpdateContext)>),
    Rc {
        this: Rc<dyn Any>,
        #[allow(clippy::type_complexity)]
        f: Box<dyn Fn(Rc<dyn Any>, &mut UpdateContext)>,
    },
    Weak {
        this: Weak<dyn Any>,
        #[allow(clippy::type_complexity)]
        f: Box<dyn Fn(Weak<dyn Any>, &mut UpdateContext)>,
    },
}

/// kind of tasks performed by the reactive runtime.
#[derive(Clone, Copy, Display, Debug, Ex)]
#[derive_ex(PartialEq, Eq, Hash, Default)]
#[display("{id}: {name}")]
#[default(Self::new(0, "<default>"))]
pub struct TaskKind {
    id: i8,
    #[eq(ignore)]
    name: &'static str,
}
impl TaskKind {
    pub const fn new(id: i8, name: &'static str) -> Self {
        Self { id, name }
    }
}

#[derive(Ex)]
#[derive_ex(Default)]
#[default(Self::new())]
struct Tasks {
    tasks: ISizeMap<Vec<Task>>,
    start: isize,
    last: isize,
}
impl Tasks {
    fn new() -> Self {
        Self {
            tasks: ISizeMap::new(),
            start: isize::MAX,
            last: isize::MIN,
        }
    }

    fn is_empty(&self) -> bool {
        self.start == isize::MAX
    }
    fn set_empty(&mut self) {
        self.start = isize::MAX;
        self.last = isize::MIN;
    }
    fn push(&mut self, kind: TaskKind, task: Task) {
        let index = kind.id as isize;
        self.tasks[index].push(task);
        self.start = min(self.start, index);
        self.last = max(self.last, index);
    }
    fn drain(&mut self, kind: Option<TaskKind>, to: &mut Vec<Task>) {
        if let Some(kind) = kind {
            let index = kind.id as isize;
            if let Some(tasks) = self.tasks.get_mut(index) {
                to.append(tasks)
            }
            if self.start == index {
                self.start += 1;
            }
            if self.start > self.last {
                self.set_empty();
            }
        } else {
            for index in self.start..=self.last {
                to.append(&mut self.tasks[index])
            }
            self.set_empty();
        }
    }
}

#[non_exhaustive]
#[derive(Display, Debug)]
#[display("detect cyclic dependency")]
pub struct CyclicError {}

impl std::error::Error for CyclicError {}