@livestore/react 0.4.0-dev.20 → 0.4.0-dev.22

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 (100) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/StoreRegistryContext.d.ts +56 -0
  3. package/dist/StoreRegistryContext.d.ts.map +1 -0
  4. package/dist/StoreRegistryContext.js +61 -0
  5. package/dist/StoreRegistryContext.js.map +1 -0
  6. package/dist/__tests__/fixture.d.ts.map +1 -1
  7. package/dist/__tests__/fixture.js +1 -6
  8. package/dist/__tests__/fixture.js.map +1 -1
  9. package/dist/experimental/components/LiveList.d.ts +4 -2
  10. package/dist/experimental/components/LiveList.d.ts.map +1 -1
  11. package/dist/experimental/components/LiveList.js +6 -5
  12. package/dist/experimental/components/LiveList.js.map +1 -1
  13. package/dist/experimental/mod.d.ts +0 -1
  14. package/dist/experimental/mod.d.ts.map +1 -1
  15. package/dist/experimental/mod.js +0 -1
  16. package/dist/experimental/mod.js.map +1 -1
  17. package/dist/mod.d.ts +4 -3
  18. package/dist/mod.d.ts.map +1 -1
  19. package/dist/mod.js +3 -2
  20. package/dist/mod.js.map +1 -1
  21. package/dist/useClientDocument.d.ts +33 -0
  22. package/dist/useClientDocument.d.ts.map +1 -1
  23. package/dist/useClientDocument.js +1 -4
  24. package/dist/useClientDocument.js.map +1 -1
  25. package/dist/useQuery.d.ts +1 -1
  26. package/dist/useQuery.d.ts.map +1 -1
  27. package/dist/useQuery.js +2 -5
  28. package/dist/useQuery.js.map +1 -1
  29. package/dist/useStore.d.ts +62 -7
  30. package/dist/useStore.d.ts.map +1 -1
  31. package/dist/useStore.js +73 -15
  32. package/dist/useStore.js.map +1 -1
  33. package/dist/useStore.test.d.ts.map +1 -0
  34. package/dist/useStore.test.js +196 -0
  35. package/dist/useStore.test.js.map +1 -0
  36. package/package.json +7 -7
  37. package/src/StoreRegistryContext.tsx +69 -0
  38. package/src/__tests__/fixture.tsx +1 -13
  39. package/src/experimental/components/LiveList.tsx +13 -4
  40. package/src/experimental/mod.ts +0 -1
  41. package/src/mod.ts +4 -3
  42. package/src/useClientDocument.ts +36 -5
  43. package/src/useQuery.ts +2 -6
  44. package/src/useStore.test.tsx +271 -0
  45. package/src/useStore.ts +102 -23
  46. package/dist/LiveStoreContext.d.ts +0 -13
  47. package/dist/LiveStoreContext.d.ts.map +0 -1
  48. package/dist/LiveStoreContext.js +0 -3
  49. package/dist/LiveStoreContext.js.map +0 -1
  50. package/dist/LiveStoreProvider.d.ts +0 -66
  51. package/dist/LiveStoreProvider.d.ts.map +0 -1
  52. package/dist/LiveStoreProvider.js +0 -232
  53. package/dist/LiveStoreProvider.js.map +0 -1
  54. package/dist/LiveStoreProvider.test.d.ts +0 -2
  55. package/dist/LiveStoreProvider.test.d.ts.map +0 -1
  56. package/dist/LiveStoreProvider.test.js +0 -117
  57. package/dist/LiveStoreProvider.test.js.map +0 -1
  58. package/dist/experimental/multi-store/StoreRegistry.d.ts +0 -61
  59. package/dist/experimental/multi-store/StoreRegistry.d.ts.map +0 -1
  60. package/dist/experimental/multi-store/StoreRegistry.js +0 -275
  61. package/dist/experimental/multi-store/StoreRegistry.js.map +0 -1
  62. package/dist/experimental/multi-store/StoreRegistry.test.d.ts +0 -2
  63. package/dist/experimental/multi-store/StoreRegistry.test.d.ts.map +0 -1
  64. package/dist/experimental/multi-store/StoreRegistry.test.js +0 -464
  65. package/dist/experimental/multi-store/StoreRegistry.test.js.map +0 -1
  66. package/dist/experimental/multi-store/StoreRegistryContext.d.ts +0 -10
  67. package/dist/experimental/multi-store/StoreRegistryContext.d.ts.map +0 -1
  68. package/dist/experimental/multi-store/StoreRegistryContext.js +0 -15
  69. package/dist/experimental/multi-store/StoreRegistryContext.js.map +0 -1
  70. package/dist/experimental/multi-store/mod.d.ts +0 -6
  71. package/dist/experimental/multi-store/mod.d.ts.map +0 -1
  72. package/dist/experimental/multi-store/mod.js +0 -6
  73. package/dist/experimental/multi-store/mod.js.map +0 -1
  74. package/dist/experimental/multi-store/storeOptions.d.ts +0 -4
  75. package/dist/experimental/multi-store/storeOptions.d.ts.map +0 -1
  76. package/dist/experimental/multi-store/storeOptions.js +0 -4
  77. package/dist/experimental/multi-store/storeOptions.js.map +0 -1
  78. package/dist/experimental/multi-store/types.d.ts +0 -44
  79. package/dist/experimental/multi-store/types.d.ts.map +0 -1
  80. package/dist/experimental/multi-store/types.js +0 -2
  81. package/dist/experimental/multi-store/types.js.map +0 -1
  82. package/dist/experimental/multi-store/useStore.d.ts +0 -11
  83. package/dist/experimental/multi-store/useStore.d.ts.map +0 -1
  84. package/dist/experimental/multi-store/useStore.js +0 -21
  85. package/dist/experimental/multi-store/useStore.js.map +0 -1
  86. package/dist/experimental/multi-store/useStore.test.d.ts.map +0 -1
  87. package/dist/experimental/multi-store/useStore.test.js +0 -144
  88. package/dist/experimental/multi-store/useStore.test.js.map +0 -1
  89. package/src/LiveStoreContext.ts +0 -14
  90. package/src/LiveStoreProvider.test.tsx +0 -248
  91. package/src/LiveStoreProvider.tsx +0 -421
  92. package/src/experimental/multi-store/StoreRegistry.test.ts +0 -631
  93. package/src/experimental/multi-store/StoreRegistry.ts +0 -347
  94. package/src/experimental/multi-store/StoreRegistryContext.tsx +0 -23
  95. package/src/experimental/multi-store/mod.ts +0 -5
  96. package/src/experimental/multi-store/storeOptions.ts +0 -8
  97. package/src/experimental/multi-store/types.ts +0 -55
  98. package/src/experimental/multi-store/useStore.test.tsx +0 -197
  99. package/src/experimental/multi-store/useStore.ts +0 -34
  100. /package/dist/{experimental/multi-store/useStore.test.d.ts → useStore.test.d.ts} +0 -0
@@ -1,631 +0,0 @@
1
- import { makeInMemoryAdapter } from '@livestore/adapter-web'
2
- import { StoreInternalsSymbol } from '@livestore/livestore'
3
- import { afterEach, describe, expect, it, vi } from 'vitest'
4
- import { schema } from '../../__tests__/fixture.tsx'
5
- import { DEFAULT_UNUSED_CACHE_TIME, StoreRegistry } from './StoreRegistry.ts'
6
- import { storeOptions } from './storeOptions.ts'
7
- import type { CachedStoreOptions } from './types.ts'
8
-
9
- describe('StoreRegistry', () => {
10
- afterEach(() => {
11
- vi.clearAllTimers()
12
- vi.useRealTimers()
13
- })
14
-
15
- it('returns a Promise when the store is loading', async () => {
16
- const registry = new StoreRegistry()
17
- const result = registry.getOrLoad(testStoreOptions())
18
-
19
- expect(result).toBeInstanceOf(Promise)
20
-
21
- // Clean up
22
- const store = await result
23
- await store.shutdownPromise()
24
- })
25
-
26
- it('returns cached store synchronously after first load resolves', async () => {
27
- const registry = new StoreRegistry()
28
-
29
- const initial = registry.getOrLoad(testStoreOptions())
30
- expect(initial).toBeInstanceOf(Promise)
31
-
32
- const store = await initial
33
-
34
- const cached = registry.getOrLoad(testStoreOptions())
35
- expect(cached).toBe(store)
36
- expect(cached).not.toBeInstanceOf(Promise)
37
-
38
- // Clean up
39
- await store.shutdownPromise()
40
- })
41
-
42
- it('reuses the same promise for concurrent getOrLoad calls while loading', async () => {
43
- const registry = new StoreRegistry()
44
- const options = testStoreOptions()
45
-
46
- const first = registry.getOrLoad(options)
47
- const second = registry.getOrLoad(options)
48
-
49
- // Both should be the same promise
50
- expect(first).toBe(second)
51
- expect(first).toBeInstanceOf(Promise)
52
-
53
- const store = await first
54
-
55
- // Both promises should resolve to the same store
56
- expect(await second).toBe(store)
57
-
58
- // Clean up
59
- await store.shutdownPromise()
60
- })
61
-
62
- it('stores and rethrows the rejection on subsequent getOrLoad calls after a failure', async () => {
63
- const registry = new StoreRegistry()
64
-
65
- // Create an invalid adapter that will cause an error
66
- const badOptions = testStoreOptions({
67
- // @ts-expect-error - intentionally passing invalid adapter to trigger error
68
- adapter: null,
69
- })
70
-
71
- await expect(registry.getOrLoad(badOptions)).rejects.toThrow()
72
-
73
- // Subsequent call should throw the cached error synchronously
74
- expect(() => registry.getOrLoad(badOptions)).toThrow()
75
- })
76
-
77
- it('disposes store after unusedCacheTime expires', async () => {
78
- vi.useFakeTimers()
79
- const registry = new StoreRegistry()
80
- const unusedCacheTime = 25
81
- const options = testStoreOptions({ unusedCacheTime })
82
-
83
- const store = await registry.getOrLoad(options)
84
-
85
- // Store should be cached
86
- expect(registry.getOrLoad(options)).toBe(store)
87
-
88
- // Advance time to trigger disposal
89
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
90
-
91
- // After disposal, store should be removed
92
- // The store is removed from cache, so next getOrLoad creates a new one
93
- const nextStore = await registry.getOrLoad(options)
94
-
95
- // Should be a different store instance
96
- expect(nextStore).not.toBe(store)
97
- expect(nextStore[StoreInternalsSymbol].clientSession.debugInstanceId).toBeDefined()
98
-
99
- // Clean up the second store (first one was disposed)
100
- await nextStore.shutdownPromise()
101
- })
102
-
103
- it('keeps the longest unusedCacheTime seen for a store when options vary across calls', async () => {
104
- vi.useFakeTimers()
105
- const registry = new StoreRegistry()
106
-
107
- const options = testStoreOptions({ unusedCacheTime: 10 })
108
- const unsubscribe = registry.subscribe(options.storeId, () => {})
109
-
110
- const store = await registry.getOrLoad(options)
111
-
112
- // Call with longer unusedCacheTime
113
- await registry.getOrLoad(testStoreOptions({ unusedCacheTime: 100 }))
114
-
115
- unsubscribe()
116
-
117
- // After 99ms, store should still be alive (100ms unusedCacheTime used)
118
- await vi.advanceTimersByTimeAsync(99)
119
-
120
- // Store should still be cached
121
- expect(registry.getOrLoad(options)).toBe(store)
122
-
123
- // After the full 100ms, store should be disposed
124
- await vi.advanceTimersByTimeAsync(1)
125
-
126
- // Next getOrLoad should create a new store
127
- const nextStore = await registry.getOrLoad(options)
128
- expect(nextStore).not.toBe(store)
129
-
130
- // Clean up the second store (first one was disposed)
131
- await nextStore.shutdownPromise()
132
- })
133
-
134
- it('preload does not throw', async () => {
135
- const registry = new StoreRegistry()
136
-
137
- // Create invalid options that would cause an error
138
- const badOptions = testStoreOptions({
139
- // @ts-expect-error - intentionally passing invalid adapter to trigger error
140
- adapter: null,
141
- })
142
-
143
- // preload should not throw
144
- await expect(registry.preload(badOptions)).resolves.toBeUndefined()
145
-
146
- // But subsequent getOrLoad should throw the cached error
147
- expect(() => registry.getOrLoad(badOptions)).toThrow()
148
- })
149
-
150
- it('does not dispose when unusedCacheTime is Infinity', async () => {
151
- vi.useFakeTimers()
152
- const registry = new StoreRegistry()
153
- const options = testStoreOptions({ unusedCacheTime: Number.POSITIVE_INFINITY })
154
-
155
- const store = await registry.getOrLoad(options)
156
-
157
- // Store should be cached
158
- expect(registry.getOrLoad(options)).toBe(store)
159
-
160
- // Advance time by a very long duration
161
- await vi.advanceTimersByTimeAsync(1000000)
162
-
163
- // Store should still be cached (not disposed)
164
- expect(registry.getOrLoad(options)).toBe(store)
165
-
166
- // Clean up manually
167
- await store.shutdownPromise()
168
- })
169
-
170
- it('throws the same error instance on multiple synchronous calls after failure', async () => {
171
- const registry = new StoreRegistry()
172
-
173
- const badOptions = testStoreOptions({
174
- // @ts-expect-error - intentionally passing invalid adapter to trigger error
175
- adapter: null,
176
- })
177
-
178
- // Wait for the first failure
179
- await expect(registry.getOrLoad(badOptions)).rejects.toThrow()
180
-
181
- // Capture the errors from subsequent synchronous calls
182
- let error1: unknown
183
- let error2: unknown
184
-
185
- try {
186
- registry.getOrLoad(badOptions)
187
- } catch (err) {
188
- error1 = err
189
- }
190
-
191
- try {
192
- registry.getOrLoad(badOptions)
193
- } catch (err) {
194
- error2 = err
195
- }
196
-
197
- // Both should be the exact same error instance (cached)
198
- expect(error1).toBeDefined()
199
- expect(error1).toBe(error2)
200
- })
201
-
202
- it('notifies subscribers when store state changes', async () => {
203
- const registry = new StoreRegistry()
204
- const options = testStoreOptions()
205
-
206
- let notificationCount = 0
207
- const listener = () => {
208
- notificationCount++
209
- }
210
-
211
- const unsubscribe = registry.subscribe(options.storeId, listener)
212
-
213
- // Start loading the store
214
- const storePromise = registry.getOrLoad(options)
215
-
216
- // Listener should be called when store starts loading
217
- expect(notificationCount).toBe(1)
218
-
219
- const store = await storePromise
220
-
221
- // Listener should be called when store loads successfully
222
- expect(notificationCount).toBe(2)
223
-
224
- unsubscribe()
225
-
226
- // Clean up
227
- await store.shutdownPromise()
228
- })
229
-
230
- it('handles rapid subscribe/unsubscribe cycles without errors', async () => {
231
- vi.useFakeTimers()
232
- const registry = new StoreRegistry()
233
- const unusedCacheTime = 50
234
- const options = testStoreOptions({ unusedCacheTime })
235
-
236
- const store = await registry.getOrLoad(options)
237
-
238
- // Rapidly subscribe and unsubscribe multiple times
239
- for (let i = 0; i < 10; i++) {
240
- const unsubscribe = registry.subscribe(options.storeId, () => {})
241
- unsubscribe()
242
- }
243
-
244
- // Advance time to check if disposal is scheduled correctly
245
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
246
-
247
- // Store should be disposed after the last unsubscribe
248
- const nextStore = await registry.getOrLoad(options)
249
- expect(nextStore).not.toBe(store)
250
-
251
- await nextStore.shutdownPromise()
252
- })
253
-
254
- it('swallows errors thrown by subscribers during notification', async () => {
255
- const registry = new StoreRegistry()
256
- const options = testStoreOptions()
257
-
258
- let errorListenerCalled = false
259
- let goodListenerCalled = false
260
-
261
- const errorListener = () => {
262
- errorListenerCalled = true
263
- throw new Error('Listener error')
264
- }
265
-
266
- const goodListener = () => {
267
- goodListenerCalled = true
268
- }
269
-
270
- registry.subscribe(options.storeId, errorListener)
271
- registry.subscribe(options.storeId, goodListener)
272
-
273
- // Should not throw despite errorListener throwing
274
- const store = await registry.getOrLoad(options)
275
-
276
- // Both listeners should have been called
277
- expect(errorListenerCalled).toBe(true)
278
- expect(goodListenerCalled).toBe(true)
279
-
280
- await store.shutdownPromise()
281
- })
282
-
283
- it('supports concurrent load and subscribe operations', async () => {
284
- const registry = new StoreRegistry()
285
- const options = testStoreOptions()
286
-
287
- let notificationCount = 0
288
- const listener = () => {
289
- notificationCount++
290
- }
291
-
292
- // Subscribe before loading starts
293
- const unsubscribe = registry.subscribe(options.storeId, listener)
294
-
295
- // Start loading
296
- const storePromise = registry.getOrLoad(options)
297
-
298
- // Listener should be notified when loading starts
299
- expect(notificationCount).toBeGreaterThan(0)
300
-
301
- const store = await storePromise
302
-
303
- // Listener should be notified when loading completes
304
- expect(notificationCount).toBe(2)
305
-
306
- unsubscribe()
307
-
308
- // Clean up
309
- await store.shutdownPromise()
310
- })
311
-
312
- it('cancels disposal when a new subscription is added', async () => {
313
- vi.useFakeTimers()
314
- const registry = new StoreRegistry()
315
- const unusedCacheTime = 50
316
- const options = testStoreOptions({ unusedCacheTime })
317
-
318
- const store = await registry.getOrLoad(options)
319
-
320
- // Advance time almost to disposal threshold
321
- await vi.advanceTimersByTimeAsync(unusedCacheTime - 5)
322
-
323
- // Add a new subscription before disposal triggers
324
- const unsubscribe = registry.subscribe(options.storeId, () => {})
325
-
326
- // Complete the original unusedCacheTime
327
- await vi.advanceTimersByTimeAsync(5)
328
-
329
- // Store should not have been disposed because we added a subscription
330
- expect(registry.getOrLoad(options)).toBe(store)
331
-
332
- // Clean up
333
- unsubscribe()
334
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
335
-
336
- // Now it should be disposed
337
- const nextStore = await registry.getOrLoad(options)
338
- expect(nextStore).not.toBe(store)
339
-
340
- await nextStore.shutdownPromise()
341
- })
342
-
343
- it('schedules disposal if store becomes unused during loading', async () => {
344
- vi.useFakeTimers()
345
- const registry = new StoreRegistry()
346
- const unusedCacheTime = 50
347
- const options = testStoreOptions({ unusedCacheTime })
348
-
349
- // Start loading without any subscription
350
- const storePromise = registry.getOrLoad(options)
351
-
352
- // Wait for store to load (no subscribers registered)
353
- const store = await storePromise
354
-
355
- // Since there were no subscribers when loading completed, disposal should be scheduled
356
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
357
-
358
- // Store should be disposed
359
- const nextStore = await registry.getOrLoad(options)
360
- expect(nextStore).not.toBe(store)
361
-
362
- await nextStore.shutdownPromise()
363
- })
364
-
365
- it('aborts loading when disposal fires while store is still loading', async () => {
366
- vi.useFakeTimers()
367
- const registry = new StoreRegistry()
368
- const unusedCacheTime = 10
369
- const options = testStoreOptions({ unusedCacheTime })
370
-
371
- // Subscribe briefly to trigger getOrLoad and then unsubscribe
372
- const unsubscribe = registry.subscribe(options.storeId, () => {})
373
-
374
- // Start loading - this will be slow due to fake timers
375
- const loadPromise = registry.getOrLoad(options)
376
-
377
- // Attach a catch handler to prevent unhandled rejection when the load is aborted
378
- const abortedPromise = (loadPromise as Promise<unknown>).catch(() => {
379
- // Expected: load was aborted by disposal
380
- })
381
-
382
- // Unsubscribe immediately, which schedules disposal
383
- unsubscribe()
384
-
385
- // Advance time to trigger disposal while still loading
386
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
387
-
388
- // Wait for the abort to complete
389
- await abortedPromise
390
-
391
- // After abort, a new getOrLoad should start a fresh load
392
- const freshLoadPromise = registry.getOrLoad(options)
393
-
394
- // This should be a new promise (not the aborted one)
395
- expect(freshLoadPromise).toBeInstanceOf(Promise)
396
- expect(freshLoadPromise).not.toBe(loadPromise)
397
-
398
- // Wait for fresh load to complete
399
- const store = await freshLoadPromise
400
- expect(store).toBeDefined()
401
-
402
- await store.shutdownPromise()
403
- })
404
-
405
- it('does not abort loading when new subscription arrives before disposal fires', async () => {
406
- vi.useFakeTimers()
407
- const registry = new StoreRegistry()
408
- const unusedCacheTime = 50
409
- const options = testStoreOptions({ unusedCacheTime })
410
-
411
- // Start loading and immediately unsubscribe to schedule disposal
412
- const unsub1 = registry.subscribe(options.storeId, () => {})
413
- const loadPromise = registry.getOrLoad(options)
414
- unsub1()
415
-
416
- // Advance time partially (before disposal fires)
417
- await vi.advanceTimersByTimeAsync(unusedCacheTime - 10)
418
-
419
- // Add a new subscription - this should cancel the pending disposal
420
- const unsub2 = registry.subscribe(options.storeId, () => {})
421
-
422
- // Advance past the original unusedCacheTime
423
- await vi.advanceTimersByTimeAsync(20)
424
-
425
- // The load should complete normally (not be aborted)
426
- const store = await loadPromise
427
-
428
- // And should be the same instance when retrieved again
429
- const cachedStore = registry.getOrLoad(options)
430
- expect(cachedStore).toBe(store)
431
-
432
- unsub2()
433
- await store.shutdownPromise()
434
- })
435
-
436
- it('manages multiple stores with different IDs independently', async () => {
437
- vi.useFakeTimers()
438
- const registry = new StoreRegistry()
439
-
440
- const options1 = testStoreOptions({ storeId: 'store-1', unusedCacheTime: 50 })
441
- const options2 = testStoreOptions({ storeId: 'store-2', unusedCacheTime: 100 })
442
-
443
- const store1 = await registry.getOrLoad(options1)
444
- const store2 = await registry.getOrLoad(options2)
445
-
446
- // Should be different store instances
447
- expect(store1).not.toBe(store2)
448
-
449
- // Both should be cached independently
450
- expect(registry.getOrLoad(options1)).toBe(store1)
451
- expect(registry.getOrLoad(options2)).toBe(store2)
452
-
453
- // Advance time to dispose store1 only
454
- await vi.advanceTimersByTimeAsync(50)
455
-
456
- // store1 should be disposed, store2 should still be cached
457
- const newStore1 = await registry.getOrLoad(options1)
458
- expect(newStore1).not.toBe(store1)
459
- expect(registry.getOrLoad(options2)).toBe(store2)
460
-
461
- // Subscribe to prevent disposal of newStore1
462
- const unsub1 = registry.subscribe(options1.storeId, () => {})
463
-
464
- // Advance remaining time to dispose store2
465
- await vi.advanceTimersByTimeAsync(50)
466
-
467
- // store2 should be disposed
468
- const newStore2 = await registry.getOrLoad(options2)
469
- expect(newStore2).not.toBe(store2)
470
-
471
- // Subscribe to prevent disposal of newStore2
472
- const unsub2 = registry.subscribe(options2.storeId, () => {})
473
-
474
- // Clean up
475
- unsub1()
476
- unsub2()
477
- await newStore1.shutdownPromise()
478
- await newStore2.shutdownPromise()
479
- })
480
-
481
- it('applies default options from constructor', async () => {
482
- vi.useFakeTimers()
483
-
484
- const registry = new StoreRegistry({
485
- defaultOptions: {
486
- unusedCacheTime: DEFAULT_UNUSED_CACHE_TIME * 2,
487
- },
488
- })
489
-
490
- const options = testStoreOptions()
491
-
492
- const store = await registry.getOrLoad(options)
493
-
494
- // Verify the store loads successfully
495
- expect(store).toBeDefined()
496
- expect(store[StoreInternalsSymbol].clientSession.debugInstanceId).toBeDefined()
497
-
498
- // Verify configured default unusedCacheTime is applied by checking disposal doesn't happen at library's default time
499
- await vi.advanceTimersByTimeAsync(DEFAULT_UNUSED_CACHE_TIME)
500
-
501
- // Store should still be cached after default unusedCacheTime
502
- expect(registry.getOrLoad(options)).toBe(store)
503
-
504
- await store.shutdownPromise()
505
- })
506
-
507
- it('allows call-site options to override default options', async () => {
508
- vi.useFakeTimers()
509
-
510
- const registry = new StoreRegistry({
511
- defaultOptions: {
512
- unusedCacheTime: 1000, // Default is long
513
- },
514
- })
515
-
516
- const options = testStoreOptions({
517
- unusedCacheTime: 10, // Override with shorter time
518
- })
519
-
520
- const store = await registry.getOrLoad(options)
521
-
522
- // Advance by the override time (10ms)
523
- await vi.advanceTimersByTimeAsync(10)
524
-
525
- // Should be disposed according to the override time, not default
526
- const nextStore = await registry.getOrLoad(options)
527
- expect(nextStore).not.toBe(store)
528
-
529
- await nextStore.shutdownPromise()
530
- })
531
-
532
- it('prevents subscriptions to stores that are shutting down', async () => {
533
- vi.useFakeTimers()
534
- const registry = new StoreRegistry()
535
- const unusedCacheTime = 10
536
- const options = testStoreOptions({ unusedCacheTime })
537
-
538
- // Load the store and wait for it to be ready
539
- const originalStore = await registry.getOrLoad(options)
540
-
541
- // Verify store is cached
542
- expect(registry.getOrLoad(options)).toBe(originalStore)
543
-
544
- // Spy on shutdownPromise to detect when shutdown starts
545
- let shutdownStarted = false
546
- let shutdownCompleted = false
547
- const originalShutdownPromise = originalStore.shutdownPromise.bind(originalStore)
548
- originalStore.shutdownPromise = () => {
549
- shutdownStarted = true
550
- return originalShutdownPromise().finally(() => {
551
- shutdownCompleted = true
552
- })
553
- }
554
-
555
- // Use vi.advanceTimersToNextTimer to advance ONLY to the disposal timer firing,
556
- // then immediately (before microtasks resolve) try to get the store
557
- vi.advanceTimersToNextTimer()
558
-
559
- // The disposal callback has now executed synchronously, which means:
560
- // 1. Subscriber check passed (no subscribers)
561
- // 2. shutdown() was called (but it's async, hasn't resolved yet)
562
- // 3. Cache entry SHOULD have been removed
563
-
564
- // Verify shutdown was initiated
565
- expect(shutdownStarted).toBe(true)
566
- // Shutdown is async, so it shouldn't have completed yet in the same tick
567
- expect(shutdownCompleted).toBe(false)
568
-
569
- const storeOrPromise = registry.getOrLoad(options)
570
-
571
- if (!(storeOrPromise instanceof Promise)) {
572
- expect.fail('getOrLoad returned dying store synchronously instead of starting fresh load')
573
- }
574
-
575
- const freshStore = await storeOrPromise
576
- // A fresh load was triggered because cache was cleared
577
- expect(freshStore).not.toBe(originalStore)
578
- await freshStore.shutdownPromise()
579
- })
580
-
581
- it('warms the cache so subsequent getOrLoad is synchronous after preload', async () => {
582
- const registry = new StoreRegistry()
583
- const options = testStoreOptions()
584
-
585
- // Preload the store
586
- await registry.preload(options)
587
-
588
- // Subsequent getOrLoad should return synchronously (not a Promise)
589
- const store = registry.getOrLoad(options)
590
- expect(store).not.toBeInstanceOf(Promise)
591
-
592
- // TypeScript doesn't narrow the type, so we need to assert
593
- if (store instanceof Promise) {
594
- throw new Error('Expected store, got Promise')
595
- }
596
-
597
- // Clean up
598
- await store.shutdownPromise()
599
- })
600
-
601
- it('schedules disposal after preload if no subscribers are added', async () => {
602
- vi.useFakeTimers()
603
- const registry = new StoreRegistry()
604
- const unusedCacheTime = 50
605
- const options = testStoreOptions({ unusedCacheTime })
606
-
607
- // Preload without subscribing
608
- await registry.preload(options)
609
-
610
- // Get the store
611
- const store = registry.getOrLoad(options)
612
- expect(store).not.toBeInstanceOf(Promise)
613
-
614
- // Advance time to trigger disposal
615
- await vi.advanceTimersByTimeAsync(unusedCacheTime)
616
-
617
- // Store should be disposed since no subscribers were added
618
- const nextStore = await registry.getOrLoad(options)
619
- expect(nextStore).not.toBe(store)
620
-
621
- await nextStore.shutdownPromise()
622
- })
623
- })
624
-
625
- const testStoreOptions = (overrides: Partial<CachedStoreOptions<typeof schema>> = {}) =>
626
- storeOptions({
627
- storeId: 'test-store',
628
- schema,
629
- adapter: makeInMemoryAdapter(),
630
- ...overrides,
631
- })