dioxus_signals/
set_compare.rs

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
use crate::write::Writable;
use std::hash::Hash;

use crate::read::Readable;
use dioxus_core::prelude::*;
use futures_util::StreamExt;
use generational_box::{Storage, UnsyncStorage};

use crate::{CopyValue, ReadOnlySignal, Signal, SignalData};
use rustc_hash::FxHashMap;

/// An object that can efficiently compare a value to a set of values.
#[derive(Debug)]
pub struct SetCompare<R: 'static, S: Storage<SignalData<bool>> = UnsyncStorage> {
    subscribers: CopyValue<FxHashMap<R, Signal<bool, S>>>,
}

impl<R: Eq + Hash> SetCompare<R> {
    /// Creates a new [`SetCompare`] which efficiently tracks when a value changes to check if it is equal to a set of values.
    ///
    /// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_memo`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
    #[track_caller]
    pub fn new(f: impl FnMut() -> R + 'static) -> SetCompare<R> {
        Self::new_maybe_sync(f)
    }
}

impl<R: Eq + Hash, S: Storage<SignalData<bool>>> SetCompare<R, S> {
    /// Creates a new [`SetCompare`] that may be `Sync + Send` which efficiently tracks when a value changes to check if it is equal to a set of values.
    ///
    /// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_memo`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
    #[track_caller]
    pub fn new_maybe_sync(mut f: impl FnMut() -> R + 'static) -> SetCompare<R> {
        let subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
            CopyValue::new(FxHashMap::default());
        let mut previous = CopyValue::new(None);

        let mut recompute = move || {
            let subscribers = subscribers.read();
            let mut previous = previous.write();

            if let Some(previous) = previous.take() {
                if let Some(mut value) = subscribers.get(&previous).cloned() {
                    *value.write() = false;
                }
            }

            let current = f();

            if let Some(mut value) = subscribers.get(&current).cloned() {
                *value.write() = true;
            }

            *previous = Some(current);
        };
        let (rc, mut changed) = ReactiveContext::new();
        spawn(async move {
            loop {
                // Recompute the value
                rc.reset_and_run_in(&mut recompute);

                // Wait for context to change
                let _ = changed.next().await;
            }
        });

        SetCompare { subscribers }
    }

    /// Returns a signal which is true when the value is equal to the value passed to this function.
    pub fn equal(&mut self, value: R) -> ReadOnlySignal<bool, S> {
        let subscribers = self.subscribers.write();

        match subscribers.get(&value) {
            Some(&signal) => signal.into(),
            None => {
                drop(subscribers);
                let mut subscribers = self.subscribers.write();
                let signal = Signal::new_maybe_sync(false);
                subscribers.insert(value, signal);
                signal.into()
            }
        }
    }
}

impl<R: 'static, S: Storage<SignalData<bool>>> PartialEq for SetCompare<R, S> {
    fn eq(&self, other: &Self) -> bool {
        self.subscribers == other.subscribers
    }
}

impl<R, S: Storage<SignalData<bool>>> Clone for SetCompare<R, S> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<R, S: Storage<SignalData<bool>>> Copy for SetCompare<R, S> {}