@livestore/livestore 0.4.0-dev.8 → 0.4.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.
- package/README.md +0 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/QueryCache.js +1 -1
- package/dist/QueryCache.js.map +1 -1
- package/dist/SqliteDbWrapper.d.ts +5 -5
- package/dist/SqliteDbWrapper.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.js +8 -8
- package/dist/SqliteDbWrapper.js.map +1 -1
- package/dist/SqliteDbWrapper.test.js +4 -3
- package/dist/SqliteDbWrapper.test.js.map +1 -1
- package/dist/effect/LiveStore.d.ts +133 -5
- package/dist/effect/LiveStore.d.ts.map +1 -1
- package/dist/effect/LiveStore.js +187 -8
- package/dist/effect/LiveStore.js.map +1 -1
- package/dist/effect/LiveStore.test.d.ts +2 -0
- package/dist/effect/LiveStore.test.d.ts.map +1 -0
- package/dist/effect/LiveStore.test.js +42 -0
- package/dist/effect/LiveStore.test.js.map +1 -0
- package/dist/effect/mod.d.ts +1 -1
- package/dist/effect/mod.d.ts.map +1 -1
- package/dist/effect/mod.js +3 -1
- package/dist/effect/mod.js.map +1 -1
- package/dist/live-queries/base-class.d.ts +110 -7
- package/dist/live-queries/base-class.d.ts.map +1 -1
- package/dist/live-queries/base-class.js +2 -2
- package/dist/live-queries/base-class.js.map +1 -1
- package/dist/live-queries/client-document-get-query.d.ts +1 -1
- package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
- package/dist/live-queries/client-document-get-query.js +4 -3
- package/dist/live-queries/client-document-get-query.js.map +1 -1
- package/dist/live-queries/computed.d.ts +56 -0
- package/dist/live-queries/computed.d.ts.map +1 -1
- package/dist/live-queries/computed.js +58 -2
- package/dist/live-queries/computed.js.map +1 -1
- package/dist/live-queries/db-query.d.ts.map +1 -1
- package/dist/live-queries/db-query.js +21 -19
- package/dist/live-queries/db-query.js.map +1 -1
- package/dist/live-queries/db-query.test.js +106 -23
- package/dist/live-queries/db-query.test.js.map +1 -1
- package/dist/live-queries/signal.d.ts +49 -0
- package/dist/live-queries/signal.d.ts.map +1 -1
- package/dist/live-queries/signal.js +49 -0
- package/dist/live-queries/signal.js.map +1 -1
- package/dist/live-queries/signal.test.js +2 -2
- package/dist/live-queries/signal.test.js.map +1 -1
- package/dist/mod.d.ts +3 -3
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +3 -2
- package/dist/mod.js.map +1 -1
- package/dist/reactive.d.ts +9 -9
- package/dist/reactive.d.ts.map +1 -1
- package/dist/reactive.js +9 -26
- package/dist/reactive.js.map +1 -1
- package/dist/reactive.test.js +2 -2
- package/dist/reactive.test.js.map +1 -1
- package/dist/store/StoreRegistry.d.ts +215 -0
- package/dist/store/StoreRegistry.d.ts.map +1 -0
- package/dist/store/StoreRegistry.js +267 -0
- package/dist/store/StoreRegistry.js.map +1 -0
- package/dist/store/StoreRegistry.test.d.ts +2 -0
- package/dist/store/StoreRegistry.test.d.ts.map +1 -0
- package/dist/store/StoreRegistry.test.js +381 -0
- package/dist/store/StoreRegistry.test.js.map +1 -0
- package/dist/store/create-store.d.ts +98 -18
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/create-store.js +49 -20
- package/dist/store/create-store.js.map +1 -1
- package/dist/store/devtools.d.ts +5 -16
- package/dist/store/devtools.d.ts.map +1 -1
- package/dist/store/devtools.js +59 -18
- package/dist/store/devtools.js.map +1 -1
- package/dist/store/store-eventstream.test.d.ts +2 -0
- package/dist/store/store-eventstream.test.d.ts.map +1 -0
- package/dist/store/store-eventstream.test.js +65 -0
- package/dist/store/store-eventstream.test.js.map +1 -0
- package/dist/store/store-types.d.ts +285 -27
- package/dist/store/store-types.d.ts.map +1 -1
- package/dist/store/store-types.js +77 -1
- package/dist/store/store-types.js.map +1 -1
- package/dist/store/store-types.test.d.ts +2 -0
- package/dist/store/store-types.test.d.ts.map +1 -0
- package/dist/store/store-types.test.js +39 -0
- package/dist/store/store-types.test.js.map +1 -0
- package/dist/store/store.d.ts +253 -66
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +442 -153
- package/dist/store/store.js.map +1 -1
- package/dist/utils/dev.d.ts.map +1 -1
- package/dist/utils/dev.js.map +1 -1
- package/dist/utils/stack-info.js +2 -2
- package/dist/utils/stack-info.js.map +1 -1
- package/dist/utils/tests/fixture.d.ts +20 -5
- package/dist/utils/tests/fixture.d.ts.map +1 -1
- package/dist/utils/tests/fixture.js +7 -0
- package/dist/utils/tests/fixture.js.map +1 -1
- package/dist/utils/tests/otel.d.ts.map +1 -1
- package/dist/utils/tests/otel.js +5 -5
- package/dist/utils/tests/otel.js.map +1 -1
- package/package.json +59 -17
- package/src/QueryCache.ts +1 -1
- package/src/SqliteDbWrapper.test.ts +5 -3
- package/src/SqliteDbWrapper.ts +12 -11
- package/src/ambient.d.ts +0 -7
- package/src/effect/LiveStore.test.ts +61 -0
- package/src/effect/LiveStore.ts +388 -13
- package/src/effect/mod.ts +13 -1
- package/src/live-queries/__snapshots__/db-query.test.ts.snap +604 -192
- package/src/live-queries/base-class.ts +126 -28
- package/src/live-queries/client-document-get-query.ts +6 -4
- package/src/live-queries/computed.ts +59 -2
- package/src/live-queries/db-query.test.ts +162 -24
- package/src/live-queries/db-query.ts +23 -20
- package/src/live-queries/signal.test.ts +3 -2
- package/src/live-queries/signal.ts +49 -0
- package/src/mod.ts +19 -2
- package/src/reactive.test.ts +3 -2
- package/src/reactive.ts +22 -23
- package/src/store/StoreRegistry.test.ts +540 -0
- package/src/store/StoreRegistry.ts +418 -0
- package/src/store/create-store.ts +158 -39
- package/src/store/devtools.ts +77 -33
- package/src/store/store-eventstream.test.ts +114 -0
- package/src/store/store-types.test.ts +52 -0
- package/src/store/store-types.ts +360 -40
- package/src/store/store.ts +571 -236
- package/src/utils/dev.ts +2 -3
- package/src/utils/stack-info.ts +2 -2
- package/src/utils/tests/fixture.ts +9 -1
- package/src/utils/tests/otel.ts +8 -7
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { LogConfig, OtelLiveDummy, provideOtel, UnknownError } from '@livestore/common'
|
|
2
|
+
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
3
|
+
import { omitUndefineds } from '@livestore/utils'
|
|
4
|
+
import {
|
|
5
|
+
Cause,
|
|
6
|
+
Effect,
|
|
7
|
+
Equal,
|
|
8
|
+
Exit,
|
|
9
|
+
Fiber,
|
|
10
|
+
Hash,
|
|
11
|
+
Layer,
|
|
12
|
+
ManagedRuntime,
|
|
13
|
+
type OtelTracer,
|
|
14
|
+
RcMap,
|
|
15
|
+
Runtime,
|
|
16
|
+
type Schema,
|
|
17
|
+
type Scope,
|
|
18
|
+
} from '@livestore/utils/effect'
|
|
19
|
+
|
|
20
|
+
import { createStore, type CreateStoreOptions } from './create-store.ts'
|
|
21
|
+
import type { Store } from './store.ts'
|
|
22
|
+
import type { OtelOptions } from './store-types.ts'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default time to keep unused stores in cache.
|
|
26
|
+
*
|
|
27
|
+
* - Browser: 60 seconds (60,000 ms)
|
|
28
|
+
* - SSR: Infinity (disables disposal to avoid disposing stores before server render completes)
|
|
29
|
+
*
|
|
30
|
+
* @internal Exported primarily for testing purposes.
|
|
31
|
+
*/
|
|
32
|
+
export const DEFAULT_UNUSED_CACHE_TIME = typeof window === 'undefined' ? Number.POSITIVE_INFINITY : 60_000
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Configuration options for stores managed by a {@link StoreRegistry}.
|
|
36
|
+
*
|
|
37
|
+
* Extends {@link CreateStoreOptions} with registry-specific settings for caching and observability.
|
|
38
|
+
* Use with {@link storeOptions} helper to get full type inference when defining reusable store configurations.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam TSchema - The LiveStore schema type
|
|
41
|
+
* @typeParam TContext - User-defined context attached to the store
|
|
42
|
+
* @typeParam TSyncPayloadSchema - Schema for the sync payload sent to the backend
|
|
43
|
+
*
|
|
44
|
+
* @see {@link storeOptions} for defining reusable store configurations
|
|
45
|
+
* @see {@link StoreRegistry} for managing store lifecycles
|
|
46
|
+
*/
|
|
47
|
+
export interface RegistryStoreOptions<
|
|
48
|
+
TSchema extends LiveStoreSchema = LiveStoreSchema.Any,
|
|
49
|
+
TContext = {},
|
|
50
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
51
|
+
> extends CreateStoreOptions<TSchema, TContext, TSyncPayloadSchema> {
|
|
52
|
+
/**
|
|
53
|
+
* OpenTelemetry configuration for tracing store operations.
|
|
54
|
+
*
|
|
55
|
+
* When provided, store operations (boot, queries, commits) will be traced
|
|
56
|
+
* under the given root span context using the specified tracer.
|
|
57
|
+
*/
|
|
58
|
+
otelOptions?: Partial<OtelOptions>
|
|
59
|
+
/**
|
|
60
|
+
* The time in milliseconds that this store should remain
|
|
61
|
+
* in memory after becoming unused. When this store becomes
|
|
62
|
+
* unused (no active retentions), it will be disposed after this duration.
|
|
63
|
+
*
|
|
64
|
+
* Stores transition to the unused state as soon as they have no
|
|
65
|
+
* active retentions, so when all components which use that store
|
|
66
|
+
* have unmounted.
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* - Per-store values override the registry-level default (set via `StoreRegistry` constructor's
|
|
70
|
+
* `defaultOptions.unusedCacheTime`)
|
|
71
|
+
* - The value is fixed when the store is first loaded into the registry. If the same `storeId` is
|
|
72
|
+
* requested again with a different `unusedCacheTime`, the original value is kept.
|
|
73
|
+
* - If set to `Infinity`, will disable automatic disposal
|
|
74
|
+
* - The maximum allowed time is about {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value | 24 days}
|
|
75
|
+
*
|
|
76
|
+
* @defaultValue `60_000` (60 seconds) or `Infinity` during SSR to avoid
|
|
77
|
+
* disposing stores before server render completes.
|
|
78
|
+
*/
|
|
79
|
+
unusedCacheTime?: number
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type StoreRegistryConfig = {
|
|
83
|
+
/**
|
|
84
|
+
* Default options that are applied to all stores when they are loaded.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* These are options that typically don't depend on the specific store being loaded:
|
|
88
|
+
* - Framework integration (`batchUpdates`)
|
|
89
|
+
* - Environment settings (`disableDevtools`, `debug`, `otelOptions`)
|
|
90
|
+
* - Behavior defaults (`confirmUnsavedChanges`, `unusedCacheTime`)
|
|
91
|
+
*
|
|
92
|
+
* Store-specific fields like `schema`, `adapter`, `storeId`, and `boot` are intentionally
|
|
93
|
+
* excluded since they vary per store definition.
|
|
94
|
+
*/
|
|
95
|
+
defaultOptions?: Partial<
|
|
96
|
+
Pick<
|
|
97
|
+
RegistryStoreOptions,
|
|
98
|
+
'batchUpdates' | 'disableDevtools' | 'confirmUnsavedChanges' | 'debug' | 'otelOptions' | 'unusedCacheTime'
|
|
99
|
+
>
|
|
100
|
+
>
|
|
101
|
+
/**
|
|
102
|
+
* Custom Effect runtime for all registry operations (loading, caching, etc.).
|
|
103
|
+
* When the runtime's scope closes, all managed stores are automatically shut down.
|
|
104
|
+
*/
|
|
105
|
+
runtime?: Runtime.Runtime<Scope.Scope | OtelTracer.OtelTracer>
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* RcMap cache key that uses storeId for equality/hashing but carries full options.
|
|
110
|
+
* This allows RcMap to deduplicate by storeId while the lookup function has access to all options.
|
|
111
|
+
*
|
|
112
|
+
* @remarks
|
|
113
|
+
* Only `storeId` is used for equality and hashing. This means if `getOrLoadPromise` is called
|
|
114
|
+
* with different options (e.g., different `adapter`) but the same `storeId`, the cached store
|
|
115
|
+
* from the first call will be returned. This is intentional - a store's identity is determined
|
|
116
|
+
* solely by its `storeId`, and callers should not expect to get different stores by varying
|
|
117
|
+
* other options while keeping the same `storeId`.
|
|
118
|
+
*/
|
|
119
|
+
class StoreCacheKey implements Equal.Equal {
|
|
120
|
+
readonly options: RegistryStoreOptions<any, any, any>
|
|
121
|
+
|
|
122
|
+
constructor(options: RegistryStoreOptions<any, any, any>) {
|
|
123
|
+
this.options = options
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Equality is based solely on `storeId`. Other options in `RegistryStoreOptions` are ignored
|
|
128
|
+
* for cache key comparison. The first options used for a given `storeId` determine the
|
|
129
|
+
* store's configuration.
|
|
130
|
+
*/
|
|
131
|
+
[Equal.symbol](that: Equal.Equal): boolean {
|
|
132
|
+
return that instanceof StoreCacheKey && this.options.storeId === that.options.storeId
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
[Hash.symbol](): number {
|
|
136
|
+
return Hash.string(this.options.storeId)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Store Registry coordinating store loading, caching, and retention
|
|
142
|
+
*
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
145
|
+
export class StoreRegistry {
|
|
146
|
+
/**
|
|
147
|
+
* Reference-counted cache mapping storeId to Store instances.
|
|
148
|
+
* Stores are created on first access and disposed after `unusedCacheTime` when all references are released.
|
|
149
|
+
*/
|
|
150
|
+
readonly #rcMap: RcMap.RcMap<StoreCacheKey, Store<any, any>, UnknownError>
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Effect runtime providing Scope and OtelTracer for all registry operations.
|
|
154
|
+
* When the runtime's scope closes, all managed stores are automatically shut down.
|
|
155
|
+
*/
|
|
156
|
+
readonly #runtime: Runtime.Runtime<Scope.Scope | OtelTracer.OtelTracer>
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Disposal callback for the runtime created by the registry.
|
|
160
|
+
* Undefined when caller provided their own runtime (caller owns cleanup in that case).
|
|
161
|
+
*/
|
|
162
|
+
readonly #disposeOwnedRuntime: (() => Promise<void>) | undefined
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* In-flight loading promises keyed by storeId.
|
|
166
|
+
* Ensures concurrent `getOrLoadPromise` calls receive the same Promise reference.
|
|
167
|
+
*/
|
|
168
|
+
readonly #loadingPromises: Map<string, Promise<Store<any, any>>> = new Map()
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Creates a new StoreRegistry instance.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const registry = new StoreRegistry({
|
|
176
|
+
* defaultOptions: {
|
|
177
|
+
* batchUpdates,
|
|
178
|
+
* unusedCacheTime: 30_000,
|
|
179
|
+
* }
|
|
180
|
+
* })
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
constructor(config: StoreRegistryConfig = {}) {
|
|
184
|
+
if (config.runtime !== undefined) {
|
|
185
|
+
this.#runtime = config.runtime
|
|
186
|
+
} else {
|
|
187
|
+
const ownedRuntime = ManagedRuntime.make(Layer.mergeAll(Layer.scope, OtelLiveDummy))
|
|
188
|
+
this.#runtime = ownedRuntime.runtimeEffect.pipe(Effect.runSync)
|
|
189
|
+
this.#disposeOwnedRuntime = () => ownedRuntime.dispose()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
this.#rcMap = RcMap.make({
|
|
193
|
+
lookup: ({ options }: StoreCacheKey) => {
|
|
194
|
+
// Merge registry defaults with call-site options (call-site takes precedence)
|
|
195
|
+
const mergedOptions = { ...config.defaultOptions, ...options }
|
|
196
|
+
return createStore(mergedOptions).pipe(
|
|
197
|
+
Effect.catchAllDefect((cause) => UnknownError.make({ cause })),
|
|
198
|
+
Effect.withSpan(`StoreRegistry.lookup:${mergedOptions.storeId}`),
|
|
199
|
+
LogConfig.withLoggerConfig(mergedOptions, { threadName: 'window' }),
|
|
200
|
+
provideOtel(
|
|
201
|
+
omitUndefineds({
|
|
202
|
+
parentSpanContext: mergedOptions.otelOptions?.rootSpanContext,
|
|
203
|
+
otelTracer: mergedOptions.otelOptions?.tracer,
|
|
204
|
+
}),
|
|
205
|
+
),
|
|
206
|
+
)
|
|
207
|
+
},
|
|
208
|
+
idleTimeToLive: ({ options }: StoreCacheKey) => options.unusedCacheTime ?? config.defaultOptions?.unusedCacheTime ?? DEFAULT_UNUSED_CACHE_TIME,
|
|
209
|
+
}).pipe(Runtime.runSync(this.#runtime))
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Gets a cached store or loads a new one, with the store lifetime scoped to the caller.
|
|
214
|
+
*
|
|
215
|
+
* @typeParam TSchema - The schema type for the store
|
|
216
|
+
* @typeParam TContext - The context type for the store
|
|
217
|
+
* @typeParam TSyncPayloadSchema - The sync payload schema type
|
|
218
|
+
* @returns An Effect that yields the store, scoped to the provided Scope
|
|
219
|
+
*
|
|
220
|
+
* @remarks
|
|
221
|
+
* - Stores are kept in cache and reused while any scope holds them
|
|
222
|
+
* - When the scope closes, the reference is released; the store is disposed after `unusedCacheTime`
|
|
223
|
+
* if no other scopes retain it
|
|
224
|
+
* - Concurrent calls with the same storeId share the same store instance
|
|
225
|
+
*/
|
|
226
|
+
getOrLoad = <
|
|
227
|
+
TSchema extends LiveStoreSchema,
|
|
228
|
+
TContext = {},
|
|
229
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
230
|
+
>(
|
|
231
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
232
|
+
): Effect.Effect<Store<TSchema, TContext>, UnknownError, Scope.Scope> =>
|
|
233
|
+
Effect.gen(this, function* () {
|
|
234
|
+
// Cast options to satisfy StoreCacheKey's wider type (type safety enforced at API boundary)
|
|
235
|
+
const key = new StoreCacheKey(options)
|
|
236
|
+
const store = yield* RcMap.get(this.#rcMap, key)
|
|
237
|
+
|
|
238
|
+
return store as Store<TSchema, TContext>
|
|
239
|
+
}).pipe(Effect.withSpan(`StoreRegistry.getOrLoad:${options.storeId}`))
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Get or load a store, returning it directly if already loaded or a promise if loading.
|
|
243
|
+
*
|
|
244
|
+
* @typeParam TSchema - The schema type for the store
|
|
245
|
+
* @typeParam TContext - The context type for the store
|
|
246
|
+
* @typeParam TSyncPayloadSchema - The sync payload schema type
|
|
247
|
+
* @returns The loaded store if available, or a Promise that resolves to the loaded store
|
|
248
|
+
* @throws unknown - store loading error
|
|
249
|
+
*
|
|
250
|
+
* @remarks
|
|
251
|
+
* - Returns the store instance directly (synchronous) when already loaded
|
|
252
|
+
* - Returns a stable Promise reference when loading is in progress or needs to be initiated
|
|
253
|
+
* - Throws with the same error instance on subsequent calls after failure
|
|
254
|
+
* - Applies default options from registry config, with call-site options taking precedence
|
|
255
|
+
* - Concurrent calls with the same storeId share the same store instance
|
|
256
|
+
*/
|
|
257
|
+
getOrLoadPromise = <
|
|
258
|
+
TSchema extends LiveStoreSchema,
|
|
259
|
+
TContext = {},
|
|
260
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
261
|
+
>(
|
|
262
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
263
|
+
): Store<TSchema, TContext> | Promise<Store<TSchema, TContext>> => {
|
|
264
|
+
const exit = this.getOrLoad(options).pipe(Effect.scoped, Runtime.runSyncExit(this.#runtime))
|
|
265
|
+
|
|
266
|
+
if (Exit.isSuccess(exit) === true) return exit.value
|
|
267
|
+
|
|
268
|
+
// Check if the failure is due to async work
|
|
269
|
+
const defect = Cause.dieOption(exit.cause)
|
|
270
|
+
if (defect._tag !== 'Some') {
|
|
271
|
+
// Handle synchronous failure
|
|
272
|
+
throw Cause.squash(exit.cause)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (Runtime.isAsyncFiberException(defect.value) === false) {
|
|
276
|
+
// Handle synchronous failure
|
|
277
|
+
throw Cause.squash(exit.cause)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const { storeId } = options
|
|
281
|
+
|
|
282
|
+
// Return cached promise if one exists (ensures concurrent calls get the same Promise reference)
|
|
283
|
+
const cached = this.#loadingPromises.get(storeId)
|
|
284
|
+
if (cached !== undefined) return cached as Promise<Store<TSchema, TContext>>
|
|
285
|
+
|
|
286
|
+
// Create and cache the promise
|
|
287
|
+
const fiber = defect.value.fiber as Fiber.RuntimeFiber<Store<TSchema, TContext>>
|
|
288
|
+
const promise = Fiber.join(fiber)
|
|
289
|
+
.pipe(Runtime.runPromise(this.#runtime))
|
|
290
|
+
.finally(() => this.#loadingPromises.delete(storeId))
|
|
291
|
+
|
|
292
|
+
this.#loadingPromises.set(storeId, promise)
|
|
293
|
+
return promise
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Retains the store in cache.
|
|
298
|
+
*
|
|
299
|
+
* @typeParam TSchema - The schema type for the store
|
|
300
|
+
* @typeParam TContext - The context type for the store
|
|
301
|
+
* @typeParam TSyncPayloadSchema - The sync payload schema type
|
|
302
|
+
* @returns A release function that, when called, removes this retention hold
|
|
303
|
+
*
|
|
304
|
+
* @remarks
|
|
305
|
+
* - Multiple retains on the same store are independent; each must be released separately
|
|
306
|
+
* - If the store isn't cached yet, it will be loaded and then retained
|
|
307
|
+
* - The store will remain in cache until all retains are released and after `unusedCacheTime` expires
|
|
308
|
+
*/
|
|
309
|
+
retain = <
|
|
310
|
+
TSchema extends LiveStoreSchema,
|
|
311
|
+
TContext = {},
|
|
312
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
313
|
+
>(
|
|
314
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
315
|
+
): (() => void) => {
|
|
316
|
+
const release = Effect.gen(this, function* () {
|
|
317
|
+
// Cast options to satisfy StoreCacheKey's wider type (type safety enforced at API boundary)
|
|
318
|
+
const key = new StoreCacheKey(options)
|
|
319
|
+
yield* RcMap.get(this.#rcMap, key)
|
|
320
|
+
// Effect.never suspends indefinitely, keeping the RcMap reference alive.
|
|
321
|
+
// When `release()` is called, the fiber is interrupted, closing the scope
|
|
322
|
+
// and releasing the RcMap entry (which may trigger disposal after idleTimeToLive).
|
|
323
|
+
yield* Effect.never
|
|
324
|
+
}).pipe(Effect.scoped, Runtime.runCallback(this.#runtime))
|
|
325
|
+
|
|
326
|
+
return () => release()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Loads a store (without suspending) to warm up the cache.
|
|
331
|
+
*
|
|
332
|
+
* @typeParam TSchema - The schema of the store to preload
|
|
333
|
+
* @typeParam TContext - The context type for the store
|
|
334
|
+
* @typeParam TSyncPayloadSchema - The sync payload schema type
|
|
335
|
+
* @returns A promise that resolves when the loading is complete (success or failure)
|
|
336
|
+
*
|
|
337
|
+
* @remarks
|
|
338
|
+
* - We don't return the store or throw as this is a fire-and-forget operation.
|
|
339
|
+
* - If the entry remains unused after preload resolves/rejects, it is scheduled for disposal.
|
|
340
|
+
* - Does not affect the retention of the store in cache.
|
|
341
|
+
*/
|
|
342
|
+
preload = async <
|
|
343
|
+
TSchema extends LiveStoreSchema,
|
|
344
|
+
TContext = {},
|
|
345
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
346
|
+
>(
|
|
347
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
348
|
+
): Promise<void> => {
|
|
349
|
+
try {
|
|
350
|
+
await this.getOrLoadPromise(options)
|
|
351
|
+
} catch {
|
|
352
|
+
// Do nothing; preload is best-effort
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Disposes the registry and all its managed stores, immediately releasing resources
|
|
358
|
+
* (database connections, WebSocket connections, web workers, etc.).
|
|
359
|
+
*
|
|
360
|
+
* Most applications should use a single `StoreRegistry` and don't need to call
|
|
361
|
+
* this method. It's only necessary when creating multiple short-lived registries to
|
|
362
|
+
* immediately release resources and avoid conflicts with subsequent registries.
|
|
363
|
+
*
|
|
364
|
+
* @returns A promise that resolves when disposal is complete
|
|
365
|
+
*
|
|
366
|
+
* @remarks
|
|
367
|
+
* - No-op if a custom `runtime` was provided to the constructor (caller owns cleanup)
|
|
368
|
+
* - Idempotent: safe to call multiple times
|
|
369
|
+
* - After disposal, the registry should not be used
|
|
370
|
+
*/
|
|
371
|
+
dispose = async (): Promise<void> => {
|
|
372
|
+
await this.#disposeOwnedRuntime?.()
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Helper for defining reusable store options with full type inference. Returns
|
|
378
|
+
* options that can be passed to `useStore()` or `storeRegistry.preload()`.
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* At runtime this is an identity function that returns the input unchanged.
|
|
382
|
+
* Its value lies in enabling TypeScript's excess property checking to catch
|
|
383
|
+
* typos and configuration errors, while allowing options to be shared across
|
|
384
|
+
* `useStore()`, `storeRegistry.preload()`, `storeRegistry.getOrLoad()`, etc.
|
|
385
|
+
*
|
|
386
|
+
* @typeParam TSchema - The LiveStore schema type
|
|
387
|
+
* @typeParam TContext - User-defined context attached to the store
|
|
388
|
+
* @typeParam TSyncPayloadSchema - Schema for the sync payload sent to the backend
|
|
389
|
+
* @param options - The store configuration options
|
|
390
|
+
* @returns The same options object, unchanged
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* export const issueStoreOptions = (issueId: string) =>
|
|
395
|
+
* storeOptions({
|
|
396
|
+
* storeId: `issue-${issueId}`,
|
|
397
|
+
* schema,
|
|
398
|
+
* adapter,
|
|
399
|
+
* unusedCacheTime: 30_000,
|
|
400
|
+
* })
|
|
401
|
+
*
|
|
402
|
+
* // In a component
|
|
403
|
+
* const issueStore = useStore(issueStoreOptions(issueId))
|
|
404
|
+
*
|
|
405
|
+
* // In a route loader or event handler
|
|
406
|
+
* storeRegistry.preload({
|
|
407
|
+
* ...issueStoreOptions(issueId),
|
|
408
|
+
* unusedCacheTime: 10_000,
|
|
409
|
+
* });
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
export const storeOptions = <
|
|
413
|
+
TSchema extends LiveStoreSchema,
|
|
414
|
+
TContext = {},
|
|
415
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
416
|
+
>(
|
|
417
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
418
|
+
): RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema> => options
|