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
use crate::impl_::cell::Cell as CellImpl;
use crate::impl_::lambda::IsLambda1;
use crate::impl_::lambda::IsLambda2;
use crate::impl_::lambda::IsLambda3;
use crate::impl_::lambda::IsLambda4;
use crate::impl_::lambda::IsLambda5;
use crate::impl_::lambda::IsLambda6;
use crate::impl_::lazy::Lazy;
use crate::listener::Listener;
use crate::sodium_ctx::SodiumCtx;
use crate::stream::Stream;
use crate::Dep;
/// Represents a value of type `A` that changes over time.
///
/// In other Functional Reactive Programming (FRP) systems this is
/// also called a _behavior_, _property_, or a _signal_. A `Cell`
/// should be used for modeling any pieces of mutable state in an FRP
/// application.
pub struct Cell<A> {
pub impl_: CellImpl<A>,
}
impl<A> Clone for Cell<A> {
fn clone(&self) -> Self {
Cell {
impl_: self.impl_.clone(),
}
}
}
impl<A: Clone + Send + 'static> Cell<A> {
/// Create a `Cell` with a constant value.
pub fn new(sodium_ctx: &SodiumCtx, value: A) -> Cell<A> {
Cell {
impl_: CellImpl::new(&sodium_ctx.impl_, value),
}
}
/// Sample the `Cell`'s current value.
///
/// `Cell::sample` may be used in the functions passed to
/// primitives that apply them to [`Stream`]s, including
/// [`Stream::map`], [`Stream::snapshot`], [`Stream::filter`], and
/// [`Stream::merge`].
///
/// When called within a function passed to [`Stream::map`] using
/// `sample` is equivalent to [snapshotting][Stream::snapshot]
/// this `Cell` with that [`Stream`].
pub fn sample(&self) -> A {
self.impl_.sample()
}
/// Sample the `Cell`'s current value lazily.
///
/// When it is necessary to use `sample` while implementing more
/// general abstractions, `sample_lazy` should be preferred in
/// case a [`CellLoop`][crate::CellLoop] is passed rather than a
/// `Cell`.
///
/// See [`Cell::sample`] for more details.
pub fn sample_lazy(&self) -> Lazy<A> {
self.impl_.sample_lazy()
}
// use as dependency to lambda1, lambda2, etc.
#[doc(hidden)]
pub fn to_dep(&self) -> Dep {
self.impl_.to_dep()
}
/// Return a [`Stream`] that gives the updates/steps for a `Cell`.
///
/// ## Important
///
/// This is an operational primitive, which isn't part of the main
/// Sodium API. It breaks the property of non-detectability of
/// cell updates/steps. The rule with this primitive is that you
/// should only use it in functions that don't allow the caller to
/// detect the `Cell` updates.
pub fn updates(&self) -> Stream<A> {
Stream {
impl_: self.impl_.updates(),
}
}
/// Return a [`Stream`] that is guaranteed to fire at least once.
///
/// When `value` is called, the returned `Stream` will fire once
/// in the current transaction with the current value of this
/// `Cell` and thereafter behaves like [`Cell::updates`].
///
/// ## Important
///
/// This is an operational primitive, which isn't part of the main
/// Sodium API. It breaks the property of non-detectability of
/// cell updates/steps. The rule with this primitive is that you
/// should only use it in functions that don't allow the caller to
/// detect the `Cell` updates.
pub fn value(&self) -> Stream<A> {
Stream {
impl_: self.impl_.value(),
}
}
/// Transform the `Cell`s value with the supplied function.
///
/// The returned `Cell` always reflects the value produced by the
/// function applied to the input `Cell`s value. The given
/// function _must_ be referentially transparent.
pub fn map<B: Clone + Send + 'static, FN: IsLambda1<A, B> + Send + Sync + 'static>(
&self,
f: FN,
) -> Cell<B> {
Cell {
impl_: self.impl_.map(f),
}
}
/// Lift a binary function into cells so the returned [`Cell`]
/// always reflects the specified function applied to the input
/// cells' values.
pub fn lift2<
B: Clone + Send + 'static,
C: Clone + Send + 'static,
FN: IsLambda2<A, B, C> + Send + 'static,
>(
&self,
cb: &Cell<B>,
f: FN,
) -> Cell<C> {
Cell {
impl_: self.impl_.lift2(&cb.impl_, f),
}
}
/// Lift a ternary function into cells so the returned [`Cell`]
/// always reflects the specified function applied to the input
/// cells' values.
pub fn lift3<
B: Clone + Send + 'static,
C: Clone + Send + 'static,
D: Clone + Send + 'static,
FN: IsLambda3<A, B, C, D> + Send + 'static,
>(
&self,
cb: &Cell<B>,
cc: &Cell<C>,
f: FN,
) -> Cell<D> {
Cell {
impl_: self.impl_.lift3(&cb.impl_, &cc.impl_, f),
}
}
/// Lift a quaternary function into cells so the returned [`Cell`]
/// always reflects the specified function applied to the input
/// cells' values.
pub fn lift4<
B: Clone + Send + 'static,
C: Clone + Send + 'static,
D: Clone + Send + 'static,
E: Clone + Send + 'static,
FN: IsLambda4<A, B, C, D, E> + Send + 'static,
>(
&self,
cb: &Cell<B>,
cc: &Cell<C>,
cd: &Cell<D>,
f: FN,
) -> Cell<E> {
Cell {
impl_: self.impl_.lift4(&cb.impl_, &cc.impl_, &cd.impl_, f),
}
}
/// Lift a five-argument function into cells so the returned
/// [`Cell`] always reflects the specified function applied to the
/// input cells' values.
pub fn lift5<
B: Clone + Send + 'static,
C: Clone + Send + 'static,
D: Clone + Send + 'static,
E: Clone + Send + 'static,
F: Clone + Send + 'static,
FN: IsLambda5<A, B, C, D, E, F> + Send + 'static,
>(
&self,
cb: &Cell<B>,
cc: &Cell<C>,
cd: &Cell<D>,
ce: &Cell<E>,
f: FN,
) -> Cell<F> {
Cell {
impl_: self
.impl_
.lift5(&cb.impl_, &cc.impl_, &cd.impl_, &ce.impl_, f),
}
}
/// Lift a six argument function into cells so the returned
/// [`Cell`] always reflects the specified function applied to the
/// input cells' values.
pub fn lift6<
B: Clone + Send + 'static,
C: Clone + Send + 'static,
D: Clone + Send + 'static,
E: Clone + Send + 'static,
F: Clone + Send + 'static,
G: Clone + Send + 'static,
FN: IsLambda6<A, B, C, D, E, F, G> + Send + 'static,
>(
&self,
cb: &Cell<B>,
cc: &Cell<C>,
cd: &Cell<D>,
ce: &Cell<E>,
cf: &Cell<F>,
f: FN,
) -> Cell<G> {
Cell {
impl_: self
.impl_
.lift6(&cb.impl_, &cc.impl_, &cd.impl_, &ce.impl_, &cf.impl_, f),
}
}
/// Unwrap a [`Stream`] in a `Cell` to give a time-varying stream implementation.
pub fn switch_s(csa: &Cell<Stream<A>>) -> Stream<A> {
Stream {
impl_: CellImpl::switch_s(&csa.map(|sa: &Stream<A>| sa.impl_.clone()).impl_),
}
}
/// Unwrap a `Cell` in another `Cell` to give a time-varying cell implementation.
pub fn switch_c(cca: &Cell<Cell<A>>) -> Cell<A> {
Cell {
impl_: CellImpl::switch_c(&cca.map(|ca: &Cell<A>| ca.impl_.clone()).impl_),
}
}
/// A variant of [`listen`][Cell::listen] that will deregister the
/// listener automatically if the listener is garbage-collected.
pub fn listen_weak<K: FnMut(&A) + Send + Sync + 'static>(&self, k: K) -> Listener {
Listener {
impl_: self.impl_.listen_weak(k),
}
}
/// Listen for updates to the value of this `Cell`.
///
/// This is the observer pattern. The returned [`Listener`] has an
/// [`unlisten`][Listener::unlisten] method to cause the listener
/// to be removed.
///
/// This is an operational mechanism for interfacing between the
/// world of I/O and FRP.
pub fn listen<K: IsLambda1<A, ()> + Send + Sync + 'static>(&self, k: K) -> Listener {
Listener {
impl_: self.impl_.listen(k),
}
}
}