signalium 0.2.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/.turbo/turbo-build.log +12 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/cjs/config.d.ts +14 -5
  4. package/dist/cjs/config.d.ts.map +1 -1
  5. package/dist/cjs/config.js +23 -14
  6. package/dist/cjs/config.js.map +1 -1
  7. package/dist/cjs/debug.d.ts +3 -0
  8. package/dist/cjs/debug.d.ts.map +1 -0
  9. package/dist/cjs/debug.js +16 -0
  10. package/dist/cjs/debug.js.map +1 -0
  11. package/dist/cjs/hooks.d.ts +45 -0
  12. package/dist/cjs/hooks.d.ts.map +1 -0
  13. package/dist/cjs/hooks.js +260 -0
  14. package/dist/cjs/hooks.js.map +1 -0
  15. package/dist/cjs/index.d.ts +5 -3
  16. package/dist/cjs/index.d.ts.map +1 -1
  17. package/dist/cjs/index.js +21 -8
  18. package/dist/cjs/index.js.map +1 -1
  19. package/dist/cjs/react/context.d.ts +4 -0
  20. package/dist/cjs/react/context.d.ts.map +1 -0
  21. package/dist/cjs/react/context.js +10 -0
  22. package/dist/cjs/react/context.js.map +1 -0
  23. package/dist/cjs/react/index.d.ts +5 -0
  24. package/dist/cjs/react/index.d.ts.map +1 -0
  25. package/dist/cjs/react/index.js +12 -0
  26. package/dist/cjs/react/index.js.map +1 -0
  27. package/dist/cjs/react/provider.d.ts +7 -0
  28. package/dist/cjs/react/provider.d.ts.map +1 -0
  29. package/dist/cjs/react/provider.js +13 -0
  30. package/dist/cjs/react/provider.js.map +1 -0
  31. package/dist/cjs/react/signal-value.d.ts +3 -0
  32. package/dist/cjs/react/signal-value.d.ts.map +1 -0
  33. package/dist/cjs/react/signal-value.js +42 -0
  34. package/dist/cjs/react/signal-value.js.map +1 -0
  35. package/dist/cjs/react/state.d.ts +3 -0
  36. package/dist/cjs/react/state.d.ts.map +1 -0
  37. package/dist/cjs/react/state.js +13 -0
  38. package/dist/cjs/react/state.js.map +1 -0
  39. package/dist/cjs/scheduling.d.ts +5 -0
  40. package/dist/cjs/scheduling.d.ts.map +1 -1
  41. package/dist/cjs/scheduling.js +59 -5
  42. package/dist/cjs/scheduling.js.map +1 -1
  43. package/dist/cjs/signals.d.ts +28 -65
  44. package/dist/cjs/signals.d.ts.map +1 -1
  45. package/dist/cjs/signals.js +223 -65
  46. package/dist/cjs/signals.js.map +1 -1
  47. package/dist/cjs/trace.d.ts +127 -0
  48. package/dist/cjs/trace.d.ts.map +1 -0
  49. package/dist/cjs/trace.js +319 -0
  50. package/dist/cjs/trace.js.map +1 -0
  51. package/dist/cjs/types.d.ts +66 -0
  52. package/dist/cjs/types.d.ts.map +1 -0
  53. package/dist/cjs/types.js +3 -0
  54. package/dist/cjs/types.js.map +1 -0
  55. package/dist/cjs/utils.d.ts +4 -0
  56. package/dist/cjs/utils.d.ts.map +1 -0
  57. package/dist/cjs/utils.js +80 -0
  58. package/dist/cjs/utils.js.map +1 -0
  59. package/dist/esm/config.d.ts +14 -5
  60. package/dist/esm/config.d.ts.map +1 -1
  61. package/dist/esm/config.js +19 -11
  62. package/dist/esm/config.js.map +1 -1
  63. package/dist/esm/debug.d.ts +3 -0
  64. package/dist/esm/debug.d.ts.map +1 -0
  65. package/dist/esm/debug.js +3 -0
  66. package/dist/esm/debug.js.map +1 -0
  67. package/dist/esm/hooks.d.ts +45 -0
  68. package/dist/esm/hooks.d.ts.map +1 -0
  69. package/dist/esm/hooks.js +243 -0
  70. package/dist/esm/hooks.js.map +1 -0
  71. package/dist/esm/index.d.ts +5 -3
  72. package/dist/esm/index.d.ts.map +1 -1
  73. package/dist/esm/index.js +4 -2
  74. package/dist/esm/index.js.map +1 -1
  75. package/dist/esm/react/context.d.ts +4 -0
  76. package/dist/esm/react/context.d.ts.map +1 -0
  77. package/dist/esm/react/context.js +6 -0
  78. package/dist/esm/react/context.js.map +1 -0
  79. package/dist/esm/react/index.d.ts +5 -0
  80. package/dist/esm/react/index.d.ts.map +1 -0
  81. package/dist/esm/react/index.js +5 -0
  82. package/dist/esm/react/index.js.map +1 -0
  83. package/dist/esm/react/provider.d.ts +7 -0
  84. package/dist/esm/react/provider.d.ts.map +1 -0
  85. package/dist/esm/react/provider.js +10 -0
  86. package/dist/esm/react/provider.js.map +1 -0
  87. package/dist/esm/react/signal-value.d.ts +3 -0
  88. package/dist/esm/react/signal-value.d.ts.map +1 -0
  89. package/dist/esm/react/signal-value.js +38 -0
  90. package/dist/esm/react/signal-value.js.map +1 -0
  91. package/dist/esm/react/state.d.ts +3 -0
  92. package/dist/esm/react/state.d.ts.map +1 -0
  93. package/dist/esm/react/state.js +10 -0
  94. package/dist/esm/react/state.js.map +1 -0
  95. package/dist/esm/scheduling.d.ts +5 -0
  96. package/dist/esm/scheduling.d.ts.map +1 -1
  97. package/dist/esm/scheduling.js +51 -1
  98. package/dist/esm/scheduling.js.map +1 -1
  99. package/dist/esm/signals.d.ts +28 -65
  100. package/dist/esm/signals.d.ts.map +1 -1
  101. package/dist/esm/signals.js +215 -61
  102. package/dist/esm/signals.js.map +1 -1
  103. package/dist/esm/trace.d.ts +127 -0
  104. package/dist/esm/trace.d.ts.map +1 -0
  105. package/dist/esm/trace.js +311 -0
  106. package/dist/esm/trace.js.map +1 -0
  107. package/dist/esm/types.d.ts +66 -0
  108. package/dist/esm/types.d.ts.map +1 -0
  109. package/dist/esm/types.js +2 -0
  110. package/dist/esm/types.js.map +1 -0
  111. package/dist/esm/utils.d.ts +4 -0
  112. package/dist/esm/utils.d.ts.map +1 -0
  113. package/dist/esm/utils.js +75 -0
  114. package/dist/esm/utils.js.map +1 -0
  115. package/package.json +43 -2
  116. package/src/__tests__/hooks/async-computed.test.ts +190 -0
  117. package/src/__tests__/hooks/async-task.test.ts +227 -0
  118. package/src/__tests__/hooks/computed.test.ts +126 -0
  119. package/src/__tests__/hooks/context.test.ts +527 -0
  120. package/src/__tests__/hooks/nesting.test.ts +303 -0
  121. package/src/__tests__/hooks/params-and-state.test.ts +168 -0
  122. package/src/__tests__/hooks/subscription.test.ts +97 -0
  123. package/src/__tests__/signals/async.test.ts +416 -0
  124. package/src/__tests__/signals/basic.test.ts +399 -0
  125. package/src/__tests__/signals/subscription.test.ts +632 -0
  126. package/src/__tests__/signals/watcher.test.ts +253 -0
  127. package/src/__tests__/utils/async.ts +6 -0
  128. package/src/__tests__/utils/builders.ts +22 -0
  129. package/src/__tests__/utils/instrumented-hooks.ts +309 -0
  130. package/src/__tests__/utils/instrumented-signals.ts +281 -0
  131. package/src/__tests__/utils/permute.ts +74 -0
  132. package/src/config.ts +32 -17
  133. package/src/debug.ts +14 -0
  134. package/src/hooks.ts +429 -0
  135. package/src/index.ts +28 -3
  136. package/src/react/__tests__/react.test.tsx +135 -0
  137. package/src/react/context.ts +8 -0
  138. package/src/react/index.ts +4 -0
  139. package/src/react/provider.tsx +18 -0
  140. package/src/react/signal-value.ts +56 -0
  141. package/src/react/state.ts +13 -0
  142. package/src/scheduling.ts +69 -1
  143. package/src/signals.ts +331 -157
  144. package/src/trace.ts +449 -0
  145. package/src/types.ts +86 -0
  146. package/src/utils.ts +83 -0
  147. package/tsconfig.json +2 -1
  148. package/vitest.workspace.ts +24 -0
  149. package/src/__tests__/async.test.ts +0 -426
  150. package/src/__tests__/basic.test.ts +0 -378
  151. package/src/__tests__/subscription.test.ts +0 -645
  152. package/src/__tests__/utils/instrumented.ts +0 -326
@@ -1,645 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { state, computed, subscription, watcher } from './utils/instrumented.js';
3
-
4
- const sleep = (ms = 0) => new Promise(r => setTimeout(r, ms));
5
- const nextTick = () => sleep();
6
-
7
- describe('Subscription Signal functionality', () => {
8
- describe('subscribe', () => {
9
- test('Subscribes when first accessed when watched', async () => {
10
- const s = subscription(() => {}, { initValue: 123 });
11
-
12
- const w = watcher(() => {
13
- s.get();
14
- });
15
-
16
- expect(s).toHaveValueAndCounts(123, {
17
- subscribe: 0,
18
- });
19
-
20
- await nextTick();
21
-
22
- expect(s).toHaveValueAndCounts(123, {
23
- subscribe: 1,
24
- });
25
- });
26
-
27
- test('Subscribes if nested', async () => {
28
- const s = subscription(() => {}, { initValue: 123 });
29
-
30
- const c = computed(() => {
31
- s.get();
32
- });
33
-
34
- const w = watcher(() => {
35
- c.get();
36
- });
37
-
38
- expect(c).toHaveCounts({ get: 0, compute: 0 });
39
- expect(s).toHaveValueAndCounts(123, {
40
- subscribe: 0,
41
- });
42
-
43
- await nextTick();
44
-
45
- expect(c).toHaveCounts({ get: 1, compute: 1 });
46
- expect(s).toHaveValueAndCounts(123, {
47
- subscribe: 1,
48
- });
49
- });
50
-
51
- test('Subscribes when dynamically connected to a watcher', async () => {
52
- const a = state(false);
53
-
54
- const s = subscription(() => {}, { initValue: 123 });
55
-
56
- const w = watcher(() => {
57
- return a.get() ? s.get() : 0;
58
- });
59
-
60
- expect(w).toHaveCounts({ effect: 0 });
61
- expect(s).toHaveValueAndCounts(123, {
62
- subscribe: 0,
63
- });
64
-
65
- await nextTick();
66
-
67
- expect(w).toHaveCounts({ effect: 1 });
68
- expect(s).toHaveValueAndCounts(123, {
69
- subscribe: 0,
70
- });
71
-
72
- a.set(true);
73
-
74
- await nextTick();
75
-
76
- expect(w).toHaveCounts({ effect: 2 });
77
- expect(s).toHaveValueAndCounts(123, {
78
- subscribe: 1,
79
- });
80
- });
81
-
82
- test('Subscribes when dynamically used by a connected computed', async () => {
83
- const a = state(false);
84
-
85
- const s = subscription(() => {}, { initValue: 123 });
86
-
87
- const c = computed(() => {
88
- return a.get() ? s.get() : 0;
89
- });
90
-
91
- const w = watcher(() => {
92
- return c.get();
93
- });
94
-
95
- expect(w).toHaveCounts({ effect: 0 });
96
- expect(c).toHaveCounts({ get: 0, compute: 0 });
97
- expect(s).toHaveValueAndCounts(123, {
98
- subscribe: 0,
99
- });
100
-
101
- await nextTick();
102
-
103
- expect(w).toHaveCounts({ effect: 1 });
104
- expect(c).toHaveCounts({ get: 1, compute: 1 });
105
- expect(s).toHaveValueAndCounts(123, {
106
- subscribe: 0,
107
- });
108
-
109
- a.set(true);
110
-
111
- await nextTick();
112
-
113
- expect(w).toHaveCounts({ effect: 2 });
114
- expect(c).toHaveCounts({ get: 2, compute: 2 });
115
- expect(s).toHaveValueAndCounts(123, {
116
- subscribe: 1,
117
- });
118
- });
119
-
120
- test('Subscribes when parent is connected and uncached', async () => {
121
- const a = state(false);
122
-
123
- const s = subscription(() => {}, { initValue: 123 });
124
-
125
- const c = computed(() => {
126
- return s.get();
127
- });
128
-
129
- const w = watcher(() => {
130
- return a.get() ? c.get() : 0;
131
- });
132
-
133
- expect(w).toHaveCounts({ effect: 0 });
134
- expect(c).toHaveCounts({ get: 0, compute: 0 });
135
- expect(s).toHaveValueAndCounts(123, {
136
- subscribe: 0,
137
- });
138
-
139
- await nextTick();
140
-
141
- expect(w).toHaveCounts({ effect: 1 });
142
- expect(c).toHaveCounts({ get: 0, compute: 0 });
143
- expect(s).toHaveValueAndCounts(123, {
144
- subscribe: 0,
145
- });
146
-
147
- a.set(true);
148
-
149
- await nextTick();
150
-
151
- expect(w).toHaveCounts({ effect: 2 });
152
- expect(c).toHaveCounts({ get: 1, compute: 1 });
153
- expect(s).toHaveValueAndCounts(123, {
154
- subscribe: 1,
155
- });
156
- });
157
-
158
- test('Subscribes when parent is connected and dirty', async () => {
159
- const a = state(false);
160
-
161
- const s = subscription(() => {}, { initValue: 123 });
162
-
163
- const c = computed(() => {
164
- return a.get() ? s.get() : 0;
165
- });
166
-
167
- const w = watcher(() => {
168
- return a.get() ? c.get() : 0;
169
- });
170
-
171
- expect(w).toHaveCounts({ effect: 0 });
172
- expect(c).toHaveCounts({ get: 0, compute: 0 });
173
- expect(s).toHaveValueAndCounts(123, {
174
- subscribe: 0,
175
- });
176
-
177
- await nextTick();
178
-
179
- expect(w).toHaveCounts({ effect: 1 });
180
- expect(c).toHaveCounts({ get: 0, compute: 0 });
181
- expect(s).toHaveValueAndCounts(123, {
182
- subscribe: 0,
183
- });
184
-
185
- a.set(true);
186
-
187
- await nextTick();
188
-
189
- expect(w).toHaveCounts({ effect: 2 });
190
- expect(c).toHaveCounts({ get: 1, compute: 1 });
191
- expect(s).toHaveValueAndCounts(123, {
192
- subscribe: 1,
193
- });
194
- });
195
-
196
- test('Subscribes when parent is connected and cached', async () => {
197
- const a = state(false);
198
-
199
- const s = subscription(() => {}, { initValue: 123 });
200
-
201
- const c = computed(() => {
202
- return s.get();
203
- });
204
-
205
- const w = watcher(() => {
206
- return a.get() ? c.get() : 0;
207
- });
208
-
209
- expect(w).toHaveCounts({ effect: 0 });
210
- expect(c).toHaveCounts({ get: 0, compute: 0 });
211
- expect(s).toHaveValueAndCounts(123, {
212
- subscribe: 0,
213
- });
214
-
215
- await nextTick();
216
-
217
- c.get();
218
-
219
- expect(w).toHaveCounts({ effect: 1 });
220
- expect(c).toHaveCounts({ get: 1, compute: 1 });
221
- expect(s).toHaveValueAndCounts(123, {
222
- subscribe: 0,
223
- });
224
-
225
- a.set(true);
226
-
227
- await nextTick();
228
-
229
- expect(w).toHaveCounts({ effect: 2 });
230
- expect(c).toHaveValueAndCounts(123, { get: 3, compute: 1 });
231
- expect(s).toHaveValueAndCounts(123, {
232
- subscribe: 1,
233
- });
234
- });
235
-
236
- test('Can set value during initial subscribe and value is used', async () => {
237
- const s = subscription(
238
- (get, set) => {
239
- set(456);
240
- },
241
- { initValue: 123 },
242
- );
243
-
244
- watcher(() => {
245
- expect(s.get()).toBe(456);
246
- });
247
-
248
- expect(s).toHaveValueAndCounts(123, {
249
- subscribe: 0,
250
- });
251
-
252
- await nextTick();
253
-
254
- expect(s).toHaveValueAndCounts(456, {
255
- subscribe: 1,
256
- });
257
- });
258
-
259
- test('Can set value during resubscribe and value is used', async () => {
260
- const s = subscription(
261
- (get, set) => {
262
- set(get() + 1);
263
- },
264
- { initValue: 123 },
265
- );
266
-
267
- let value;
268
-
269
- let w = watcher(() => {
270
- value = s.get();
271
- });
272
-
273
- await nextTick();
274
-
275
- expect(value).toBe(124);
276
- expect(s).toHaveValueAndCounts(124, {
277
- subscribe: 1,
278
- });
279
-
280
- w.disconnect();
281
-
282
- await nextTick();
283
-
284
- expect(value).toBe(124);
285
- expect(s).toHaveValueAndCounts(124, {
286
- subscribe: 1,
287
- });
288
-
289
- w = watcher(() => {
290
- value = s.get();
291
- });
292
-
293
- await nextTick();
294
-
295
- expect(value).toBe(125);
296
- expect(s).toHaveValueAndCounts(125, {
297
- subscribe: 2,
298
- });
299
- });
300
-
301
- test('Can set value during resubscribe and cached parents are dirtied', async () => {
302
- const s = subscription(
303
- (get, set) => {
304
- set(get() + 1);
305
- },
306
- { initValue: 123 },
307
- );
308
-
309
- const c = computed(() => {
310
- return s.get() + 1;
311
- });
312
-
313
- let value;
314
-
315
- let w = watcher(() => {
316
- value = c.get();
317
- });
318
-
319
- await nextTick();
320
-
321
- expect(value).toBe(125);
322
- expect(s).toHaveValueAndCounts(124, {
323
- subscribe: 1,
324
- });
325
-
326
- w.disconnect();
327
-
328
- await nextTick();
329
-
330
- expect(value).toBe(125);
331
- expect(s).toHaveValueAndCounts(124, {
332
- subscribe: 1,
333
- });
334
-
335
- w = watcher(() => {
336
- value = c.get();
337
- });
338
-
339
- await nextTick();
340
-
341
- expect(value).toBe(126);
342
- expect(s).toHaveValueAndCounts(125, {
343
- subscribe: 2,
344
- });
345
- });
346
- });
347
-
348
- describe('update', () => {
349
- test('Updates if consumed values are changed', async () => {
350
- const a = state(0);
351
-
352
- const s = subscription(
353
- (get, set) => {
354
- set(a.get() + 1);
355
-
356
- return {
357
- update() {
358
- set(a.get() + 1);
359
- },
360
- };
361
- },
362
- { initValue: 123 },
363
- );
364
-
365
- watcher(() => {
366
- s.get();
367
- });
368
-
369
- expect(s).toHaveValueAndCounts(123, {
370
- subscribe: 0,
371
- });
372
-
373
- await nextTick();
374
-
375
- expect(s).toHaveValueAndCounts(1, {
376
- subscribe: 1,
377
- });
378
-
379
- a.set(1);
380
-
381
- expect(s).toHaveValueAndCounts(2, {
382
- subscribe: 1,
383
- update: 1,
384
- });
385
- });
386
-
387
- test('Update is scheduled and values are pulled if subscription is active and dirtied, even if parents are not', async () => {
388
- const a = state(0);
389
-
390
- const s = subscription(
391
- (get, set) => {
392
- set(a.get() + 1);
393
-
394
- return {
395
- update() {
396
- set(a.get());
397
- },
398
- };
399
- },
400
- { initValue: 123 },
401
- );
402
-
403
- const w = watcher(() => {
404
- s.get();
405
- });
406
-
407
- expect(s).toHaveValueAndCounts(123, {
408
- subscribe: 0,
409
- });
410
-
411
- await nextTick();
412
-
413
- expect(w).toHaveCounts({ effect: 1 });
414
- expect(s).toHaveValueAndCounts(1, {
415
- subscribe: 1,
416
- });
417
-
418
- a.set(1);
419
-
420
- await nextTick();
421
-
422
- expect(s).toHaveValueAndCounts(1, {
423
- subscribe: 1,
424
- update: 1,
425
- });
426
-
427
- expect(w).toHaveCounts({ effect: 1 });
428
- });
429
-
430
- test('Update is pulled eagerly if a parent is scheduled before a subscription and uses the updates value', async () => {
431
- const a = state(0);
432
- const b = state(0);
433
-
434
- const s = subscription(
435
- (get, set) => {
436
- set(a.get() + 1);
437
-
438
- return {
439
- update() {
440
- set(a.get() + 1);
441
- },
442
- };
443
- },
444
- { initValue: 123 },
445
- );
446
-
447
- let value;
448
-
449
- const w = watcher(() => {
450
- b.get();
451
- value = s.get();
452
- });
453
-
454
- expect(s).toHaveValueAndCounts(123, {
455
- subscribe: 0,
456
- });
457
-
458
- await nextTick();
459
-
460
- expect(w).toHaveCounts({ effect: 1 });
461
- expect(s).toHaveValueAndCounts(1, {
462
- subscribe: 1,
463
- });
464
-
465
- b.set(1);
466
- a.set(1);
467
-
468
- await nextTick();
469
-
470
- expect(value).toBe(2);
471
-
472
- expect(s).toHaveValueAndCounts(2, {
473
- subscribe: 1,
474
- update: 1,
475
- });
476
-
477
- expect(w).toHaveCounts({ effect: 2 });
478
- });
479
-
480
- test('Update can set value during eager pull and updated value is used by parent', async () => {
481
- // ...
482
- });
483
-
484
- test('Update can set value and trigger a dirty for parent that happens in the same flush', async () => {
485
- // ...
486
- });
487
-
488
- test('Update can trigger an dirty for a parent that has already flushed this time around (secondary flush, edge case)', async () => {
489
- // ...
490
- });
491
- });
492
-
493
- describe('unsubscribe', async () => {
494
- test('It unsubscribes when all watchers are disconnected', async () => {
495
- const s = subscription(
496
- (get, set) => {
497
- return {
498
- unsubscribe() {
499
- // ...
500
- },
501
- };
502
- },
503
- { initValue: 123 },
504
- );
505
-
506
- let value;
507
-
508
- let w = watcher(() => {
509
- value = s.get();
510
- });
511
-
512
- let w2 = watcher(() => {
513
- s.get();
514
- });
515
-
516
- await nextTick();
517
-
518
- expect(value).toBe(123);
519
- expect(s).toHaveValueAndCounts(123, {
520
- subscribe: 1,
521
- unsubscribe: 0,
522
- });
523
-
524
- w.disconnect();
525
- w2.disconnect();
526
-
527
- await nextTick();
528
-
529
- expect(value).toBe(123);
530
- expect(s).toHaveValueAndCounts(123, {
531
- subscribe: 1,
532
- unsubscribe: 1,
533
- });
534
- });
535
-
536
- test('It unsubscribes when all watchers are disconnected at different times', async () => {
537
- const s = subscription(
538
- (get, set) => {
539
- return {
540
- unsubscribe() {
541
- // ...
542
- },
543
- };
544
- },
545
- { initValue: 123 },
546
- );
547
-
548
- let value;
549
-
550
- let w = watcher(() => {
551
- value = s.get();
552
- });
553
-
554
- await nextTick();
555
-
556
- expect(value).toBe(123);
557
- expect(s).toHaveValueAndCounts(123, {
558
- subscribe: 1,
559
- unsubscribe: 0,
560
- });
561
-
562
- let w2 = watcher(() => {
563
- s.get();
564
- });
565
-
566
- await nextTick();
567
-
568
- expect(value).toBe(123);
569
- expect(s).toHaveValueAndCounts(123, {
570
- subscribe: 1,
571
- unsubscribe: 0,
572
- });
573
-
574
- w.disconnect();
575
-
576
- await nextTick();
577
-
578
- expect(value).toBe(123);
579
- expect(s).toHaveValueAndCounts(123, {
580
- subscribe: 1,
581
- unsubscribe: 0,
582
- });
583
-
584
- w2.disconnect();
585
-
586
- await nextTick();
587
-
588
- expect(value).toBe(123);
589
- expect(s).toHaveValueAndCounts(123, {
590
- subscribe: 1,
591
- unsubscribe: 1,
592
- });
593
- });
594
-
595
- test('It stays subscribed when all watchers are disconnected and new ones are connected in the same flush', async () => {
596
- const s = subscription(
597
- (get, set) => {
598
- return {
599
- unsubscribe() {
600
- // ...
601
- },
602
- };
603
- },
604
- { initValue: 123 },
605
- );
606
-
607
- let value;
608
-
609
- let w = watcher(() => {
610
- value = s.get();
611
- });
612
-
613
- await nextTick();
614
-
615
- expect(value).toBe(123);
616
- expect(s).toHaveValueAndCounts(123, {
617
- subscribe: 1,
618
- unsubscribe: 0,
619
- });
620
-
621
- w.disconnect();
622
- let w2 = watcher(() => {
623
- s.get();
624
- });
625
-
626
- await nextTick();
627
-
628
- expect(value).toBe(123);
629
- expect(s).toHaveValueAndCounts(123, {
630
- subscribe: 1,
631
- unsubscribe: 0,
632
- });
633
-
634
- w2.disconnect();
635
-
636
- await nextTick();
637
-
638
- expect(value).toBe(123);
639
- expect(s).toHaveValueAndCounts(123, {
640
- subscribe: 1,
641
- unsubscribe: 1,
642
- });
643
- });
644
- });
645
- });