-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwrapper.spec.ts
1052 lines (905 loc) · 28.8 KB
/
wrapper.spec.ts
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright 2024 Bloomberg Finance L.P.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/* eslint-disable @typescript-eslint/no-this-alias */
import { afterEach, describe, expect, it, vi } from "vitest";
import { Signal } from './wrapper.js';
describe("Signal.State", () => {
it("should work", () => {
const stateSignal = new Signal.State(0);
expect(stateSignal.get()).toEqual(0);
stateSignal.set(10);
expect(stateSignal.get()).toEqual(10);
});
});
describe("Computed", () => {
it("should work", () => {
const stateSignal = new Signal.State(1);
const computedSignal = new Signal.Computed(() => {
const f = stateSignal.get() * 2;
return f;
});
expect(computedSignal.get()).toEqual(2);
stateSignal.set(5);
expect(stateSignal.get()).toEqual(5);
expect(computedSignal.get()).toEqual(10);
});
});
describe("Watcher", () => {
type Destructor = () => void;
const notifySpy = vi.fn();
const watcher = new Signal.subtle.Watcher(() => {
notifySpy();
});
function effect(cb: () => Destructor | void): () => void {
let destructor: Destructor | void;
const c = new Signal.Computed(() => (destructor = cb()));
watcher.watch(c);
c.get();
return () => {
destructor?.();
watcher.unwatch(c);
};
}
function flushPending() {
for (const signal of watcher.getPending()) {
signal.get();
}
expect(watcher.getPending()).toStrictEqual([]);
}
afterEach(() => watcher.unwatch(...Signal.subtle.introspectSources(watcher)));
it("should work", () => {
const watchedSpy = vi.fn();
const unwatchedSpy = vi.fn();
const stateSignal = new Signal.State(1, {
[Signal.subtle.watched]: watchedSpy,
[Signal.subtle.unwatched]: unwatchedSpy,
});
stateSignal.set(100);
stateSignal.set(5);
const computedSignal = new Signal.Computed(() => stateSignal.get() * 2);
let calls = 0;
let output = 0;
let computedOutput = 0;
// Ensure the call backs are not called yet
expect(watchedSpy).not.toHaveBeenCalled();
expect(unwatchedSpy).not.toHaveBeenCalled();
// Expect the watcher to not have any sources as nothing has been connected yet
expect(Signal.subtle.introspectSources(watcher)).toHaveLength(0);
expect(Signal.subtle.introspectSinks(computedSignal)).toHaveLength(0);
expect(Signal.subtle.introspectSinks(stateSignal)).toHaveLength(0);
expect(Signal.subtle.hasSinks(stateSignal)).toEqual(false);
const destructor = effect(() => {
output = stateSignal.get();
computedOutput = computedSignal.get();
calls++;
return () => {};
});
// The signal is now watched
expect(Signal.subtle.hasSinks(stateSignal)).toEqual(true);
// Now that the effect is created, there will be a source
expect(Signal.subtle.introspectSources(watcher)).toHaveLength(1);
expect(Signal.subtle.introspectSinks(computedSignal)).toHaveLength(1);
// Note: stateSignal has more sinks because one is for the computed signal and one is the effect.
expect(Signal.subtle.introspectSinks(stateSignal)).toHaveLength(2);
// Now the watched callback should be called
expect(watchedSpy).toHaveBeenCalled();
expect(unwatchedSpy).not.toHaveBeenCalled();
// It should not have notified yet
expect(notifySpy).not.toHaveBeenCalled();
stateSignal.set(10);
// After a signal has been set, it should notify
expect(notifySpy).toHaveBeenCalled();
// Initially, the effect should not have run
expect(calls).toEqual(1);
expect(output).toEqual(5);
expect(computedOutput).toEqual(10);
flushPending();
// The effect should run, and thus increment the value
expect(calls).toEqual(2);
expect(output).toEqual(10);
expect(computedOutput).toEqual(20);
// Kicking it off again, the effect should run again
watcher.watch();
stateSignal.set(20);
expect(watcher.getPending()).toHaveLength(1);
flushPending();
// After a signal has been set, it should notify again
expect(notifySpy).toHaveBeenCalledTimes(2);
expect(calls).toEqual(3);
expect(output).toEqual(20);
expect(computedOutput).toEqual(40);
Signal.subtle.untrack(() => {
// Untrack doesn't affect set, only get
stateSignal.set(999);
expect(calls).toEqual(3);
flushPending();
expect(calls).toEqual(4);
});
// Destroy and un-subscribe
destructor();
// Since now it is un-subscribed, it should now be called
expect(unwatchedSpy).toHaveBeenCalled();
// We can confirm that it is un-watched by checking it
expect(Signal.subtle.hasSinks(stateSignal)).toEqual(false);
// Since now it is un-subscribed, this should have no effect now
stateSignal.set(200);
flushPending();
// Make sure that effect is no longer running
// Everything should stay the same
expect(calls).toEqual(4);
expect(output).toEqual(999);
expect(computedOutput).toEqual(1998);
expect(watcher.getPending()).toHaveLength(0);
// Adding any other effect after an unwatch should work as expected
const destructor2 = effect(() => {
output = stateSignal.get();
return () => {};
});
stateSignal.set(300);
flushPending();
});
it("provides `this` to notify as normal function", () => {
const mockGetPending = vi.fn();
const watcher = new Signal.subtle.Watcher(function() {
this.getPending();
});
watcher.getPending = mockGetPending;
const signal = new Signal.State<number>(0);
watcher.watch(signal);
signal.set(1);
expect(mockGetPending).toBeCalled();
});
it("can be closed in if needed in notify as an arrow function", () => {
const mockGetPending = vi.fn();
const watcher = new Signal.subtle.Watcher(() => {
watcher.getPending();
});
watcher.getPending = mockGetPending;
const signal = new Signal.State<number>(0);
watcher.watch(signal);
signal.set(1);
expect(mockGetPending).toBeCalled();
});
});
describe("Expected class shape", () => {
it("should be on the prototype", () => {
expect(typeof Signal.State.prototype.get).toBe("function");
expect(typeof Signal.State.prototype.set).toBe("function");
expect(typeof Signal.Computed.prototype.get).toBe("function");
expect(typeof Signal.subtle.Watcher.prototype.watch).toBe("function");
expect(typeof Signal.subtle.Watcher.prototype.unwatch).toBe("function");
expect(typeof Signal.subtle.Watcher.prototype.getPending).toBe("function");
});
});
describe("Comparison semantics", () => {
it("should cache State by Object.is", () => {
const state = new Signal.State(NaN);
let calls = 0;
const computed = new Signal.Computed(() => {
calls++;
return state.get();
});
expect(calls).toBe(0);
expect(computed.get()).toBe(NaN);
expect(calls).toBe(1);
state.set(NaN);
expect(computed.get()).toBe(NaN);
expect(calls).toBe(1);
});
it("should track Computed by Object.is", () => {
const state = new Signal.State(1);
let value = 5;
let calls = 0;
const computed = new Signal.Computed(() => (state.get(), value));
const c2 = new Signal.Computed(() => (calls++, computed.get()));
expect(calls).toBe(0);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(2);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
value = NaN;
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(3);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
state.set(4);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
});
it("applies custom equality in State", () => {
let ecalls = 0;
const state = new Signal.State(1, {
equals() {
ecalls++;
return false;
},
});
let calls = 0;
const computed = new Signal.Computed(() => {
calls++;
return state.get();
});
expect(calls).toBe(0);
expect(ecalls).toBe(0);
expect(computed.get()).toBe(1);
expect(ecalls).toBe(0);
expect(calls).toBe(1);
state.set(1);
expect(computed.get()).toBe(1);
expect(ecalls).toBe(1);
expect(calls).toBe(2);
});
it("applies custom equality in Computed", () => {
const s = new Signal.State(5);
let ecalls = 0;
const c1 = new Signal.Computed(() => (s.get(), 1), {
equals() {
ecalls++;
return false;
},
});
let calls = 0;
const c2 = new Signal.Computed(() => {
calls++;
return c1.get();
});
expect(calls).toBe(0);
expect(ecalls).toBe(0);
expect(c2.get()).toBe(1);
expect(ecalls).toBe(0);
expect(calls).toBe(1);
s.set(10);
expect(c2.get()).toBe(1);
expect(ecalls).toBe(1);
expect(calls).toBe(2);
});
});
describe("Untrack", () => {
it("works", () => {
const state = new Signal.State(1);
const computed = new Signal.Computed(() =>
Signal.subtle.untrack(() => state.get()),
);
expect(computed.get()).toBe(1);
state.set(2);
expect(computed.get()).toBe(1);
});
it("works differently without untrack", () => {
const state = new Signal.State(1);
const computed = new Signal.Computed(() => state.get());
expect(computed.get()).toBe(1);
state.set(2);
expect(computed.get()).toBe(2);
});
});
describe("liveness", () => {
it("only changes on first and last descendant", () => {
const watchedSpy = vi.fn();
const unwatchedSpy = vi.fn();
const state = new Signal.State(1, {
[Signal.subtle.watched]: watchedSpy,
[Signal.subtle.unwatched]: unwatchedSpy,
});
const computed = new Signal.Computed(() => state.get());
computed.get();
expect(watchedSpy).not.toBeCalled();
expect(unwatchedSpy).not.toBeCalled();
const w = new Signal.subtle.Watcher(() => {});
const w2 = new Signal.subtle.Watcher(() => {});
w.watch(computed);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).not.toBeCalled();
w2.watch(computed);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).not.toBeCalled();
w2.unwatch(computed);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).not.toBeCalled();
w.unwatch(computed);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).toBeCalledTimes(1);
});
it("is tracked well on computed signals", () => {
const watchedSpy = vi.fn();
const unwatchedSpy = vi.fn();
const s = new Signal.State(1);
const c = new Signal.Computed(() => s.get(), {
[Signal.subtle.watched]: watchedSpy,
[Signal.subtle.unwatched]: unwatchedSpy,
});
c.get();
expect(watchedSpy).not.toBeCalled();
expect(unwatchedSpy).not.toBeCalled();
const w = new Signal.subtle.Watcher(() => {});
w.watch(c);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).not.toBeCalled();
w.unwatch(c);
expect(watchedSpy).toBeCalledTimes(1);
expect(unwatchedSpy).toBeCalledTimes(1);
});
});
describe("Errors", () => {
it("are cached by computed signals", () => {
const s = new Signal.State("first");
let n = 0;
const c = new Signal.Computed(() => {
n++;
throw s.get();
});
let n2 = 0;
const c2 = new Signal.Computed(() => {
n2++;
return c.get();
});
expect(n).toBe(0);
expect(() => c.get()).toThrowError("first");
expect(() => c2.get()).toThrowError("first");
expect(n).toBe(1);
expect(n2).toBe(1);
expect(() => c.get()).toThrowError("first");
expect(() => c2.get()).toThrowError("first");
expect(n).toBe(1);
expect(n2).toBe(1);
s.set("second");
expect(() => c.get()).toThrowError("second");
expect(() => c2.get()).toThrowError("second");
expect(n).toBe(2);
expect(n2).toBe(2);
// Doesn't retrigger on setting state to the same value
s.set("second");
expect(n).toBe(2);
});
it("are cached by computed signals when watched", () => {
const s = new Signal.State("first");
let n = 0;
const c = new Signal.Computed<unknown>(() => {
n++;
throw s.get();
});
const w = new Signal.subtle.Watcher(() => {});
w.watch(c);
expect(n).toBe(0);
expect(() => c.get()).toThrowError("first");
expect(n).toBe(1);
expect(() => c.get()).toThrowError("first");
expect(n).toBe(1);
s.set("second");
expect(() => c.get()).toThrowError("second");
expect(n).toBe(2);
s.set("second");
expect(n).toBe(2);
});
it("are cached by computed signals when equals throws", () => {
const s = new Signal.State(0);
const cSpy = vi.fn(() => s.get());
const c = new Signal.Computed(cSpy, {
equals() { throw new Error("equals"); },
});
c.get();
s.set(1);
// Error is cached; c throws again without needing to rerun.
expect(() => c.get()).toThrowError("equals");
expect(cSpy).toBeCalledTimes(2);
expect(() => c.get()).toThrowError("equals");
expect(cSpy).toBeCalledTimes(2);
})
});
describe("Cycles", () => {
it("detects trivial cycles", () => {
const c = new Signal.Computed(() => c.get());
expect(() => c.get()).toThrow();
});
it("detects slightly larger cycles", () => {
const c = new Signal.Computed(() => c2.get());
const c2 = new Signal.Computed(() => c.get());
const c3 = new Signal.Computed(() => c2.get());
expect(() => c3.get()).toThrow();
});
});
describe("Pruning", () => {
it("only recalculates until things are equal", () => {
const s = new Signal.State(0);
let n = 0;
const c = new Signal.Computed(() => (n++, s.get()));
let n2 = 0;
const c2 = new Signal.Computed(() => (n2++, c.get(), 5));
let n3 = 0;
const c3 = new Signal.Computed(() => (n3++, c2.get()));
expect(n).toBe(0);
expect(n2).toBe(0);
expect(n3).toBe(0);
expect(c3.get()).toBe(5);
expect(n).toBe(1);
expect(n2).toBe(1);
expect(n3).toBe(1);
s.set(1);
expect(n).toBe(1);
expect(n2).toBe(1);
expect(n3).toBe(1);
expect(c3.get()).toBe(5);
expect(n).toBe(2);
expect(n2).toBe(2);
expect(n3).toBe(1);
});
it("does similar pruning for live signals", () => {
const s = new Signal.State(0);
let n = 0;
const c = new Signal.Computed(() => (n++, s.get()));
let n2 = 0;
const c2 = new Signal.Computed(() => (n2++, c.get(), 5));
let n3 = 0;
const c3 = new Signal.Computed(() => (n3++, c2.get()));
const w = new Signal.subtle.Watcher(() => {});
w.watch(c3);
expect(n).toBe(0);
expect(n2).toBe(0);
expect(n3).toBe(0);
expect(c3.get()).toBe(5);
expect(n).toBe(1);
expect(n2).toBe(1);
expect(n3).toBe(1);
s.set(1);
expect(n).toBe(1);
expect(n2).toBe(1);
expect(n3).toBe(1);
expect(w.getPending().length).toBe(1);
expect(c3.get()).toBe(5);
expect(n).toBe(2);
expect(n2).toBe(2);
expect(n3).toBe(1);
expect(w.getPending().length).toBe(0);
});
});
describe("Prohibited contexts", () => {
it("allows writes during computed", () => {
const s = new Signal.State(1);
const c = new Signal.Computed(() => (s.set(s.get() + 1), s.get()));
expect(c.get()).toBe(2);
expect(s.get()).toBe(2);
// Note: c is marked clean in this case, even though re-evaluating it
// would cause it to change value (due to the set inside of it).
expect(c.get()).toBe(2);
expect(s.get()).toBe(2);
s.set(3);
expect(c.get()).toBe(4);
expect(s.get()).toBe(4);
});
it("disallows reads and writes during watcher notify", () => {
const s = new Signal.State(1);
const w = new Signal.subtle.Watcher(() => {
s.get();
});
w.watch(s);
expect(() => s.set(2)).toThrow();
w.unwatch(s);
expect(() => s.set(3)).not.toThrow();
const w2 = new Signal.subtle.Watcher(() => {
s.set(4);
});
w2.watch(s);
expect(() => s.set(5)).toThrow();
w2.unwatch(s);
expect(() => s.set(3)).not.toThrow();
});
});
describe("Custom equality", () => {
it("works for State", () => {
let answer = true;
const s = new Signal.State(1, {
equals() {
return answer;
},
});
let n = 0;
const c = new Signal.Computed(() => (n++, s.get()));
expect(c.get()).toBe(1);
expect(n).toBe(1);
s.set(2);
expect(s.get()).toBe(1);
expect(c.get()).toBe(1);
expect(n).toBe(1);
answer = false;
s.set(2);
expect(s.get()).toBe(2);
expect(c.get()).toBe(2);
expect(n).toBe(2);
s.set(2);
expect(s.get()).toBe(2);
expect(c.get()).toBe(2);
expect(n).toBe(3);
});
it("works for Computed", () => {
let answer = true;
let value = 1;
const u = new Signal.State(1);
const s = new Signal.Computed(() => (u.get(), value), {
equals() {
return answer;
},
});
let n = 0;
const c = new Signal.Computed(() => (n++, s.get()));
expect(c.get()).toBe(1);
expect(n).toBe(1);
u.set(2);
value = 2;
expect(s.get()).toBe(1);
expect(c.get()).toBe(1);
expect(n).toBe(1);
answer = false;
u.set(3);
expect(s.get()).toBe(2);
expect(c.get()).toBe(2);
expect(n).toBe(2);
u.set(4);
expect(s.get()).toBe(2);
expect(c.get()).toBe(2);
expect(n).toBe(3);
});
it("does not leak tracking information", () => {
const exact = new Signal.State(1);
const epsilon = new Signal.State(0.1);
const counter = new Signal.State(1);
const cutoff = vi.fn((a, b) => Math.abs(a - b) < epsilon.get());
const innerFn = vi.fn(() => exact.get());
const inner = new Signal.Computed(innerFn, {
equals: cutoff
});
const outerFn = vi.fn(() => {
counter.get();
return inner.get();
});
const outer = new Signal.Computed(outerFn);
outer.get();
// Everything runs the first time.
expect(innerFn).toBeCalledTimes(1);
expect(outerFn).toBeCalledTimes(1);
exact.set(2);
counter.set(2);
outer.get()
// `outer` reruns because `counter` changed, `inner` reruns when called by
// `outer`, and `cutoff` is called for the first time.
expect(innerFn).toBeCalledTimes(2);
expect(outerFn).toBeCalledTimes(2);
expect(cutoff).toBeCalledTimes(1);
epsilon.set(0.2);
outer.get();
// Changing something `cutoff` depends on makes `inner` need to rerun, but
// (since the new and old values are equal) not `outer`.
expect(innerFn).toBeCalledTimes(3);
expect(outerFn).toBeCalledTimes(2);
expect(cutoff).toBeCalledTimes(2);
});
});
describe("Receivers", () => {
it("is this for computed", () => {
let receiver;
const c = new Signal.Computed(function () {
receiver = this;
});
expect(c.get()).toBe(undefined);
expect(receiver).toBe(c);
});
it("is this for watched/unwatched", () => {
let r1, r2;
const s = new Signal.State(1, {
[Signal.subtle.watched]() {
r1 = this;
},
[Signal.subtle.unwatched]() {
r2 = this;
},
});
expect(r1).toBe(undefined);
expect(r2).toBe(undefined);
const w = new Signal.subtle.Watcher(() => {});
w.watch(s);
expect(r1).toBe(s);
expect(r2).toBe(undefined);
w.unwatch(s);
expect(r2).toBe(s);
});
it("is this for equals", () => {
let receiver;
const options = {
equals() {
receiver = this;
return false;
},
};
const s = new Signal.State(1, options);
s.set(2);
expect(receiver).toBe(s);
const c = new Signal.Computed(() => s.get(), options);
expect(c.get()).toBe(2);
s.set(4);
expect(c.get()).toBe(4);
expect(receiver).toBe(c);
});
});
describe("Dynamic dependencies", () => {
function run(live) {
const states = Array.from("abcdefgh").map((s) => new Signal.State(s));
const sources = new Signal.State(states);
const computed = new Signal.Computed(() => {
let str = "";
for (const state of sources.get()) str += state.get();
return str;
});
if (live) {
const w = new Signal.subtle.Watcher(() => {});
w.watch(computed);
}
expect(computed.get()).toBe("abcdefgh");
expect(Signal.subtle.introspectSources(computed).slice(1)).toStrictEqual(
states,
);
sources.set(states.slice(0, 5));
expect(computed.get()).toBe("abcde");
expect(Signal.subtle.introspectSources(computed).slice(1)).toStrictEqual(
states.slice(0, 5),
);
sources.set(states.slice(3));
expect(computed.get()).toBe("defgh");
expect(Signal.subtle.introspectSources(computed).slice(1)).toStrictEqual(
states.slice(3),
);
}
it("works live", () => run(true));
it("works not live", () => run(false));
});
describe("watch and unwatch", () => {
it("handles multiple watchers well", () => {
const s = new Signal.State(1);
const s2 = new Signal.State(2);
let n = 0;
const w = new Signal.subtle.Watcher(() => n++);
w.watch(s, s2);
s.set(4);
expect(n).toBe(1);
expect(w.getPending()).toStrictEqual([]);
w.watch();
s2.set(8);
expect(n).toBe(2);
w.unwatch(s);
s.set(3);
expect(n).toBe(2);
w.watch();
s2.set(3);
expect(n).toBe(3);
w.watch();
s.set(2);
expect(n).toBe(3);
});
it("understands dynamic dependency sets", () => {
let w1 = 0,
u1 = 0,
w2 = 0,
u2 = 0,
n = 0,
d = 0;
let s1 = new Signal.State(1, {
[Signal.subtle.watched]() {
w1++;
},
[Signal.subtle.unwatched]() {
u1++;
},
});
let s2 = new Signal.State(2, {
[Signal.subtle.watched]() {
w2++;
},
[Signal.subtle.unwatched]() {
u2++;
},
});
let which: { get(): number } = s1;
let c = new Signal.Computed(() => (d++, which.get()));
let w = new Signal.subtle.Watcher(() => n++);
w.watch(c);
expect(w1 + w2 + u1 + u2 + n + d).toBe(0);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);
expect(c.get()).toBe(1);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(0);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(1);
s1.set(3);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(1);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(1);
expect(c.get()).toBe(3);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(1);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(2);
which = s2;
w.watch();
s1.set(4);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(2);
expect(c.get()).toBe(2);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(3);
w.watch();
which = {
get() {
return 10;
},
};
s1.set(5);
expect(c.get()).toBe(2);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(3);
w.watch();
s2.set(0);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(3);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(3);
expect(c.get()).toBe(10);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(1);
expect(n).toBe(3);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(4);
});
});
describe("type checks", () => {
it("checks types in methods", () => {
let x = {};
let s = new Signal.State(1);
let c = new Signal.Computed(() => {});
let w = new Signal.subtle.Watcher(() => {});
expect(() => Signal.State.prototype.get.call(x)).toThrowError(TypeError);
expect(Signal.State.prototype.get.call(s)).toBe(1);
expect(() => Signal.State.prototype.get.call(c)).toThrowError(TypeError);
expect(() => Signal.State.prototype.get.call(w)).toThrowError(TypeError);
expect(() => Signal.State.prototype.set.call(x, 2)).toThrowError(TypeError);
expect(Signal.State.prototype.set.call(s, 2)).toBe(undefined);
expect(() => Signal.State.prototype.set.call(c, 2)).toThrowError(TypeError);
expect(() => Signal.State.prototype.set.call(w, 2)).toThrowError(TypeError);
expect(() => Signal.Computed.prototype.get.call(x)).toThrowError(TypeError);
expect(() => Signal.Computed.prototype.get.call(s)).toThrowError(TypeError);
expect(Signal.Computed.prototype.get.call(c)).toBe(undefined);
expect(() => Signal.Computed.prototype.get.call(w)).toThrowError(TypeError);
expect(() => Signal.subtle.Watcher.prototype.watch.call(x, s)).toThrowError(
TypeError,
);
expect(() => Signal.subtle.Watcher.prototype.watch.call(s, s)).toThrowError(
TypeError,
);
expect(() => Signal.subtle.Watcher.prototype.watch.call(c, s)).toThrowError(
TypeError,
);
expect(Signal.subtle.Watcher.prototype.watch.call(w, s)).toBe(undefined);
expect(() => Signal.subtle.Watcher.prototype.watch.call(w, w)).toThrowError(
TypeError,
);
expect(() =>
Signal.subtle.Watcher.prototype.unwatch.call(x, s),
).toThrowError(TypeError);
expect(() =>
Signal.subtle.Watcher.prototype.unwatch.call(s, s),
).toThrowError(TypeError);
expect(() =>
Signal.subtle.Watcher.prototype.unwatch.call(c, s),
).toThrowError(TypeError);
expect(Signal.subtle.Watcher.prototype.unwatch.call(w, s)).toBe(undefined);
expect(() =>
Signal.subtle.Watcher.prototype.unwatch.call(w, w),
).toThrowError(TypeError);
expect(() =>
Signal.subtle.Watcher.prototype.getPending.call(x, s),