sodium-rust 2.1.2

Sodium FRP (Functional Reactive Programming)
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
use crate::impl_::dep::Dep;
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_::lambda::{
    lambda1, lambda1_deps, lambda2, lambda2_deps, lambda3, lambda3_deps, lambda4_deps,
    lambda5_deps, lambda6_deps,
};
use crate::impl_::lazy::Lazy;
use crate::impl_::listener::Listener;
use crate::impl_::node::{IsNode, Node, WeakNode};
use crate::impl_::sodium_ctx::SodiumCtx;
use crate::impl_::sodium_ctx::SodiumCtxData;
use crate::impl_::stream::Stream;
use crate::impl_::stream::StreamWeakForwardRef;
use crate::impl_::stream::WeakStream;

use std::mem;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::sync::Weak;

pub struct CellWeakForwardRef<A> {
    data: Arc<RwLock<Option<WeakCell<A>>>>,
}

impl<A> Clone for CellWeakForwardRef<A> {
    fn clone(&self) -> Self {
        CellWeakForwardRef {
            data: self.data.clone(),
        }
    }
}

impl<A: Send + 'static> CellWeakForwardRef<A> {
    pub fn new() -> CellWeakForwardRef<A> {
        CellWeakForwardRef {
            data: Arc::new(RwLock::new(None)),
        }
    }

    pub fn assign(&self, c: &Cell<A>) {
        let mut x = self.data.write().unwrap();
        *x = Some(Cell::downgrade(c))
    }

    fn unwrap(&self) -> Cell<A> {
        let x = self.data.read().unwrap();
        (&*x).clone().unwrap().upgrade().unwrap()
    }
}

pub struct Cell<A> {
    pub data: Arc<Mutex<CellData<A>>>,
    pub node: Node,
}

pub struct WeakCell<A> {
    pub data: Weak<Mutex<CellData<A>>>,
    pub node: WeakNode,
}

impl<A> Clone for Cell<A> {
    fn clone(&self) -> Self {
        Cell {
            data: self.data.clone(),
            node: self.node.clone(),
        }
    }
}

impl<A> Clone for WeakCell<A> {
    fn clone(&self) -> Self {
        WeakCell {
            data: self.data.clone(),
            node: self.node.clone(),
        }
    }
}

pub struct CellData<A> {
    stream: Stream<A>,
    value: Lazy<A>,
    next_value_op: Option<A>,
}

impl<A> Cell<A> {
    pub fn with_data<R, K: FnOnce(&mut CellData<A>) -> R>(&self, k: K) -> R {
        let mut l = self.data.lock();
        let data: &mut CellData<A> = l.as_mut().unwrap();
        k(data)
    }

    pub fn sodium_ctx(&self) -> SodiumCtx {
        self.with_data(|data: &mut CellData<A>| data.stream.sodium_ctx())
    }

    pub fn to_dep(&self) -> Dep {
        Dep::new(self.node().gc_node.clone())
    }

    pub fn node(&self) -> &Node {
        &self.node
    }
}

impl<A: Send + 'static> Cell<A> {
    pub fn new(sodium_ctx: &SodiumCtx, value: A) -> Cell<A>
    where
        A: Clone,
    {
        let cell_data = Arc::new(Mutex::new(CellData {
            stream: Stream::new(sodium_ctx),
            value: Lazy::of_value(value),
            next_value_op: None,
        }));
        Cell {
            data: cell_data,
            node: Node::new(sodium_ctx, "Cell::new", || {}, vec![]),
        }
    }

    pub fn _new(sodium_ctx: &SodiumCtx, stream: Stream<A>, value: Lazy<A>) -> Cell<A>
    where
        A: Clone,
    {
        sodium_ctx.transaction(|| {
            let cell_data = Arc::new(Mutex::new(CellData {
                stream: stream.clone(),
                value,
                next_value_op: None,
            }));
            let sodium_ctx = sodium_ctx.clone();
            let node: Node;
            let c_forward_ref = CellWeakForwardRef::new();
            {
                let c = c_forward_ref.clone();
                let stream_node = stream.box_clone();
                let stream_dep = stream.to_dep();
                let sodium_ctx = sodium_ctx.clone();
                let sodium_ctx2 = sodium_ctx.clone();
                node = Node::new(
                    &sodium_ctx2,
                    "Cell::hold",
                    move || {
                        let c = c.unwrap();
                        let firing_op = stream.with_firing_op(|firing_op| firing_op.clone());
                        if let Some(firing) = firing_op {
                            let is_first = c.with_data(|data: &mut CellData<A>| {
                                let is_first = data.next_value_op.is_none();
                                data.next_value_op = Some(firing);
                                is_first
                            });
                            if is_first {
                                sodium_ctx.post(move || {
                                    c.with_data(|data: &mut CellData<A>| {
                                        let mut next_value_op: Option<A> = None;
                                        mem::swap(&mut next_value_op, &mut data.next_value_op);
                                        if let Some(next_value) = next_value_op {
                                            data.value = Lazy::of_value(next_value);
                                        }
                                    })
                                });
                            }
                        }
                    },
                    vec![stream_node],
                );
                // Hack: Add stream gc node twice, because one is kepted in the cell_data for Cell::update() to return.
                <dyn IsNode>::add_update_dependencies(&node, vec![stream_dep.clone(), stream_dep]);
            }
            let c = Cell {
                data: cell_data,
                node: node.clone(),
            };
            c_forward_ref.assign(&c);
            // initial update of Cell incase of stream (in Cell::hold) firing during same transaction cell is created.
            {
                let c = c.clone();
                sodium_ctx.pre_eot(move || {
                    let mut update = node.data.update.write().unwrap();
                    let update: &mut Box<_> = &mut *update;
                    update();
                    // c captured, but not used so that update() will not crash here
                    c.nop();
                });
            }
            //
            c
        })
    }

    pub fn nop(&self) {
        // no operation. (NOP)
        // Purpose is for capturing inside closure to extend lifetime to atleast the lifetime of the closure,
        // but do not use it in that closure
    }

    pub fn sample(&self) -> A
    where
        A: Clone,
    {
        self.with_data(|data: &mut CellData<A>| data.value.run())
    }

    pub fn sample_lazy(&self) -> Lazy<A> {
        self.with_data(|data: &mut CellData<A>| data.value.clone())
    }

    pub fn updates(&self) -> Stream<A> {
        self.with_data(|data: &mut CellData<A>| data.stream.clone())
    }

    pub fn value(&self) -> Stream<A>
    where
        A: Clone,
    {
        let sodium_ctx = self.sodium_ctx();
        sodium_ctx.transaction(|| {
            let s1 = self.updates();
            let spark: Stream<Lazy<A>> = Stream::new(&sodium_ctx);
            {
                let spark = spark.clone();
                let self_ = self.clone();
                spark._send(self_.with_data(|data: &mut CellData<A>| data.value.clone()));
                let node = spark.node();
                {
                    let mut changed = node.data.changed.write().unwrap();
                    *changed = true;
                }
                sodium_ctx.with_data(|data: &mut SodiumCtxData| {
                    data.changed_nodes.push(node.box_clone())
                });
            }
            s1.or_else(&spark.map(|x: &Lazy<A>| x.run()))
        })
    }

    pub fn map<B: Send + 'static, FN: IsLambda1<A, B> + Send + Sync + 'static>(
        &self,
        f: FN,
    ) -> Cell<B>
    where
        A: Clone,
        B: Clone,
    {
        let self_ = self.clone();
        let f_deps = lambda1_deps(&f);
        let f = Arc::new(Mutex::new(f));
        let init;
        {
            let f = f.clone();
            init = Lazy::new(move || {
                let mut l = f.lock();
                let f = l.as_mut().unwrap();
                f.call(&self_.sample())
            });
        }
        self.updates()
            .map(lambda1(
                move |a: &A| {
                    let mut l = f.lock();
                    let f = l.as_mut().unwrap();
                    f.call(a)
                },
                f_deps,
            ))
            .hold_lazy(init)
    }

    pub fn lift2<
        B: Send + Clone + 'static,
        C: Send + Clone + 'static,
        FN: IsLambda2<A, B, C> + Send + 'static,
    >(
        &self,
        cb: &Cell<B>,
        f: FN,
    ) -> Cell<C>
    where
        A: Clone,
    {
        let sodium_ctx = self.sodium_ctx();
        let lhs = self.sample_lazy();
        let rhs = cb.sample_lazy();
        let init: Lazy<C>;
        let f_deps = lambda2_deps(&f);
        let f = Arc::new(Mutex::new(f));
        {
            let lhs = lhs.clone();
            let rhs = rhs.clone();
            let f = f.clone();
            init = Lazy::new(move || {
                let mut l = f.lock();
                let f = l.as_mut().unwrap();
                f.call(&lhs.run(), &rhs.run())
            });
        }
        let state: Arc<Mutex<(Lazy<A>, Lazy<B>)>> = Arc::new(Mutex::new((lhs, rhs)));
        let s1: Stream<()>;
        let s2: Stream<()>;
        {
            let state = state.clone();
            s1 = self.updates().map(move |a: &A| {
                let mut l = state.lock();
                let state2: &mut (Lazy<A>, Lazy<B>) = l.as_mut().unwrap();
                state2.0 = Lazy::of_value(a.clone());
            });
        }
        {
            let state = state.clone();
            s2 = cb.updates().map(move |b: &B| {
                let mut l = state.lock();
                let state2: &mut (Lazy<A>, Lazy<B>) = l.as_mut().unwrap();
                state2.1 = Lazy::of_value(b.clone());
            });
        }
        let s = s1.or_else(&s2).map(lambda1(
            move |_: &()| {
                let l = state.lock();
                let state2: &(Lazy<A>, Lazy<B>) = l.as_ref().unwrap();
                let mut l = f.lock();
                let f = l.as_mut().unwrap();
                f.call(&state2.0.run(), &state2.1.run())
            },
            f_deps,
        ));
        Cell::_new(&sodium_ctx, s, init)
    }

    pub fn lift3<
        B: Send + Clone + 'static,
        C: Send + Clone + 'static,
        D: Send + Clone + 'static,
        FN: IsLambda3<A, B, C, D> + Send + 'static,
    >(
        &self,
        cb: &Cell<B>,
        cc: &Cell<C>,
        mut f: FN,
    ) -> Cell<D>
    where
        A: Clone,
    {
        let f_deps = lambda3_deps(&f);
        self.lift2(cb, |a: &A, b: &B| (a.clone(), b.clone())).lift2(
            cc,
            lambda2(
                move |(ref a, ref b): &(A, B), c: &C| f.call(a, b, c),
                f_deps,
            ),
        )
    }

    pub fn lift4<
        B: Send + Clone + 'static,
        C: Send + Clone + 'static,
        D: Send + Clone + 'static,
        E: Send + Clone + 'static,
        FN: IsLambda4<A, B, C, D, E> + Send + 'static,
    >(
        &self,
        cb: &Cell<B>,
        cc: &Cell<C>,
        cd: &Cell<D>,
        mut f: FN,
    ) -> Cell<E>
    where
        A: Clone,
    {
        let f_deps = lambda4_deps(&f);
        self.lift3(cb, cc, |a: &A, b: &B, c: &C| {
            (a.clone(), b.clone(), c.clone())
        })
        .lift2(
            cd,
            lambda2(
                move |(ref a, ref b, ref c): &(A, B, C), d: &D| f.call(a, b, c, d),
                f_deps,
            ),
        )
    }

    pub fn lift5<
        B: Send + Clone + 'static,
        C: Send + Clone + 'static,
        D: Send + Clone + 'static,
        E: Send + Clone + 'static,
        F: Send + Clone + 'static,
        FN: IsLambda5<A, B, C, D, E, F> + Send + 'static,
    >(
        &self,
        cb: &Cell<B>,
        cc: &Cell<C>,
        cd: &Cell<D>,
        ce: &Cell<E>,
        mut f: FN,
    ) -> Cell<F>
    where
        A: Clone,
    {
        let f_deps = lambda5_deps(&f);
        self.lift3(cb, cc, |a: &A, b: &B, c: &C| {
            (a.clone(), b.clone(), c.clone())
        })
        .lift3(
            cd,
            ce,
            lambda3(
                move |(ref a, ref b, ref c): &(A, B, C), d: &D, e: &E| f.call(a, b, c, d, e),
                f_deps,
            ),
        )
    }

    pub fn lift6<
        B: Send + Clone + 'static,
        C: Send + Clone + 'static,
        D: Send + Clone + 'static,
        E: Send + Clone + 'static,
        F: Send + Clone + 'static,
        G: Send + Clone + '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>,
        mut f: FN,
    ) -> Cell<G>
    where
        A: Clone,
    {
        let f_deps = lambda6_deps(&f);
        self.lift4(cb, cc, cd, |a: &A, b: &B, c: &C, d: &D| {
            (a.clone(), b.clone(), c.clone(), d.clone())
        })
        .lift3(
            ce,
            cf,
            lambda3(
                move |(ref a, ref b, ref c, ref d): &(A, B, C, D), e: &E, f2: &F| {
                    f.call(a, b, c, d, e, f2)
                },
                f_deps,
            ),
        )
    }

    pub fn switch_s(csa: &Cell<Stream<A>>) -> Stream<A>
    where
        A: Clone,
    {
        let csa = csa.clone();
        let sodium_ctx = csa.sodium_ctx();
        Stream::_new(&sodium_ctx, |sa: StreamWeakForwardRef<A>| {
            let inner_s: Arc<Mutex<WeakStream<A>>> =
                Arc::new(Mutex::new(Stream::downgrade(&Stream::new(&sodium_ctx))));
            let sa = sa;
            let node1: Node;
            {
                let inner_s = inner_s.clone();
                node1 = Node::new(
                    &sodium_ctx,
                    "switch_s inner node",
                    move || {
                        let l = inner_s.lock();
                        let inner_s: &WeakStream<A> = l.as_ref().unwrap();
                        let inner_s = inner_s.upgrade().unwrap();
                        inner_s.with_firing_op(|firing_op: &mut Option<A>| {
                            if let Some(ref firing) = firing_op {
                                let sa = sa.unwrap();
                                sa._send(firing.clone());
                            }
                        });
                    },
                    vec![],
                );
            }
            {
                let inner_s = inner_s.clone();
                let csa = csa.clone();
                let node1 = node1.clone();
                sodium_ctx.pre_eot(move || {
                    let mut l = inner_s.lock();
                    let inner_s: &mut WeakStream<A> = l.as_mut().unwrap();
                    let s = csa.sample();
                    *inner_s = Stream::downgrade(&s);
                    <dyn IsNode>::add_dependency(&node1, s);
                });
            }
            let node2: Node;
            let csa_updates = csa.updates();
            let csa_updates_node = csa_updates.box_clone();
            let csa_updates_dep = csa_updates.to_dep();
            {
                let node1: Node = node1.clone();
                let sodium_ctx = sodium_ctx.clone();
                let sodium_ctx2 = sodium_ctx.clone();
                node2 = Node::new(
                    &sodium_ctx2,
                    "switch_s outer node",
                    move || {
                        csa_updates.with_firing_op(|firing_op: &mut Option<Stream<A>>| {
                            if let Some(ref firing) = firing_op {
                                let firing = firing.clone();
                                let node1 = node1.clone();
                                let inner_s = inner_s.clone();
                                sodium_ctx.pre_post(move || {
                                    let mut l = inner_s.lock();
                                    let inner_s: &mut WeakStream<A> = l.as_mut().unwrap();
                                    <dyn IsNode>::remove_dependency(
                                        &node1,
                                        &inner_s.upgrade().unwrap(),
                                    );
                                    <dyn IsNode>::add_dependency(&node1, firing.clone());
                                    *inner_s = Stream::downgrade(&firing);
                                });
                            }
                        });
                    },
                    vec![csa_updates_node.box_clone()],
                );
            }
            <dyn IsNode>::add_update_dependencies(
                &node2,
                vec![csa_updates_dep, Dep::new(node1.gc_node().clone())],
            );
            <dyn IsNode>::add_dependency(&node1, node2);
            node1
        })
    }

    pub fn switch_c(cca: &Cell<Cell<A>>) -> Cell<A>
    where
        A: Clone,
    {
        let cca2 = cca.clone();
        let cca = cca.clone();
        let sodium_ctx = cca.sodium_ctx();
        Stream::_new(&sodium_ctx, |sa: StreamWeakForwardRef<A>| {
            let node1 = Node::new(
                &sodium_ctx,
                "switch_c outer node",
                || {},
                vec![cca.updates().box_clone()],
            );
            let last_inner_s = Arc::new(Mutex::new(Stream::downgrade(&Stream::new(&sodium_ctx))));
            let node2 = Node::new(
                &sodium_ctx,
                "switch_c inner node",
                || {},
                vec![node1.box_clone()],
            );
            {
                let last_inner_s = last_inner_s.clone();
                let cca = cca.clone();
                let node2 = node2.clone();
                sodium_ctx.pre_eot(move || {
                    let mut l = last_inner_s.lock();
                    let last_inner_s: &mut WeakStream<A> = l.as_mut().unwrap();
                    let s = cca.sample().updates();
                    *last_inner_s = Stream::downgrade(&s);
                    <dyn IsNode>::add_dependency(&node2, s);
                });
            }
            let node1_update;
            {
                let sodium_ctx = sodium_ctx.clone();
                let node1 = node1.clone();
                let node2 = node2.clone();
                let cca = cca.clone();
                let sa = sa.clone();
                let last_inner_s = last_inner_s.clone();
                node1_update = move || {
                    cca.updates()
                        .with_firing_op(|firing_op: &mut Option<Cell<A>>| {
                            if let Some(ref firing) = firing_op {
                                // will be overwriten by node2 firing if there is one
                                sodium_ctx.update_node(firing.updates().node());
                                let sa = sa.unwrap();
                                sa._send(firing.sample());
                                //
                                {
                                    let mut changed = node1.data.changed.write().unwrap();
                                    *changed = true;
                                }
                                {
                                    let mut changed = node2.data.changed.write().unwrap();
                                    *changed = true;
                                }
                                let new_inner_s = firing.updates();
                                new_inner_s.with_firing_op(|firing2_op: &mut Option<A>| {
                                    if let Some(ref firing2) = firing2_op {
                                        sa._send(firing2.clone());
                                    }
                                });
                                let mut l = last_inner_s.lock();
                                let last_inner_s: &mut WeakStream<A> = l.as_mut().unwrap();
                                <dyn IsNode>::remove_dependency(
                                    &node2,
                                    last_inner_s.upgrade().unwrap().node(),
                                );
                                <dyn IsNode>::add_dependency(&node2, new_inner_s.clone());
                                {
                                    let mut changed = node2.data.changed.write().unwrap();
                                    *changed = true;
                                }
                                *last_inner_s = Stream::downgrade(&new_inner_s);
                            }
                        });
                };
            }
            <dyn IsNode>::add_update_dependencies(
                &node1,
                vec![
                    Dep::new(node1.gc_node.clone()),
                    Dep::new(node2.gc_node.clone()),
                ],
            );
            {
                let mut update = node1.data.update.write().unwrap();
                *update = Box::new(node1_update);
            }
            {
                let last_inner_s = last_inner_s;
                let node2_update = move || {
                    let l = last_inner_s.lock();
                    let last_inner_s: &WeakStream<A> = l.as_ref().unwrap();
                    let last_inner_s = last_inner_s.upgrade().unwrap();
                    last_inner_s.with_firing_op(|firing_op: &mut Option<A>| {
                        if let Some(ref firing) = firing_op {
                            let sa = sa.unwrap();
                            sa._send(firing.clone());
                        }
                    });
                };
                {
                    let mut update = node2.data.update.write().unwrap();
                    *update = Box::new(node2_update);
                }
            }
            node2
        })
        .hold_lazy(Lazy::new(move || cca2.sample().sample()))
    }

    pub fn listen_weak<K: FnMut(&A) + Send + Sync + 'static>(&self, k: K) -> Listener
    where
        A: Clone,
    {
        self.sodium_ctx()
            .transaction(|| self.value().listen_weak(k))
    }

    pub fn listen<K: IsLambda1<A, ()> + Send + Sync + 'static>(&self, k: K) -> Listener
    where
        A: Clone,
    {
        self.sodium_ctx().transaction(|| self.value().listen(k))
    }

    pub fn downgrade(this: &Self) -> WeakCell<A> {
        WeakCell {
            data: Arc::downgrade(&this.data),
            node: Node::downgrade2(&this.node),
        }
    }
}

impl<A> WeakCell<A> {
    pub fn upgrade(&self) -> Option<Cell<A>> {
        let data_op = self.data.upgrade();
        let node_op = self.node.upgrade2();
        if data_op.is_none() || node_op.is_none() {
            return None;
        }
        let data = data_op.unwrap();
        let node = node_op.unwrap();
        Some(Cell { data, node })
    }
}