floem_reactive/
derived.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
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
use std::marker::PhantomData;

use crate::{read::SignalTrack, RwSignal, SignalGet, SignalUpdate, SignalWith};

/// A signal that is derived from an [RwSignal](super::RwSignal) but lets you specify getters and setters for the signal.
///
/// This is useful when you want a single state variable and don't want to use effects to synchronize multiple signals.
///
/// This is also useful when you want a derived signal that implements the [SignalGet], [SignalWith], etc. traits.
pub struct DerivedRwSignal<
    T: 'static,
    O,
    GF: Fn(&T) -> O + Clone + 'static,
    UF: Fn(&O) -> T + 'static,
> {
    signal: RwSignal<T>,
    getter: RwSignal<Box<GF>>,
    setter: RwSignal<Box<UF>>,
    ty: PhantomData<T>,
}

impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Clone for DerivedRwSignal<T, O, GF, UF> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Copy for DerivedRwSignal<T, O, GF, UF> {}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalGet<O>
    for DerivedRwSignal<T, O, GF, UF>
{
    fn id(&self) -> crate::id::Id {
        self.signal.id
    }

    fn try_get(&self) -> Option<O>
    where
        O: 'static,
    {
        let sig = self.getter;
        SignalGet::id(self).signal().map(|signal| {
            let func = sig.get_untracked();
            func(&signal.get()).clone()
        })
    }

    fn try_get_untracked(&self) -> Option<O>
    where
        O: 'static,
    {
        let sig = self.getter;
        SignalGet::id(self).signal().map(|signal| {
            let func = sig.get_untracked();
            func(&signal.get_untracked()).clone()
        })
    }
}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalWith<O>
    for DerivedRwSignal<T, O, GF, UF>
{
    fn id(&self) -> crate::id::Id {
        self.signal.id
    }

    fn with<O2>(&self, f: impl FnOnce(&O) -> O2) -> O2
    where
        T: 'static,
    {
        let func = self.getter.get_untracked();
        SignalWith::id(self).signal().unwrap().with(|t| f(&func(t)))
    }

    fn with_untracked<O2>(&self, f: impl FnOnce(&O) -> O2) -> O2
    where
        T: 'static,
    {
        let func = self.getter.get_untracked();
        SignalWith::id(self)
            .signal()
            .unwrap()
            .with_untracked(|t| f(&func(t)))
    }

    fn try_with<O2>(&self, f: impl FnOnce(Option<&O>) -> O2) -> O2
    where
        T: 'static,
    {
        if let Some(signal) = SignalWith::id(self).signal() {
            let func = self.getter.get_untracked();
            signal.with(|v| f(Some(&func(v))))
        } else {
            f(None)
        }
    }

    fn try_with_untracked<O2>(&self, f: impl FnOnce(Option<&O>) -> O2) -> O2
    where
        T: 'static,
    {
        if let Some(signal) = SignalWith::id(self).signal() {
            let func = self.getter.get_untracked();
            signal.with_untracked(|v| f(Some(&func(v))))
        } else {
            f(None)
        }
    }
}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalTrack<O>
    for DerivedRwSignal<T, O, GF, UF>
{
    fn id(&self) -> crate::id::Id {
        self.signal.id
    }
    fn track(&self) {
        SignalWith::id(self).signal().unwrap().subscribe();
    }

    fn try_track(&self) {
        if let Some(signal) = SignalWith::id(self).signal() {
            signal.subscribe();
        }
    }
}

impl<T: 'static, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalUpdate<O>
    for DerivedRwSignal<T, O, GF, UF>
{
    fn id(&self) -> crate::id::Id {
        self.signal.id
    }

    fn set(&self, new_value: O)
    where
        O: 'static,
    {
        if let Some(signal) = self.id().signal() {
            let func = self.setter.get_untracked();
            signal.update_value::<_, T>(|v| {
                let new = func(&new_value);
                *v = new;
            });
        }
    }

    fn update(&self, f: impl FnOnce(&mut O))
    where
        O: 'static,
    {
        if let Some(signal) = self.id().signal() {
            let get_func = self.getter.get_untracked();
            let set_func = self.setter.get_untracked();
            signal.update_value::<_, T>(|cv| {
                let mut new = get_func(cv);
                f(&mut new);
                let new = set_func(&new);
                *cv = new;
            });
        }
    }

    fn try_update<O2>(&self, f: impl FnOnce(&mut O) -> O2) -> Option<O2>
    where
        O: 'static,
    {
        self.id().signal().map(|signal| {
            let get_func = self.getter.get_untracked();
            let set_func = self.setter.get_untracked();
            signal.update_value::<_, T>(|cv| {
                let mut new = get_func(cv);
                let ret = f(&mut new);
                let new = set_func(&new);
                *cv = new;
                ret
            })
        })
    }
}
impl<T, O, GF, UF> DerivedRwSignal<T, O, GF, UF>
where
    GF: Fn(&T) -> O + Clone + 'static,
    UF: Fn(&O) -> T + 'static,
{
    pub fn new(signal: RwSignal<T>, getter: GF, setter: UF) -> Self {
        let getter = RwSignal::new(Box::new(getter));
        let setter = RwSignal::new(Box::new(setter));
        DerivedRwSignal {
            signal,
            getter,
            setter,
            ty: PhantomData,
        }
    }
}

pub fn create_derived_rw_signal<T, O, GF, UF>(
    signal: RwSignal<T>,
    getter: GF,
    setter: UF,
) -> DerivedRwSignal<T, O, GF, UF>
where
    GF: Fn(&T) -> O + Clone + 'static,
    UF: Fn(&O) -> T + 'static,
{
    let getter = RwSignal::new(Box::new(getter));
    let setter = RwSignal::new(Box::new(setter));
    DerivedRwSignal {
        signal,
        getter,
        setter,
        ty: PhantomData,
    }
}