vertigo 0.13.1

Reactive Real-DOM library with SSR for Rust
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
use std::{
    cell::{Cell, RefCell},
    rc::Rc,
};

use super::{Computed, DropResource, Graph, Value};

#[test]
fn basic_sum() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(1);
    let b = g.value(2);
    let sum = g.computed({
        let a = a.clone();
        let b = b.clone();
        move |ctx| a.get(ctx) + b.get(ctx)
    });

    g.transaction(|ctx| {
        assert_eq!(sum.get(ctx), 3);
    });

    a.set(4);
    g.transaction(|ctx| {
        assert_eq!(sum.get(ctx), 6);
    });
    logs.assert_eq(&[]);
}

#[test]
fn two_sets_in_one_transaction_compute_once() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(0);
    let b = g.value(0);
    let runs = Rc::new(Cell::new(0));
    let sum = g.computed({
        let a = a.clone();
        let b = b.clone();
        let runs = runs.clone();
        move |ctx| {
            runs.set(runs.get() + 1);
            a.get(ctx) + b.get(ctx)
        }
    });

    g.transaction(|ctx| {
        assert_eq!(sum.get(ctx), 0);
    });
    runs.set(0);

    g.transaction(|_| {
        a.set(10);
        b.set(20);
    });

    g.transaction(|ctx| {
        assert_eq!(sum.get(ctx), 30);
    });
    assert_eq!(runs.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn cutoff_leaves_fanout_untouched() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(1);
    let even = g.computed({
        let a = a.clone();
        move |ctx| a.get(ctx) % 2 == 0
    });

    let child_runs = Rc::new(Cell::new(0));
    let children: Vec<_> = (0..10_000)
        .map(|i| {
            let even = even.clone();
            let child_runs = child_runs.clone();
            g.computed(move |ctx| {
                child_runs.set(child_runs.get() + 1);
                (even.get(ctx), i)
            })
        })
        .collect();

    g.transaction(|ctx| {
        assert!(!even.get(ctx));
        for child in &children {
            let _ = child.get(ctx);
        }
    });
    assert_eq!(child_runs.get(), 10_000);

    child_runs.set(0);
    a.set(3);
    g.transaction(|ctx| {
        assert!(!even.get(ctx));
    });
    assert_eq!(
        child_runs.get(),
        0,
        "odd→odd: even cut off, 10_000 must not run"
    );

    a.set(4);
    g.transaction(|ctx| {
        assert!(even.get(ctx));
        assert_eq!(children[0].get(ctx), (true, 0));
    });
    assert_eq!(
        child_runs.get(),
        10_000,
        "odd→even: fan-out must run once each"
    );
    logs.assert_eq(&[]);
}

#[test]
fn diamond_waits_for_both_parents() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(1);
    let left = g.computed({
        let a = a.clone();
        move |ctx| a.get(ctx) + 1
    });
    let right = g.computed({
        let a = a.clone();
        move |ctx| a.get(ctx) * 10
    });
    let d_runs = Rc::new(Cell::new(0));
    let d = g.computed({
        let left = left.clone();
        let right = right.clone();
        let d_runs = d_runs.clone();
        move |ctx| {
            d_runs.set(d_runs.get() + 1);
            left.get(ctx) + right.get(ctx)
        }
    });

    g.transaction(|ctx| {
        assert_eq!(d.get(ctx), 12);
    });
    d_runs.set(0);

    a.set(2);
    g.transaction(|ctx| {
        assert_eq!(left.get(ctx), 3);
        assert_eq!(right.get(ctx), 20);
        assert_eq!(d.get(ctx), 23);
    });
    assert_eq!(d_runs.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn subscribe_skips_when_parent_cuts_off() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(1);
    let even = g.computed({
        let a = a.clone();
        move |ctx| a.get(ctx) % 2 == 0
    });

    let fires = Rc::new(Cell::new(0));
    let last = Rc::new(Cell::new(false));
    let _sub: DropResource = even.subscribe({
        let fires = fires.clone();
        let last = last.clone();
        move |v| {
            fires.set(fires.get() + 1);
            last.set(v);
        }
    });

    assert_eq!(fires.get(), 1);
    assert!(!last.get());

    a.set(3);
    assert_eq!(fires.get(), 1);

    a.set(4);
    assert_eq!(fires.get(), 2);
    assert!(last.get());
    logs.assert_eq(&[]);
}

#[test]
fn equal_set_does_not_enqueue() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(5);
    let runs = Rc::new(Cell::new(0));
    let c = g.computed({
        let a = a.clone();
        let runs = runs.clone();
        move |ctx| {
            runs.set(runs.get() + 1);
            a.get(ctx)
        }
    });

    g.transaction(|ctx| {
        assert_eq!(c.get(ctx), 5);
    });
    runs.set(0);

    a.set(5);
    assert_eq!(runs.get(), 0);
    logs.assert_eq(&[]);
}

#[test]
fn default_graph_value_new() {
    let logs = super::default_graph().logger().listen();
    let a = Value::new(1);
    let double = Computed::from({
        let a = a.clone();
        move |ctx| a.get(ctx) * 2
    });

    super::transaction(|ctx| {
        assert_eq!(double.get(ctx), 2);
    });

    a.set(5);
    super::transaction(|ctx| {
        assert_eq!(double.get(ctx), 10);
    });
    logs.assert_eq(&[]);
}

#[test]
fn when_connect_tracks_watchers() {
    let logs = super::default_graph().logger().listen();
    let connect_count = Rc::new(Cell::new(0));
    let disconnect_count = Rc::new(Cell::new(0));

    let value = Value::new(1);
    let comp = value.to_computed().when_connect({
        let connect_count = connect_count.clone();
        let disconnect_count = disconnect_count.clone();
        move || {
            connect_count.set(connect_count.get() + 1);
            DropResource::new({
                let disconnect_count = disconnect_count.clone();
                move || {
                    disconnect_count.set(disconnect_count.get() + 1);
                }
            })
        }
    });

    assert_eq!(connect_count.get(), 0);
    assert_eq!(disconnect_count.get(), 0);

    let drop_resource = comp.subscribe(|_| {});

    assert_eq!(connect_count.get(), 1);
    assert_eq!(disconnect_count.get(), 0);

    drop(drop_resource);

    assert_eq!(connect_count.get(), 1);
    assert_eq!(disconnect_count.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn when_connect_multiple_subscribers_share_one_connection() {
    let logs = super::default_graph().logger().listen();
    let connect_count = Rc::new(Cell::new(0));
    let disconnect_count = Rc::new(Cell::new(0));

    let value = Value::new(1);
    let comp = value.to_computed().when_connect({
        let connect_count = connect_count.clone();
        let disconnect_count = disconnect_count.clone();
        move || {
            connect_count.set(connect_count.get() + 1);
            DropResource::new({
                let disconnect_count = disconnect_count.clone();
                move || {
                    disconnect_count.set(disconnect_count.get() + 1);
                }
            })
        }
    });

    let drop1 = comp.clone().subscribe(|_| {});
    assert_eq!(connect_count.get(), 1);

    let drop2 = comp.subscribe(|_| {});
    assert_eq!(connect_count.get(), 1);

    drop(drop1);
    assert_eq!(disconnect_count.get(), 0);

    drop(drop2);
    assert_eq!(disconnect_count.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn when_connect_disconnects_while_computed_lives() {
    let logs = super::default_graph().logger().listen();
    let connect = Rc::new(Cell::new(0));
    let disconnect = Rc::new(Cell::new(0));
    let value = Value::new(1);
    let comp = value.to_computed().when_connect({
        let connect = connect.clone();
        let disconnect = disconnect.clone();
        move || {
            connect.set(1);
            DropResource::new({
                let disconnect = disconnect.clone();
                move || disconnect.set(1)
            })
        }
    });
    let keep = comp.clone();
    let sub = comp.subscribe(|_| {});
    assert_eq!(connect.get(), 1);
    drop(sub);
    assert_eq!(disconnect.get(), 1);
    drop(keep);
    logs.assert_eq(&[]);
}

#[test]
fn when_connect_runs_after_on_after_transaction() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let order = Rc::new(RefCell::new(Vec::new()));
    let _hook = g.on_after_transaction({
        let order = order.clone();
        move || order.borrow_mut().push("hook")
    });
    let value = g.value(1);
    let comp = value.to_computed().when_connect({
        let order = order.clone();
        move || {
            order.borrow_mut().push("connect");
            DropResource::new(|| {})
        }
    });
    let _sub = comp.subscribe({
        let order = order.clone();
        move |_| order.borrow_mut().push("subscribe")
    });
    assert_eq!(*order.borrow(), ["subscribe", "hook", "connect"]);
    logs.assert_eq(&[]);
}

/// A `when_connect` closure may write, and that write runs a whole wave before the
/// closure returns. If the wave takes the last child away from the node that is
/// connecting, the resource it returns must be dropped - the node is no longer watched,
/// so nothing is left to keep the external work alive.
#[test]
fn connect_that_unwatches_itself_disconnects() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let connects = Rc::new(Cell::new(0));
    let disconnects = Rc::new(Cell::new(0));

    let flag = g.value(true);
    let source = g.value(1);

    let connected = source.to_computed().when_connect({
        let connects = connects.clone();
        let disconnects = disconnects.clone();
        let flag = flag.clone();
        move || {
            connects.set(connects.get() + 1);
            // From `create` after the graph has settled: costs `connected` its only child.
            flag.set(false);
            DropResource::new({
                let disconnects = disconnects.clone();
                move || disconnects.set(disconnects.get() + 1)
            })
        }
    });

    let reader = g.computed({
        let flag = flag.clone();
        let connected = connected.clone();
        move |ctx| {
            if flag.get(ctx) { connected.get(ctx) } else { 0 }
        }
    });
    let _sub = reader.subscribe(|_| {});

    assert_eq!(connects.get(), 1);
    assert_eq!(
        disconnects.get(),
        1,
        "unwatched, so it must not stay connected"
    );

    // And it stays disconnected: no later wave revives it.
    source.set(2);
    assert_eq!(connects.get(), 1);
    assert_eq!(disconnects.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn watch_and_unwatch_in_one_transaction_does_not_connect() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let connects = Rc::new(Cell::new(0));
    let disconnects = Rc::new(Cell::new(0));
    let value = g.value(1);
    let comp = value.to_computed().when_connect({
        let connects = connects.clone();
        let disconnects = disconnects.clone();
        move || {
            connects.set(connects.get() + 1);
            DropResource::new({
                let disconnects = disconnects.clone();
                move || disconnects.set(disconnects.get() + 1)
            })
        }
    });
    g.transaction(|_| {
        let sub = comp.subscribe(|_| {});
        drop(sub);
    });
    assert_eq!(connects.get(), 0);
    assert_eq!(disconnects.get(), 0);
    logs.assert_eq(&[]);
}

#[test]
fn nested_computed_subscription() {
    let logs = super::default_graph().logger().listen();
    let token_value = Value::new("token1".to_string());
    let token_computed = token_value.to_computed();

    let bearer_auth = Computed::from({
        let token_computed = token_computed.clone();
        move |_ctx| Some(token_computed.clone())
    });

    let counter = Rc::new(Cell::new(0));

    let revalidate_trigger = Computed::from({
        let bearer_auth = bearer_auth.clone();
        move |ctx| bearer_auth.get(ctx).map(|c| c.get(ctx))
    });

    let _drop = revalidate_trigger.subscribe({
        let counter = counter.clone();
        move |_| {
            counter.set(counter.get() + 1);
        }
    });

    assert_eq!(counter.get(), 1);

    token_value.set("token2".to_string());
    assert_eq!(counter.get(), 2);
    logs.assert_eq(&[]);
}

#[test]
fn nested_computed_subscription_no_flattening() {
    let logs = super::default_graph().logger().listen();
    let token_value = Value::new("token1".to_string());
    let token_computed = token_value.to_computed();

    let bearer_auth = Computed::from({
        let token_computed = token_computed.clone();
        move |_ctx| Some(token_computed.clone())
    });

    let counter = Rc::new(Cell::new(0));

    let _drop = bearer_auth.subscribe({
        let counter = counter.clone();
        move |_| {
            counter.set(counter.get() + 1);
        }
    });

    assert_eq!(counter.get(), 1);

    token_value.set("token2".to_string());
    assert_eq!(counter.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn on_after_transaction_fires_after_set() {
    let logs = super::default_graph().logger().listen();
    let fires = Rc::new(Cell::new(0));
    let _hook = super::on_after_transaction({
        let fires = fires.clone();
        move || {
            fires.set(fires.get() + 1);
        }
    });

    let a = Value::new(1);
    a.set(2);
    assert_eq!(fires.get(), 1);
    logs.assert_eq(&[]);
}

#[test]
fn set_from_subscribe_is_ignored() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(0);
    let b = g.value(0);
    let _sub = a.to_computed().subscribe({
        let b = b.clone();
        move |_| b.set(1)
    });
    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);

    g.transaction(|ctx| {
        assert_eq!(b.get(ctx), 0);
    });
    logs.assert_eq(&[]);

    a.set(2);
    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);

    g.transaction(|ctx| {
        assert_eq!(b.get(ctx), 0);
    });
    logs.assert_eq(&[]);
}

#[test]
fn set_from_compute_is_ignored() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(0);
    let b = g.value(0);
    let c = g.computed({
        let a = a.clone();
        let b = b.clone();
        move |ctx| {
            b.set(a.get(ctx));
            a.get(ctx)
        }
    });
    logs.assert_eq(&[]);

    g.transaction(|ctx| {
        assert_eq!(c.get(ctx), 0);
        assert_eq!(b.get(ctx), 0);
    });
    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);
    logs.assert_eq(&[]);
}

/// `when_connect` runs after the wave, so a write from `create` is a new transaction
/// and must reach the subscriber too, not just land in the value.
#[test]
fn write_from_when_connect_reaches_the_subscriber() {
    let g = Graph::new();
    let logs = g.logger().listen();

    let source = g.value(0);
    let observed = g
        .computed({
            let source = source.clone();
            move |ctx| source.get(ctx)
        })
        .when_connect({
            let source = source.clone();
            move || {
                source.set(7);
                DropResource::new(|| {})
            }
        });

    let seen = Rc::new(RefCell::new(Vec::new()));
    let _sub = observed.subscribe({
        let seen = seen.clone();
        move |value| seen.borrow_mut().push(value)
    });

    assert_eq!(*seen.borrow(), vec![0, 7]);
    logs.assert_eq(&[]);
}

/// Disconnect must not write. Connect takes the node's last child away; dropping the
/// resource tries to give it back and is ignored, so the node stays disconnected.
#[test]
fn disconnect_must_not_write() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let connects = Rc::new(Cell::new(0));
    let flag = g.value(true);
    let source = g.value(1);

    let connected = source.to_computed().when_connect({
        let connects = connects.clone();
        let flag = flag.clone();
        move || {
            connects.set(connects.get() + 1);
            flag.set(false);
            DropResource::new({
                let flag = flag.clone();
                move || flag.set(true)
            })
        }
    });

    let reader = g.computed({
        let flag = flag.clone();
        let connected = connected.clone();
        move |ctx| {
            if flag.get(ctx) { connected.get(ctx) } else { 0 }
        }
    });
    let _sub = reader.subscribe(|_| {});

    assert_eq!(connects.get(), 1);
    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);
    g.transaction(|ctx| assert!(!flag.get(ctx)));
}

/// Dropping something from inside a subscribe callback - a component going away while the
/// view is rebuilt - runs that `Drop` inside the callback, so its writes are refused like
/// any other write from there.
#[test]
fn a_write_from_a_drop_inside_a_callback_is_ignored() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let cleared = g.value(false);
    let source = g.value(0);

    let resource = Rc::new(RefCell::new(Some(DropResource::new({
        let cleared = cleared.clone();
        move || cleared.set(true)
    }))));

    let _sub = source.to_computed().subscribe({
        let resource = resource.clone();
        move |value| {
            if value == 1 {
                resource.borrow_mut().take();
            }
        }
    });
    logs.assert_eq(&[]);

    source.set(1);

    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);
    assert!(!g.transaction(|ctx| cleared.get(ctx)));
}

#[test]
fn set_from_drop_resource_is_ignored() {
    let g = Graph::new();
    let logs = g.logger().listen();
    let a = g.value(0);
    let resource = DropResource::new({
        let a = a.clone();
        move || a.set(1)
    });
    drop(resource);
    logs.assert_eq(&[super::graph::BLOCKED_WRITE]);
    g.transaction(|ctx| assert_eq!(a.get(ctx), 0));
}

/// A compute closure that reads its own value closes a cycle. It is caught by the read,
/// while the path is still on the refresh stack, so the panic can name it.
#[test]
#[should_panic(expected = "NodeId(2) -> NodeId(2)")]
fn a_computed_reading_itself_panics() {
    let g = Graph::new();
    let source = g.value(1i32);
    let holder: Rc<RefCell<Option<Computed<i32>>>> = Rc::new(RefCell::new(None));

    let c = g.computed({
        let source = source.clone();
        let holder = holder.clone();
        move |ctx| {
            let base = source.get(ctx);
            // The first run must not close the cycle - a `Computed` that reads itself
            // before it ever had a value just recurses in `ensure`.
            if base < 5 {
                return base;
            }
            match holder.borrow().clone() {
                Some(myself) => base + myself.get(ctx),
                None => base,
            }
        }
    });
    *holder.borrow_mut() = Some(c.clone());
    let _sub = c.subscribe(|_| {});

    source.set(5);
}

/// Two computeds that start reading each other mid-wave. The path names both.
#[test]
#[should_panic(expected = "cycle in the reactive graph")]
fn two_computeds_reading_each_other_panic() {
    let g = Graph::new();
    let flag = g.value(false);
    let hold_a: Rc<RefCell<Option<Computed<i32>>>> = Rc::new(RefCell::new(None));
    let hold_b: Rc<RefCell<Option<Computed<i32>>>> = Rc::new(RefCell::new(None));

    let a = g.computed({
        let flag = flag.clone();
        let hold_b = hold_b.clone();
        move |ctx| match (flag.get(ctx), hold_b.borrow().clone()) {
            (true, Some(other)) => other.get(ctx) + 1,
            _ => 1,
        }
    });
    let b = g.computed({
        let flag = flag.clone();
        let hold_a = hold_a.clone();
        move |ctx| match (flag.get(ctx), hold_a.borrow().clone()) {
            (true, Some(other)) => other.get(ctx) + 1,
            _ => 2,
        }
    });
    *hold_a.borrow_mut() = Some(a.clone());
    *hold_b.borrow_mut() = Some(b.clone());
    let _sa = a.subscribe(|_| {});
    let _sb = b.subscribe(|_| {});

    flag.set(true);
}

/// Two `when_connect` closures that write can hand the connection back and forth: the
/// first takes its own reader away and gives it to the second, and the second does the
/// reverse. Neither write is illegal - `create` is a legal place to write - and there is
/// no state this settles in, so the flush has to end it.
///
/// One connect per node per flush is what ends it. `second` connects, its write hands the
/// reader back to `first`, and `first` has already had its turn in this flush, so it is
/// left disconnected and the graph says which node that was. Which of the two loses
/// depends on the order a round is processed in, and that order is by node id, so the
/// outcome is the same on every run.
///
/// The counter is a safety valve, not the thing under test: past it the closures stop
/// writing, so a graph that cannot end the loop fails an assert instead of hanging.
#[test]
fn two_connects_that_hand_the_connection_back_and_forth_are_cut() {
    const VALVE: u32 = 1000;

    let g = Graph::new();
    let logs = g.logger().listen();
    let connects = Rc::new(Cell::new(0));
    let flag = g.value(true);
    let source = g.value(1);

    let connect = |wants: bool| {
        let connects = connects.clone();
        let flag = flag.clone();
        move || {
            connects.set(connects.get() + 1);
            if connects.get() < VALVE {
                flag.set(wants);
            }
            DropResource::new(|| {})
        }
    };

    let first = source.to_computed().when_connect(connect(false));
    let second = source.to_computed().when_connect(connect(true));

    let reader = g.computed({
        let flag = flag.clone();
        let first = first.clone();
        let second = second.clone();
        move |ctx| {
            if flag.get(ctx) {
                first.get(ctx)
            } else {
                second.get(ctx)
            }
        }
    });
    let _sub = reader.subscribe(|_| {});

    assert!(
        connects.get() < VALVE,
        "connected {} times - the flush only stopped because the test refused to keep writing",
        connects.get()
    );
    assert_eq!(
        connects.get(),
        2,
        "two nodes, one connect each - a flush connects a node once"
    );

    let messages = logs.take();
    assert_eq!(messages.len(), 1, "{messages:?}");
    assert!(
        messages[0].starts_with(super::graph::CONNECT_LOOP),
        "{messages:?}"
    );
}