-
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
import { heapStats } from "bun:jsc";
import { describe, expect, mock, test } from "bun:test";
import { derived, effect, signal } from "../src/signals.js";
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
describe("signal", () => {
test("should be able to return containing value via `get`", () => {
const $s = signal(1);
expect($s.get()).toBe(1);
});
test("`.value` alias should work", () => {
const $s = signal(1);
expect($s.value).toBe(1);
$s.value = 2;
expect($s.get()).toBe(2);
});
test("should update containing value via `set`", () => {
const $s = signal(1);
$s.set(2);
expect($s.get()).toBe(2);
});
test("should skip updates when the value is same", () => {
const fn = mock(() => {});
const $s = signal(1);
effect(() => {
$s.get();
fn();
});
$s.set(1);
$s.set(1);
expect(fn).toHaveBeenCalledTimes(1);
});
test("`once` should not trigger re-run", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
effect(() => {
const $c = derived(() => $s.once());
fn($c.get());
});
$s.set(2);
expect(fn.mock.calls).toEqual([[1]]);
});
test("`update` pass the current value as an argument", () => {
const $s = signal(1);
$s.update((n) => n + 1);
expect($s.once()).toBe(2);
});
test("should stop updating once disposed", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
effect(() => {
fn($s.get());
});
$s.dispose();
$s.set(2);
expect(fn.mock.calls).toEqual([[1]]);
});
test("should be able to manually specify which Computation to associate", async () => {
const fn = mock((_n: number) => {});
const $s = signal(0);
effect((ctx) => {
sleep(5).then(() => {
// At the time this line, the Effect (ctx) is popped from global stack.
fn($s.get(ctx));
});
});
$s.set(1);
await sleep(6);
$s.set(2);
await sleep(6);
expect(fn.mock.calls).toEqual([[1], [2]]);
});
test("should work with immediate multiple writes", () => {
const $s = signal(0);
$s.set(1);
$s.set(2);
$s.set(3);
expect($s.once()).toBe(3);
$s.set(2);
$s.set(1);
$s.set(0);
expect($s.once()).toBe(0);
});
});
describe("derived", () => {
test("should create a derived signal based on another signal", () => {
const $s = signal(1);
const $c = derived(() => $s.get() + 1);
expect($c.get()).toBe(2);
});
test("`.value` alias should work", () => {
const $s = signal(1);
const $c = derived(() => $s.get() + 1);
expect($c.value).toBe(2);
});
test("should be updated when dependent signal was updated", () => {
const $s = signal(1);
const $c = derived(() => $s.get() + 1);
expect($c.get()).toBe(2);
$s.set(2);
expect($c.get()).toBe(3);
});
test("should be updated when one of dependent signal got updated", () => {
const $s1 = signal(1);
const $s2 = signal(2);
const $c = derived(() => $s1.get() + $s2.get());
expect($c.get()).toBe(3);
$s1.set(10);
expect($c.get()).toBe(12);
});
test("should trigger effect re-run", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
const $c = derived(() => $s.get() + 1);
effect(() => {
fn($c.get());
});
$s.set(2);
expect(fn.mock.calls).toEqual([[2], [3]]);
});
test("should not react to unnecessary change", () => {
const fn = mock((_v: unknown) => {});
const $s1 = signal(true);
const $s2 = signal(1);
effect(() => {
const $c = derived(() => {
return $s1.get() ? $s2.get() : null;
});
effect(() => {
fn($c.get());
});
});
$s1.set(false);
$s2.set(2);
expect(fn.mock.calls).toEqual([[1], [null]]);
});
test("should update nested computation", () => {
const fn = mock((_v: number) => {});
const $s = signal(1);
const $c1 = derived(() => $s.get() + 1);
const $c2 = derived(() => $c1.get() + 1);
const $c3 = derived(() => $c2.get() + 1);
effect(() => {
fn($c3.get());
});
$s.set(2);
expect(fn.mock.calls).toEqual([[4], [5]]);
});
test("should stop reacting to dependants' changes once disposed", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
const $c = derived(() => {
fn($s.get());
return 0;
});
$c.get();
$c.dispose();
$s.set(2);
expect(fn.mock.calls).toEqual([[1]]);
});
test("should de-associate Signals", () => {
const fn = mock(() => {});
const $s1 = signal(1);
const $s2 = signal(10);
const $s3 = signal(100);
const $c = derived(() => {
fn();
if ($s1.get() > 0) {
return $s2.get();
} else {
return $s3.get();
}
});
expect($c.get()).toBe(10);
$s1.set(0);
expect($c.get()).toBe(100);
$s2.set(20);
expect($c.get()).toBe(100);
expect(fn.mock.calls.length).toBe(2);
});
test("should manually specify which Computation to associate", async () => {
const fn = mock((_n: number) => {});
const $s = signal(0);
const $c = derived(() => $s.get());
effect((ctx) => {
sleep(5).then(() => {
// At the time this line, the Effect (ctx) is popped from global stack.
fn($c.get(ctx));
});
});
$s.set(1);
await sleep(6);
$s.set(2);
await sleep(6);
expect(fn.mock.calls).toEqual([[1], [2]]);
});
test("should dispose child setups on update", () => {
const fn = mock(() => {});
const $s = signal(0);
const $c = derived(() => {
effect(() => {
return () => {
fn();
};
});
return $s.get();
});
$s.set(1);
expect(fn.mock.calls.length).toBe(1);
$c.dispose();
expect(fn.mock.calls.length).toBe(2);
});
});
describe("effect", () => {
test("should run the callback function immediately", () => {
const fn = mock(() => {});
effect(() => {
fn();
});
expect(fn).toHaveBeenCalledTimes(1);
});
test("should re-run the callback function when inner signal got updated", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
effect(() => {
fn($s.get());
});
$s.set(2);
$s.set(3);
expect(fn.mock.calls).toEqual([[1], [2], [3]]);
});
test("should not run same effect multiple times for same signal update", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
const $c = derived(() => $s.get());
effect(() => {
$s.get();
fn($c.get());
});
$s.set(2);
expect(fn.mock.calls).toEqual([[1], [2]]);
});
test("should run effect without dupe", () => {
const fn = mock((_n: number) => {});
const $s = signal(1);
effect(() => {
fn($s.get());
});
$s.set(2);
$s.set(2);
$s.set(1);
$s.set(1);
$s.set(2);
$s.set(2);
expect(fn.mock.calls).toEqual([[1], [2], [1], [2]]);
});
test("should destroy signals and computations created inside the callback function", () => {
const fn1 = mock((_n: number) => {});
const fn2 = mock((_n: number) => {});
const $s1 = signal(1);
effect(() => {
const $s2 = signal($s1.get() * 10);
effect(() => {
fn1($s2.get());
});
const $c = derived(() => {
return $s1.get();
});
effect(() => {
fn2($c.get());
});
});
$s1.set(2);
expect(fn1.mock.calls).toEqual([[10], [20]]);
expect(fn2.mock.calls).toEqual([[1], [2]]);
});
test("should re-run callback only when scope item was updated", () => {
const fn1 = mock(() => {});
const fn2 = mock(() => {});
const fn3 = mock((_v: unknown) => {});
const $s = signal(1);
const $c = derived(() => $s.get());
effect(() => {
fn1();
effect(() => {
fn2();
$s.get();
effect(() => {
fn3($c.get());
});
});
});
$s.set(2);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(2);
expect(fn3.mock.calls).toEqual([[1], [2]]);
});
test("should run cleanup function on dispose", () => {
const fn = mock(() => {});
const e = effect(() => {
return () => {
fn();
};
});
expect(fn.mock.calls.length).toBe(0);
e.dispose();
expect(fn.mock.calls.length).toBe(1);
});
test("should run cleanup function on re-run", () => {
const fn = mock(() => {});
const $s = signal(1);
effect(() => {
$s.get();
return () => {
fn();
};
});
$s.set(2);
expect(fn.mock.calls.length).toBe(1);
});
test("should de-asocciate Signals", () => {
const fn = mock(() => {});
const $s1 = signal(1);
const $s2 = signal(10);
const $s3 = signal(100);
effect(() => {
fn();
if ($s1.get() > 0) {
$s2.get();
} else {
$s3.get();
}
});
$s1.set(0);
$s2.set(20);
expect(fn.mock.calls.length).toBe(2);
});
});
// These test measures object counts instead of heap size, because it's hard to
// define test thresholds (acceptable margin).
describe("GC", () => {
// Error margin for object counts
const ERROR_MARGIN = 100;
test("Signals should not retain references to stale computation closures", () => {
Bun.gc(true);
let p1: number;
let p2: number;
{
const $s = signal(1);
const $c = derived(() => $s.get().toString(16));
const eff = effect(() => {
$c.get();
});
for (let i = 0; i < 10_000; i++) {
$s.set(i);
}
Bun.gc(true);
p1 = heapStats().objectCount;
for (let i = 0; i < 10_000; i++) {
$s.set(i);
}
Bun.gc(true);
p2 = heapStats().objectCount;
eff.dispose();
}
Bun.gc(true);
const p3 = heapStats().objectCount;
// If stale references are held, memory usage would radically inrease.
expect(p2).toBeLessThanOrEqual(p1 + ERROR_MARGIN);
// If stale references are held, counts go over 10_000.
expect(p3).toBeLessThanOrEqual(p1 + ERROR_MARGIN);
expect(p3).toBeLessThanOrEqual(p2 + ERROR_MARGIN);
});
test("Deriveds and Effects should free descendants", () => {
Bun.gc(true);
let p1: number;
let p2: number;
{
const $s = signal(1);
const $c = derived(() => {
return signal($s.get().toString(16)).get();
});
const eff = effect(() => {
derived(() => {
return $c.get();
}).get();
});
for (let i = 0; i < 10_000; i++) {
$s.set(i);
}
Bun.gc(true);
p1 = heapStats().objectCount;
for (let i = 0; i < 10_000; i++) {
$s.set(i);
}
Bun.gc(true);
p2 = heapStats().objectCount;
eff.dispose();
}
Bun.gc(true);
const p3 = heapStats().objectCount;
// If stale references are held, memory usage would radically inrease.
expect(p2).toBeLessThanOrEqual(p1 + ERROR_MARGIN);
// If stale references are held, counts go over 10_000.
expect(p3).toBeLessThanOrEqual(p1 + ERROR_MARGIN);
expect(p3).toBeLessThanOrEqual(p2 + ERROR_MARGIN);
});
});