@livestore/livestore 0.4.0-dev.9 → 0.5.0-dev.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 (132) hide show
  1. package/README.md +0 -1
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/QueryCache.js +1 -1
  4. package/dist/QueryCache.js.map +1 -1
  5. package/dist/SqliteDbWrapper.d.ts +5 -5
  6. package/dist/SqliteDbWrapper.d.ts.map +1 -1
  7. package/dist/SqliteDbWrapper.js +15 -13
  8. package/dist/SqliteDbWrapper.js.map +1 -1
  9. package/dist/SqliteDbWrapper.test.js +24 -6
  10. package/dist/SqliteDbWrapper.test.js.map +1 -1
  11. package/dist/effect/LiveStore.d.ts +134 -6
  12. package/dist/effect/LiveStore.d.ts.map +1 -1
  13. package/dist/effect/LiveStore.js +190 -11
  14. package/dist/effect/LiveStore.js.map +1 -1
  15. package/dist/effect/LiveStore.test.d.ts +2 -0
  16. package/dist/effect/LiveStore.test.d.ts.map +1 -0
  17. package/dist/effect/LiveStore.test.js +41 -0
  18. package/dist/effect/LiveStore.test.js.map +1 -0
  19. package/dist/effect/mod.d.ts +1 -1
  20. package/dist/effect/mod.d.ts.map +1 -1
  21. package/dist/effect/mod.js +3 -1
  22. package/dist/effect/mod.js.map +1 -1
  23. package/dist/live-queries/base-class.d.ts +129 -9
  24. package/dist/live-queries/base-class.d.ts.map +1 -1
  25. package/dist/live-queries/base-class.js +30 -4
  26. package/dist/live-queries/base-class.js.map +1 -1
  27. package/dist/live-queries/client-document-get-query.d.ts +1 -1
  28. package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
  29. package/dist/live-queries/client-document-get-query.js +4 -3
  30. package/dist/live-queries/client-document-get-query.js.map +1 -1
  31. package/dist/live-queries/computed.d.ts +56 -0
  32. package/dist/live-queries/computed.d.ts.map +1 -1
  33. package/dist/live-queries/computed.js +62 -6
  34. package/dist/live-queries/computed.js.map +1 -1
  35. package/dist/live-queries/db-query.d.ts +2 -2
  36. package/dist/live-queries/db-query.d.ts.map +1 -1
  37. package/dist/live-queries/db-query.js +41 -30
  38. package/dist/live-queries/db-query.js.map +1 -1
  39. package/dist/live-queries/db-query.test.js +112 -29
  40. package/dist/live-queries/db-query.test.js.map +1 -1
  41. package/dist/live-queries/signal.d.ts +49 -0
  42. package/dist/live-queries/signal.d.ts.map +1 -1
  43. package/dist/live-queries/signal.js +51 -2
  44. package/dist/live-queries/signal.js.map +1 -1
  45. package/dist/live-queries/signal.test.js +4 -4
  46. package/dist/live-queries/signal.test.js.map +1 -1
  47. package/dist/mod.d.ts +3 -3
  48. package/dist/mod.d.ts.map +1 -1
  49. package/dist/mod.js +3 -2
  50. package/dist/mod.js.map +1 -1
  51. package/dist/reactive.d.ts +10 -10
  52. package/dist/reactive.d.ts.map +1 -1
  53. package/dist/reactive.js +29 -27
  54. package/dist/reactive.js.map +1 -1
  55. package/dist/reactive.test.js +2 -2
  56. package/dist/reactive.test.js.map +1 -1
  57. package/dist/store/StoreRegistry.d.ts +215 -0
  58. package/dist/store/StoreRegistry.d.ts.map +1 -0
  59. package/dist/store/StoreRegistry.js +267 -0
  60. package/dist/store/StoreRegistry.js.map +1 -0
  61. package/dist/store/StoreRegistry.test.d.ts +2 -0
  62. package/dist/store/StoreRegistry.test.d.ts.map +1 -0
  63. package/dist/store/StoreRegistry.test.js +384 -0
  64. package/dist/store/StoreRegistry.test.js.map +1 -0
  65. package/dist/store/create-store.d.ts +99 -22
  66. package/dist/store/create-store.d.ts.map +1 -1
  67. package/dist/store/create-store.js +85 -37
  68. package/dist/store/create-store.js.map +1 -1
  69. package/dist/store/devtools.d.ts +6 -18
  70. package/dist/store/devtools.d.ts.map +1 -1
  71. package/dist/store/devtools.js +61 -19
  72. package/dist/store/devtools.js.map +1 -1
  73. package/dist/store/store-eventstream.test.d.ts +2 -0
  74. package/dist/store/store-eventstream.test.d.ts.map +1 -0
  75. package/dist/store/store-eventstream.test.js +65 -0
  76. package/dist/store/store-eventstream.test.js.map +1 -0
  77. package/dist/store/store-types.d.ts +271 -31
  78. package/dist/store/store-types.d.ts.map +1 -1
  79. package/dist/store/store-types.js +41 -1
  80. package/dist/store/store-types.js.map +1 -1
  81. package/dist/store/store-types.test.d.ts +2 -0
  82. package/dist/store/store-types.test.d.ts.map +1 -0
  83. package/dist/store/store-types.test.js +39 -0
  84. package/dist/store/store-types.test.js.map +1 -0
  85. package/dist/store/store.d.ts +255 -68
  86. package/dist/store/store.d.ts.map +1 -1
  87. package/dist/store/store.js +533 -186
  88. package/dist/store/store.js.map +1 -1
  89. package/dist/utils/dev.d.ts.map +1 -1
  90. package/dist/utils/dev.js +1 -1
  91. package/dist/utils/dev.js.map +1 -1
  92. package/dist/utils/stack-info.d.ts.map +1 -1
  93. package/dist/utils/stack-info.js +3 -2
  94. package/dist/utils/stack-info.js.map +1 -1
  95. package/dist/utils/tests/fixture.d.ts +46 -39
  96. package/dist/utils/tests/fixture.d.ts.map +1 -1
  97. package/dist/utils/tests/fixture.js +7 -0
  98. package/dist/utils/tests/fixture.js.map +1 -1
  99. package/dist/utils/tests/otel.d.ts.map +1 -1
  100. package/dist/utils/tests/otel.js +5 -5
  101. package/dist/utils/tests/otel.js.map +1 -1
  102. package/package.json +50 -18
  103. package/src/QueryCache.ts +1 -1
  104. package/src/SqliteDbWrapper.test.ts +30 -6
  105. package/src/SqliteDbWrapper.ts +21 -17
  106. package/src/ambient.d.ts +0 -7
  107. package/src/effect/LiveStore.test.ts +61 -0
  108. package/src/effect/LiveStore.ts +426 -22
  109. package/src/effect/mod.ts +13 -1
  110. package/src/live-queries/__snapshots__/db-query.test.ts.snap +814 -178
  111. package/src/live-queries/base-class.ts +162 -32
  112. package/src/live-queries/client-document-get-query.ts +6 -4
  113. package/src/live-queries/computed.ts +65 -8
  114. package/src/live-queries/db-query.test.ts +168 -30
  115. package/src/live-queries/db-query.ts +58 -43
  116. package/src/live-queries/signal.test.ts +5 -4
  117. package/src/live-queries/signal.ts +52 -3
  118. package/src/mod.ts +19 -2
  119. package/src/reactive.test.ts +3 -2
  120. package/src/reactive.ts +58 -49
  121. package/src/store/StoreRegistry.test.ts +543 -0
  122. package/src/store/StoreRegistry.ts +429 -0
  123. package/src/store/create-store.ts +227 -74
  124. package/src/store/devtools.ts +305 -261
  125. package/src/store/store-eventstream.test.ts +123 -0
  126. package/src/store/store-types.test.ts +52 -0
  127. package/src/store/store-types.ts +318 -44
  128. package/src/store/store.ts +695 -283
  129. package/src/utils/dev.ts +3 -4
  130. package/src/utils/stack-info.ts +5 -2
  131. package/src/utils/tests/fixture.ts +9 -1
  132. package/src/utils/tests/otel.ts +8 -7
@@ -0,0 +1,543 @@
1
+ import { describe, expect, it } from '@effect/vitest'
2
+
3
+ import { makeInMemoryAdapter } from '@livestore/adapter-web'
4
+ import { OtelLiveDummy, UnknownError } from '@livestore/common'
5
+ import { Effect, Fiber, type OtelTracer, type Scope, TestClock } from '@livestore/utils/effect'
6
+
7
+ import { schema } from '../utils/tests/fixture.ts'
8
+ import { StoreInternalsSymbol } from './store-types.ts'
9
+ import { type RegistryStoreOptions, StoreRegistry, storeOptions } from './StoreRegistry.ts'
10
+
11
+ describe('StoreRegistry', () => {
12
+ it('returns a promise when the store is loading', async () => {
13
+ const storeRegistry = new StoreRegistry()
14
+ const result = storeRegistry.getOrLoadPromise(testStoreOptions())
15
+
16
+ expect(result).toBeInstanceOf(Promise)
17
+
18
+ // Clean up
19
+ const store = await result
20
+ await store.shutdownPromise()
21
+ })
22
+
23
+ it('returns cached store synchronously after first load resolves', async () => {
24
+ const storeRegistry = new StoreRegistry()
25
+
26
+ const initial = storeRegistry.getOrLoadPromise(testStoreOptions())
27
+ expect(initial).toBeInstanceOf(Promise)
28
+
29
+ const store = await initial
30
+
31
+ const cached = storeRegistry.getOrLoadPromise(testStoreOptions())
32
+ expect(cached).toBe(store)
33
+ expect(cached).not.toBeInstanceOf(Promise)
34
+
35
+ // Clean up
36
+ await store.shutdownPromise()
37
+ })
38
+
39
+ it('reuses the same promise for concurrent getOrLoadPromise calls while loading', async () => {
40
+ const storeRegistry = new StoreRegistry()
41
+ const options = testStoreOptions()
42
+
43
+ const first = storeRegistry.getOrLoadPromise(options)
44
+ const second = storeRegistry.getOrLoadPromise(options)
45
+
46
+ // Both should be the same promise
47
+ expect(first).toBe(second)
48
+ expect(first).toBeInstanceOf(Promise)
49
+
50
+ const store = await first
51
+
52
+ // Both promises should resolve to the same store
53
+ expect(await second).toBe(store)
54
+
55
+ // Clean up
56
+ await store.shutdownPromise()
57
+ })
58
+
59
+ it('throws synchronously and rethrows on subsequent calls for sync failures', () => {
60
+ const storeRegistry = new StoreRegistry()
61
+
62
+ const badOptions = testStoreOptions({
63
+ // @ts-expect-error - intentionally passing invalid adapter to trigger error
64
+ adapter: null,
65
+ })
66
+
67
+ // First call throws synchronously
68
+ expect(() => storeRegistry.getOrLoadPromise(badOptions)).toThrow()
69
+
70
+ // Subsequent call should also throw synchronously (cached error)
71
+ expect(() => storeRegistry.getOrLoadPromise(badOptions)).toThrow()
72
+ })
73
+
74
+ it('caches and rethrows rejection on subsequent calls for async failures', async () => {
75
+ const storeRegistry = new StoreRegistry()
76
+
77
+ // Create an adapter that fails asynchronously (after yielding to the event loop)
78
+ const failingAdapter = () =>
79
+ Effect.gen(function* () {
80
+ yield* Effect.sleep(0) // Force async execution
81
+ return yield* UnknownError.make({ cause: new Error('Async failure') })
82
+ })
83
+ const badOptions = testStoreOptions({
84
+ adapter: failingAdapter,
85
+ })
86
+
87
+ // First call returns a promise that rejects
88
+ await expect(storeRegistry.getOrLoadPromise(badOptions)).rejects.toThrow()
89
+
90
+ // Subsequent call should throw the cached error synchronously (RcMap caches failures)
91
+ expect(() => storeRegistry.getOrLoadPromise(badOptions)).toThrow()
92
+ })
93
+
94
+ it('throws the same error instance on multiple calls after failure', async () => {
95
+ const storeRegistry = new StoreRegistry()
96
+
97
+ // Create an adapter that fails asynchronously
98
+ const failingAdapter = () =>
99
+ Effect.gen(function* () {
100
+ yield* Effect.sleep(0) // Force async execution
101
+ return yield* UnknownError.make({ cause: new Error('Async failure') })
102
+ })
103
+
104
+ const badOptions = testStoreOptions({
105
+ adapter: failingAdapter,
106
+ })
107
+
108
+ // Wait for the first failure
109
+ await expect(storeRegistry.getOrLoadPromise(badOptions)).rejects.toThrow()
110
+
111
+ // Capture the errors from subsequent calls
112
+ let error1: unknown
113
+ let error2: unknown
114
+
115
+ try {
116
+ storeRegistry.getOrLoadPromise(badOptions)
117
+ } catch (err) {
118
+ error1 = err
119
+ }
120
+
121
+ try {
122
+ storeRegistry.getOrLoadPromise(badOptions)
123
+ } catch (err) {
124
+ error2 = err
125
+ }
126
+
127
+ // Both should be the exact same error instance (cached)
128
+ expect(error1).toBeDefined()
129
+ expect(error1).toBe(error2)
130
+ })
131
+
132
+ it('preload does not throw', async () => {
133
+ const storeRegistry = new StoreRegistry()
134
+
135
+ // Create invalid options that would cause an error
136
+ const badOptions = testStoreOptions({
137
+ // @ts-expect-error - intentionally passing invalid adapter to trigger error
138
+ adapter: null,
139
+ })
140
+
141
+ // preload should not throw
142
+ await expect(storeRegistry.preload(badOptions)).resolves.toBeUndefined()
143
+
144
+ // But subsequent getOrLoadStore should throw the cached error
145
+ expect(() => storeRegistry.getOrLoadPromise(badOptions)).toThrow()
146
+ })
147
+
148
+ it('warms the cache so subsequent getOrLoadStore is synchronous after preload', async () => {
149
+ const storeRegistry = new StoreRegistry()
150
+ const options = testStoreOptions()
151
+
152
+ // Preload the store
153
+ await storeRegistry.preload(options)
154
+
155
+ // Subsequent getOrLoadStore should return synchronously (not a Promise)
156
+ const store = storeRegistry.getOrLoadPromise(options)
157
+ expect(store).not.toBeInstanceOf(Promise)
158
+
159
+ // TypeScript doesn't narrow the type, so we need to assert
160
+ if (store instanceof Promise) {
161
+ throw new Error('Expected store, got Promise')
162
+ }
163
+
164
+ // Clean up
165
+ await store.shutdownPromise()
166
+ })
167
+
168
+ it.layer(OtelLiveDummy)('time-dependent (using TestClock)', (it) => {
169
+ it.effect('disposes store after unusedCacheTime expires', () =>
170
+ Effect.gen(function* () {
171
+ const unusedCacheTime = 25
172
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
173
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
174
+ const options = testStoreOptions()
175
+
176
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
177
+
178
+ // Store should still be in cache
179
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
180
+ expect(cached).toBe(store)
181
+
182
+ // Let the idle timer fiber register its sleep with TestClock
183
+ yield* Effect.yieldNow
184
+
185
+ // Advance time past unusedCacheTime → idle timer fires → entry evicted
186
+ yield* TestClock.adjust(unusedCacheTime)
187
+
188
+ // After eviction, a new load should produce a different store
189
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
190
+ expect(nextStore).not.toBe(store)
191
+ expect(nextStore[StoreInternalsSymbol].clientSession.debugInstanceId).toBeDefined()
192
+ }),
193
+ )
194
+
195
+ it.effect('does not dispose when unusedCacheTime is Infinity', () =>
196
+ Effect.gen(function* () {
197
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
198
+ const registry = new StoreRegistry({
199
+ context: services,
200
+ defaultOptions: { unusedCacheTime: Number.POSITIVE_INFINITY },
201
+ })
202
+ const options = testStoreOptions()
203
+
204
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
205
+
206
+ // Advance a large amount of time — no idle timer was started for Infinity
207
+ yield* TestClock.adjust(100_000)
208
+
209
+ // Store should still be cached
210
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
211
+ expect(cached).toBe(store)
212
+ }),
213
+ )
214
+
215
+ it.effect('schedules disposal if store becomes unused during loading', () =>
216
+ Effect.gen(function* () {
217
+ const unusedCacheTime = 50
218
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
219
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
220
+ const options = testStoreOptions()
221
+
222
+ // Load without retaining — disposal is scheduled when scope closes
223
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
224
+
225
+ yield* Effect.yieldNow
226
+ yield* TestClock.adjust(unusedCacheTime)
227
+
228
+ // Store should be disposed
229
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
230
+ expect(nextStore).not.toBe(store)
231
+ }),
232
+ )
233
+
234
+ it.effect('allows call-site options to override default options', () =>
235
+ Effect.gen(function* () {
236
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
237
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime: 10_000 } }) // Long default
238
+
239
+ const unusedCacheTimeOverride = 25
240
+ const options = testStoreOptions({ unusedCacheTime: unusedCacheTimeOverride })
241
+
242
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
243
+
244
+ yield* Effect.yieldNow
245
+ yield* TestClock.adjust(unusedCacheTimeOverride)
246
+
247
+ // Should be disposed according to the override time, not default
248
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
249
+ expect(nextStore).not.toBe(store)
250
+ }),
251
+ )
252
+
253
+ it.effect('disposes different stores according to their own unusedCacheTime', () =>
254
+ Effect.gen(function* () {
255
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
256
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime: 1000 } })
257
+
258
+ const shortOptions = testStoreOptions({ storeId: 'short-lived', unusedCacheTime: 25 })
259
+ const longOptions = testStoreOptions({ storeId: 'long-lived', unusedCacheTime: 10_000 })
260
+
261
+ const shortStore = yield* registry.getOrLoad(shortOptions).pipe(Effect.scoped)
262
+ const longStore = yield* registry.getOrLoad(longOptions).pipe(Effect.scoped)
263
+
264
+ yield* Effect.yieldNow
265
+
266
+ // Advance past short store's unusedCacheTime only
267
+ yield* TestClock.adjust(25)
268
+
269
+ // Short store should be disposed, long store should still be cached
270
+ const nextShortStore = yield* registry.getOrLoad(shortOptions).pipe(Effect.scoped)
271
+ expect(nextShortStore).not.toBe(shortStore)
272
+
273
+ const cachedLongStore = yield* registry.getOrLoad(longOptions).pipe(Effect.scoped)
274
+ expect(cachedLongStore).toBe(longStore)
275
+ }),
276
+ )
277
+
278
+ // This test is skipped because we don't yet support dynamic `unusedCacheTime` updates for cached stores.
279
+ // See https://github.com/livestorejs/livestore/issues/918
280
+ it.effect.skip('keeps the longest unusedCacheTime seen for a store when options vary across calls', () =>
281
+ Effect.gen(function* () {
282
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
283
+ const registry = new StoreRegistry({ context: services })
284
+
285
+ const options = testStoreOptions({ unusedCacheTime: 10 })
286
+ const release = registry.retain(options)
287
+
288
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
289
+
290
+ // Call with longer unusedCacheTime
291
+ yield* registry.getOrLoad(testStoreOptions({ unusedCacheTime: 100 })).pipe(Effect.scoped)
292
+
293
+ release()
294
+ yield* Effect.yieldNow
295
+
296
+ // After 99ms, store should still be alive (100ms unusedCacheTime used)
297
+ yield* TestClock.adjust(99)
298
+
299
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
300
+ expect(cached).toBe(store)
301
+
302
+ // After 1 more ms, store should be disposed
303
+ yield* TestClock.adjust(1)
304
+
305
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
306
+ expect(nextStore).not.toBe(store)
307
+ }),
308
+ )
309
+
310
+ it.effect('handles rapid retain/release cycles without errors', () =>
311
+ Effect.gen(function* () {
312
+ const unusedCacheTime = 50
313
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
314
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
315
+ const options = testStoreOptions()
316
+
317
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
318
+
319
+ // Rapidly retain and release multiple times
320
+ for (let i = 0; i < 10; i++) {
321
+ const release = registry.retain(options)
322
+ release()
323
+ }
324
+
325
+ yield* Effect.yieldNow
326
+ yield* TestClock.adjust(unusedCacheTime)
327
+
328
+ // Store should be disposed after the last release
329
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
330
+ expect(nextStore).not.toBe(store)
331
+ }),
332
+ )
333
+
334
+ it.effect('cancels disposal when new retain', () =>
335
+ Effect.gen(function* () {
336
+ const unusedCacheTime = 50
337
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
338
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
339
+ const options = testStoreOptions()
340
+
341
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
342
+
343
+ yield* Effect.yieldNow
344
+
345
+ // Advance almost to disposal threshold
346
+ yield* TestClock.adjust(unusedCacheTime - 5)
347
+
348
+ // Add a new retain before disposal triggers
349
+ const release = registry.retain(options)
350
+
351
+ // Complete the original unusedCacheTime
352
+ yield* TestClock.adjust(5)
353
+
354
+ // Store should not have been disposed because retain keeps it alive
355
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
356
+ expect(cached).toBe(store)
357
+
358
+ // Release retain — new idle timer starts
359
+ release()
360
+ yield* Effect.yieldNow
361
+
362
+ yield* TestClock.adjust(unusedCacheTime)
363
+
364
+ // Now it should be disposed
365
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
366
+ expect(nextStore).not.toBe(store)
367
+ }),
368
+ )
369
+
370
+ it.effect('aborts loading when disposal fires while store is still loading', () =>
371
+ Effect.gen(function* () {
372
+ const unusedCacheTime = 10
373
+ const loadDelay = 1000
374
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
375
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
376
+
377
+ // Adapter that takes time to load (controlled by TestClock)
378
+ const baseAdapter = makeInMemoryAdapter()
379
+ const options = testStoreOptions({
380
+ adapter: ((args: any) =>
381
+ Effect.gen(function* () {
382
+ yield* Effect.sleep(loadDelay)
383
+ return yield* baseAdapter(args)
384
+ })) as any,
385
+ })
386
+
387
+ // Retain triggers loading (won't complete until clock advances past loadDelay)
388
+ const release = registry.retain(options)
389
+ yield* Effect.yieldNow
390
+
391
+ // Release immediately — schedules disposal after unusedCacheTime
392
+ release()
393
+ yield* Effect.yieldNow
394
+
395
+ // Advance past unusedCacheTime but NOT past loadDelay → disposal fires, interrupts loading
396
+ yield* TestClock.adjust(unusedCacheTime)
397
+
398
+ // Start a fresh load — since the first was aborted, this should be a new entry
399
+ const freshLoadFiber = yield* Effect.forkChild(registry.getOrLoad(options).pipe(Effect.scoped))
400
+ yield* Effect.yieldNow
401
+
402
+ // Advance enough for the fresh load to complete
403
+ yield* TestClock.adjust(loadDelay)
404
+ const store = yield* Fiber.join(freshLoadFiber)
405
+
406
+ expect(store).toBeDefined()
407
+ }),
408
+ )
409
+
410
+ it.effect('retain keeps store alive past unusedCacheTime', () =>
411
+ Effect.gen(function* () {
412
+ const unusedCacheTime = 50
413
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
414
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
415
+ const options = testStoreOptions()
416
+
417
+ // Load the store
418
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
419
+
420
+ // Retain the store before disposal could fire
421
+ const release = registry.retain(options)
422
+
423
+ yield* Effect.yieldNow
424
+
425
+ // Advance past unusedCacheTime — idle timer fires but refCount > 0, so no eviction
426
+ yield* TestClock.adjust(unusedCacheTime + 50)
427
+
428
+ // Store should still be cached because retain keeps it alive
429
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
430
+ expect(cached).toBe(store)
431
+
432
+ release()
433
+ }),
434
+ )
435
+
436
+ it.effect('manages multiple stores with different IDs independently', () =>
437
+ Effect.gen(function* () {
438
+ const unusedCacheTime = 50
439
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
440
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
441
+
442
+ const options1 = testStoreOptions({ storeId: 'store-1' })
443
+ const options2 = testStoreOptions({ storeId: 'store-2' })
444
+
445
+ const store1 = yield* registry.getOrLoad(options1).pipe(Effect.scoped)
446
+ const store2 = yield* registry.getOrLoad(options2).pipe(Effect.scoped)
447
+
448
+ // Should be different store instances
449
+ expect(store1).not.toBe(store2)
450
+
451
+ // Both should be cached independently
452
+ const cached1 = yield* registry.getOrLoad(options1).pipe(Effect.scoped)
453
+ const cached2 = yield* registry.getOrLoad(options2).pipe(Effect.scoped)
454
+ expect(cached1).toBe(store1)
455
+ expect(cached2).toBe(store2)
456
+
457
+ yield* Effect.yieldNow
458
+ yield* TestClock.adjust(unusedCacheTime)
459
+
460
+ // Both stores should be disposed
461
+ const newStore1 = yield* registry.getOrLoad(options1).pipe(Effect.scoped)
462
+ const newStore2 = yield* registry.getOrLoad(options2).pipe(Effect.scoped)
463
+ expect(newStore1).not.toBe(store1)
464
+ expect(newStore2).not.toBe(store2)
465
+ }),
466
+ )
467
+
468
+ it.effect('applies default options from constructor', () =>
469
+ Effect.gen(function* () {
470
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
471
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime: 100 } })
472
+ const options = testStoreOptions()
473
+
474
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
475
+
476
+ // Verify the store loads successfully
477
+ expect(store).toBeDefined()
478
+ expect(store[StoreInternalsSymbol].clientSession.debugInstanceId).toBeDefined()
479
+
480
+ yield* Effect.yieldNow
481
+
482
+ // After 50ms, store should still be cached (default is 100ms)
483
+ yield* TestClock.adjust(50)
484
+
485
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
486
+ expect(cached).toBe(store)
487
+ }),
488
+ )
489
+
490
+ it.effect('does not serve a disposed store from cache', () =>
491
+ Effect.gen(function* () {
492
+ const unusedCacheTime = 25
493
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
494
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
495
+ const options = testStoreOptions()
496
+
497
+ const originalStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
498
+
499
+ // Verify store is cached
500
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
501
+ expect(cached).toBe(originalStore)
502
+
503
+ yield* Effect.yieldNow
504
+ yield* TestClock.adjust(unusedCacheTime)
505
+
506
+ // After disposal, calling getOrLoad should produce a fresh store
507
+ const freshStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
508
+ expect(freshStore).not.toBe(originalStore)
509
+ }),
510
+ )
511
+
512
+ it.effect('schedules disposal after preload if no retainers are added', () =>
513
+ Effect.gen(function* () {
514
+ const unusedCacheTime = 50
515
+ const services = yield* Effect.context<Scope.Scope | OtelTracer.OtelTracer>()
516
+ const registry = new StoreRegistry({ context: services, defaultOptions: { unusedCacheTime } })
517
+ const options = testStoreOptions()
518
+
519
+ // Preload without retaining (load + immediate release)
520
+ const store = yield* registry.getOrLoad(options).pipe(Effect.scoped)
521
+
522
+ // Verify it's cached
523
+ const cached = yield* registry.getOrLoad(options).pipe(Effect.scoped)
524
+ expect(cached).toBe(store)
525
+
526
+ yield* Effect.yieldNow
527
+ yield* TestClock.adjust(unusedCacheTime)
528
+
529
+ // Store should be disposed since no retainers were added
530
+ const nextStore = yield* registry.getOrLoad(options).pipe(Effect.scoped)
531
+ expect(nextStore).not.toBe(store)
532
+ }),
533
+ )
534
+ })
535
+ })
536
+
537
+ const testStoreOptions = (overrides: Partial<RegistryStoreOptions<typeof schema>> = {}) =>
538
+ storeOptions({
539
+ storeId: 'test-store',
540
+ schema,
541
+ adapter: makeInMemoryAdapter(),
542
+ ...overrides,
543
+ })