@livestore/livestore 0.4.0 → 0.5.0-dev.1
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/dist/.tsbuildinfo +1 -1
- package/dist/QueryCache.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.d.ts +1 -1
- package/dist/SqliteDbWrapper.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.js +9 -7
- package/dist/SqliteDbWrapper.js.map +1 -1
- package/dist/SqliteDbWrapper.test.js +22 -5
- package/dist/SqliteDbWrapper.test.js.map +1 -1
- package/dist/effect/LiveStore.d.ts +68 -16
- package/dist/effect/LiveStore.d.ts.map +1 -1
- package/dist/effect/LiveStore.js +22 -20
- package/dist/effect/LiveStore.js.map +1 -1
- package/dist/effect/LiveStore.test.js +7 -8
- package/dist/effect/LiveStore.test.js.map +1 -1
- package/dist/effect/mod.js +2 -2
- package/dist/internal/mod.js +2 -2
- package/dist/live-queries/base-class.d.ts +20 -3
- package/dist/live-queries/base-class.d.ts.map +1 -1
- package/dist/live-queries/base-class.js +30 -4
- package/dist/live-queries/base-class.js.map +1 -1
- package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
- package/dist/live-queries/client-document-get-query.js +1 -1
- package/dist/live-queries/computed.d.ts +1 -1
- package/dist/live-queries/computed.d.ts.map +1 -1
- package/dist/live-queries/computed.js +5 -5
- package/dist/live-queries/computed.js.map +1 -1
- package/dist/live-queries/db-query.d.ts +3 -3
- package/dist/live-queries/db-query.d.ts.map +1 -1
- package/dist/live-queries/db-query.js +30 -21
- package/dist/live-queries/db-query.js.map +1 -1
- package/dist/live-queries/db-query.test.js +15 -15
- package/dist/live-queries/db-query.test.js.map +1 -1
- package/dist/live-queries/mod.js +4 -4
- package/dist/live-queries/signal.d.ts +1 -1
- package/dist/live-queries/signal.d.ts.map +1 -1
- package/dist/live-queries/signal.js +2 -2
- package/dist/live-queries/signal.js.map +1 -1
- package/dist/live-queries/signal.test.js +5 -5
- package/dist/live-queries/signal.test.js.map +1 -1
- package/dist/mod.js +8 -8
- package/dist/reactive.d.ts +1 -1
- package/dist/reactive.d.ts.map +1 -1
- package/dist/reactive.js +20 -1
- package/dist/reactive.js.map +1 -1
- package/dist/reactive.test.js +1 -1
- package/dist/store/StoreRegistry.d.ts +12 -12
- package/dist/store/StoreRegistry.d.ts.map +1 -1
- package/dist/store/StoreRegistry.js +25 -25
- package/dist/store/StoreRegistry.js.map +1 -1
- package/dist/store/StoreRegistry.test.js +65 -62
- package/dist/store/StoreRegistry.test.js.map +1 -1
- package/dist/store/create-store.d.ts +27 -16
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/create-store.js +55 -27
- package/dist/store/create-store.js.map +1 -1
- package/dist/store/create-store.test.d.ts +2 -0
- package/dist/store/create-store.test.d.ts.map +1 -0
- package/dist/store/create-store.test.js +28 -0
- package/dist/store/create-store.test.js.map +1 -0
- package/dist/store/devtools.d.ts +1 -2
- package/dist/store/devtools.d.ts.map +1 -1
- package/dist/store/devtools.js +6 -5
- package/dist/store/devtools.js.map +1 -1
- package/dist/store/store-commit.test.d.ts +2 -0
- package/dist/store/store-commit.test.d.ts.map +1 -0
- package/dist/store/store-commit.test.js +85 -0
- package/dist/store/store-commit.test.js.map +1 -0
- package/dist/store/store-eventstream.test.js +6 -6
- package/dist/store/store-eventstream.test.js.map +1 -1
- package/dist/store/store-types.d.ts +10 -28
- package/dist/store/store-types.d.ts.map +1 -1
- package/dist/store/store-types.js +2 -38
- package/dist/store/store-types.js.map +1 -1
- package/dist/store/store-types.test.js +4 -4
- package/dist/store/store.d.ts +15 -16
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +149 -91
- package/dist/store/store.js.map +1 -1
- package/dist/utils/data-structures.d.ts.map +1 -1
- package/dist/utils/dev.d.ts.map +1 -1
- package/dist/utils/dev.js +1 -1
- package/dist/utils/dev.js.map +1 -1
- package/dist/utils/function-string.d.ts +3 -3
- package/dist/utils/function-string.d.ts.map +1 -1
- package/dist/utils/stack-info.d.ts.map +1 -1
- package/dist/utils/stack-info.js +2 -1
- package/dist/utils/stack-info.js.map +1 -1
- package/dist/utils/stack-info.test.js +1 -1
- package/dist/utils/tests/fixture.d.ts +26 -34
- package/dist/utils/tests/fixture.d.ts.map +1 -1
- package/dist/utils/tests/fixture.js.map +1 -1
- package/dist/utils/tests/mod.js +3 -3
- package/dist/utils/tests/otel.d.ts +6 -6
- package/dist/utils/tests/otel.d.ts.map +1 -1
- package/package.json +40 -50
- package/src/SqliteDbWrapper.test.ts +25 -3
- package/src/SqliteDbWrapper.ts +13 -10
- package/src/effect/LiveStore.test.ts +16 -16
- package/src/effect/LiveStore.ts +83 -51
- package/src/live-queries/__snapshots__/db-query.test.ts.snap +303 -97
- package/src/live-queries/base-class.ts +39 -7
- package/src/live-queries/computed.ts +5 -5
- package/src/live-queries/db-query.test.ts +9 -9
- package/src/live-queries/db-query.ts +42 -30
- package/src/live-queries/signal.test.ts +2 -2
- package/src/live-queries/signal.ts +3 -3
- package/src/reactive.ts +45 -35
- package/src/store/StoreRegistry.test.ts +62 -59
- package/src/store/StoreRegistry.ts +49 -38
- package/src/store/create-store.test.ts +35 -0
- package/src/store/create-store.ts +110 -55
- package/src/store/devtools.ts +283 -283
- package/src/store/store-commit.test.ts +137 -0
- package/src/store/store-eventstream.test.ts +16 -7
- package/src/store/store-types.test.ts +1 -1
- package/src/store/store-types.ts +10 -56
- package/src/store/store.ts +184 -110
- package/src/utils/dev.ts +1 -1
- package/src/utils/stack-info.ts +4 -1
|
@@ -2,19 +2,19 @@ import * as otel from '@opentelemetry/api'
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
type Adapter,
|
|
5
|
+
type BackendIdMismatchError,
|
|
5
6
|
type BootStatus,
|
|
6
7
|
type ClientSession,
|
|
7
8
|
type ClientSessionDevtoolsChannel,
|
|
8
|
-
type ClientSessionSyncProcessorSimulationParams,
|
|
9
9
|
type IntentionalShutdownCause,
|
|
10
|
-
LogConfig,
|
|
11
10
|
type MaterializeError,
|
|
12
|
-
type BackendIdMismatchError,
|
|
13
11
|
type MigrationsReport,
|
|
14
12
|
provideOtel,
|
|
15
13
|
type ServerAheadError,
|
|
16
14
|
UnknownError,
|
|
15
|
+
type LogConfig,
|
|
17
16
|
} from '@livestore/common'
|
|
17
|
+
import { STATE_REBUILD_BATCH_SIZE_DEFAULT, StateRebuildBatchSizeSchema } from '@livestore/common/leader-thread'
|
|
18
18
|
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
19
19
|
import { isDevEnv, LS_DEV, omitUndefineds } from '@livestore/utils'
|
|
20
20
|
import {
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
Layer,
|
|
28
28
|
OtelTracer,
|
|
29
29
|
Queue,
|
|
30
|
-
|
|
30
|
+
References,
|
|
31
31
|
Schema,
|
|
32
32
|
Scope,
|
|
33
33
|
TaskTracing,
|
|
@@ -43,6 +43,14 @@ import type {
|
|
|
43
43
|
import { StoreInternalsSymbol } from './store-types.ts'
|
|
44
44
|
import { STORE_DEFAULT_PARAMS, Store } from './store.ts'
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Hard upper bound (ms) for the detached shutdown drain. A dead/unresponsive leader must not keep
|
|
48
|
+
* the drain (and therefore the lifetime scope) alive forever; after this bound the scope is
|
|
49
|
+
* force-closed. Kept comfortably above the 1s caller-side soft wait so that a still-progressing
|
|
50
|
+
* in-flight leader push is allowed to finish rather than being interrupted.
|
|
51
|
+
*/
|
|
52
|
+
const SHUTDOWN_DRAIN_HARD_TIMEOUT_MS = 30_000
|
|
53
|
+
|
|
46
54
|
declare global {
|
|
47
55
|
/** Store instances for console debugging */
|
|
48
56
|
var __debugLiveStore: Record<string, Store<any, any>> | undefined
|
|
@@ -64,29 +72,28 @@ declare global {
|
|
|
64
72
|
* const { store } = yield* AppStore.Tag
|
|
65
73
|
* ```
|
|
66
74
|
*/
|
|
67
|
-
export class LiveStoreContextRunning extends Context.
|
|
68
|
-
LiveStoreContextRunning,
|
|
69
|
-
|
|
70
|
-
>() {
|
|
75
|
+
export class LiveStoreContextRunning extends Context.Service<LiveStoreContextRunning, LiveStoreContextRunning_>()(
|
|
76
|
+
'@livestore/livestore/effect/LiveStoreContextRunning',
|
|
77
|
+
) {
|
|
71
78
|
static fromDeferred = Effect.gen(function* () {
|
|
72
79
|
const deferred = yield* DeferredStoreContext
|
|
73
|
-
const ctx = yield* deferred
|
|
74
|
-
return Layer.succeed(LiveStoreContextRunning, ctx)
|
|
75
|
-
}).pipe(Layer.
|
|
80
|
+
const ctx = yield* Deferred.await(deferred)
|
|
81
|
+
return Layer.succeed(LiveStoreContextRunning, LiveStoreContextRunning.of(ctx))
|
|
82
|
+
}).pipe(Layer.unwrap)
|
|
76
83
|
}
|
|
77
84
|
|
|
78
85
|
/**
|
|
79
86
|
* @deprecated Use `StoreContext.DeferredTag` from `makeStoreContext()` instead.
|
|
80
87
|
*/
|
|
81
|
-
export class DeferredStoreContext extends Context.
|
|
88
|
+
export class DeferredStoreContext extends Context.Service<
|
|
82
89
|
DeferredStoreContext,
|
|
83
|
-
Deferred.Deferred<LiveStoreContextRunning['
|
|
84
|
-
>() {}
|
|
90
|
+
Deferred.Deferred<LiveStoreContextRunning['Service'], UnknownError>
|
|
91
|
+
>()('@livestore/livestore/effect/DeferredStoreContext') {}
|
|
85
92
|
|
|
86
93
|
export type LiveStoreContextProps<
|
|
87
94
|
TSchema extends LiveStoreSchema,
|
|
88
95
|
TContext = {},
|
|
89
|
-
TSyncPayloadSchema extends Schema.Schema
|
|
96
|
+
TSyncPayloadSchema extends Schema.Codec<Schema.Json, Schema.Json> = typeof Schema.Json,
|
|
90
97
|
> = {
|
|
91
98
|
schema: TSchema
|
|
92
99
|
/**
|
|
@@ -115,7 +122,7 @@ export type LiveStoreContextProps<
|
|
|
115
122
|
/**
|
|
116
123
|
* Schema describing the shape of the sync payload and used to encode it.
|
|
117
124
|
*
|
|
118
|
-
* - If omitted, `Schema.
|
|
125
|
+
* - If omitted, `Schema.Json` is used (no additional typing/validation).
|
|
119
126
|
* - Prefer exporting a schema from your app (e.g. `export const SyncPayload = Schema.Struct({ authToken: Schema.String })`)
|
|
120
127
|
* and pass it here to get end-to-end type safety and validation.
|
|
121
128
|
*/
|
|
@@ -130,14 +137,20 @@ export type LiveStoreContextProps<
|
|
|
130
137
|
* const SyncPayload = Schema.Struct({ authToken: Schema.String })
|
|
131
138
|
* useStore({ ..., syncPayloadSchema: SyncPayload, syncPayload: { authToken: '...' } })
|
|
132
139
|
*/
|
|
133
|
-
syncPayload?:
|
|
140
|
+
syncPayload?: TSyncPayloadSchema['Type']
|
|
141
|
+
/** Advanced runtime tuning parameters. */
|
|
142
|
+
params?: {
|
|
143
|
+
/** Number of events read and committed per SQLite state-rebuild batch. Defaults to 100. */
|
|
144
|
+
stateRebuildBatchSize?: number
|
|
145
|
+
}
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
export interface CreateStoreOptions<
|
|
137
149
|
TSchema extends LiveStoreSchema,
|
|
138
150
|
TContext = {},
|
|
139
|
-
TSyncPayloadSchema extends Schema.Schema
|
|
140
|
-
>
|
|
151
|
+
TSyncPayloadSchema extends Schema.Codec<Schema.Json, Schema.Json> = typeof Schema.Json,
|
|
152
|
+
>
|
|
153
|
+
extends LogConfig.LoggerOptions {
|
|
141
154
|
/** The LiveStore schema defining tables, events, and materializers. */
|
|
142
155
|
schema: TSchema
|
|
143
156
|
/** Adapter used for data storage and synchronization. */
|
|
@@ -192,7 +205,7 @@ export interface CreateStoreOptions<
|
|
|
192
205
|
/**
|
|
193
206
|
* Schema describing the shape of the sync payload and used to encode it.
|
|
194
207
|
*
|
|
195
|
-
* - If omitted, `Schema.
|
|
208
|
+
* - If omitted, `Schema.Json` is used (no additional typing/validation).
|
|
196
209
|
* - Prefer exporting a schema from your app (e.g. `export const SyncPayload = Schema.Struct({ authToken: Schema.String })`)
|
|
197
210
|
* and pass it here to get end-to-end type safety and validation.
|
|
198
211
|
*/
|
|
@@ -206,16 +219,22 @@ export interface CreateStoreOptions<
|
|
|
206
219
|
*
|
|
207
220
|
* @default undefined
|
|
208
221
|
*/
|
|
209
|
-
syncPayload?:
|
|
210
|
-
/**
|
|
222
|
+
syncPayload?: TSyncPayloadSchema['Type']
|
|
223
|
+
/** Advanced runtime tuning parameters. */
|
|
211
224
|
params?: {
|
|
212
225
|
/** Max events pushed to the leader per write batch. */
|
|
213
226
|
leaderPushBatchSize?: number
|
|
214
227
|
/** Chunk size used when the stream replays confirmed events. */
|
|
215
228
|
eventQueryBatchSize?: number
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Number of events read and committed per batch when rebuilding SQLite state from the event log.
|
|
231
|
+
* Lower values reduce per-batch resource usage but perform more queries and savepoints.
|
|
232
|
+
* This is a per-client runtime setting and changing it does not trigger a rebuild.
|
|
233
|
+
*
|
|
234
|
+
* @default 100
|
|
235
|
+
* @minimum 1
|
|
236
|
+
*/
|
|
237
|
+
stateRebuildBatchSize?: number
|
|
219
238
|
}
|
|
220
239
|
debug?: {
|
|
221
240
|
instanceId?: string
|
|
@@ -225,7 +244,7 @@ export interface CreateStoreOptions<
|
|
|
225
244
|
export type CreateStoreOptionsPromise<
|
|
226
245
|
TSchema extends LiveStoreSchema = LiveStoreSchema.Any,
|
|
227
246
|
TContext = {},
|
|
228
|
-
TSyncPayloadSchema extends Schema.Schema
|
|
247
|
+
TSyncPayloadSchema extends Schema.Codec<Schema.Json, Schema.Json> = typeof Schema.Json,
|
|
229
248
|
> = CreateStoreOptions<TSchema, TContext, TSyncPayloadSchema> & {
|
|
230
249
|
signal?: AbortSignal
|
|
231
250
|
otelOptions?: Partial<OtelOptions>
|
|
@@ -235,7 +254,7 @@ export type CreateStoreOptionsPromise<
|
|
|
235
254
|
export const createStorePromise = async <
|
|
236
255
|
TSchema extends LiveStoreSchema = LiveStoreSchema.Any,
|
|
237
256
|
TContext = {},
|
|
238
|
-
TSyncPayloadSchema extends Schema.Schema
|
|
257
|
+
TSyncPayloadSchema extends Schema.Codec<Schema.Json, Schema.Json> = typeof Schema.Json,
|
|
239
258
|
>({
|
|
240
259
|
signal,
|
|
241
260
|
otelOptions,
|
|
@@ -243,15 +262,15 @@ export const createStorePromise = async <
|
|
|
243
262
|
}: CreateStoreOptionsPromise<TSchema, TContext, TSyncPayloadSchema>): Promise<Store<TSchema, TContext>> =>
|
|
244
263
|
Effect.gen(function* () {
|
|
245
264
|
const scope = yield* Scope.make()
|
|
246
|
-
const
|
|
265
|
+
const services = yield* Effect.context()
|
|
247
266
|
|
|
248
267
|
if (signal !== undefined) {
|
|
249
268
|
signal.addEventListener('abort', () => {
|
|
250
|
-
Scope.close(scope, Exit.void).pipe(Effect.tapCauseLogPretty,
|
|
269
|
+
Scope.close(scope, Exit.void).pipe(Effect.tapCauseLogPretty, Effect.runForkWith(services))
|
|
251
270
|
})
|
|
252
271
|
}
|
|
253
272
|
|
|
254
|
-
return yield* createStore({ ...options }).pipe(Scope.
|
|
273
|
+
return yield* createStore({ ...options }).pipe(Scope.provide(scope))
|
|
255
274
|
}).pipe(
|
|
256
275
|
Effect.withSpan('createStore', {
|
|
257
276
|
attributes: { storeId: options.storeId, disableDevtools: options.disableDevtools },
|
|
@@ -259,14 +278,19 @@ export const createStorePromise = async <
|
|
|
259
278
|
provideOtel(omitUndefineds({ parentSpanContext: otelOptions?.rootSpanContext, otelTracer: otelOptions?.tracer })),
|
|
260
279
|
Effect.tapCauseLogPretty,
|
|
261
280
|
Effect.annotateLogs({ thread: 'window' }),
|
|
262
|
-
|
|
281
|
+
Effect.provide(
|
|
282
|
+
Layer.mergeAll(
|
|
283
|
+
options.logger ?? Layer.empty,
|
|
284
|
+
Layer.succeed(References.MinimumLogLevel, options.logLevel ?? (isDevEnv() === true ? 'Debug' : 'Info')),
|
|
285
|
+
),
|
|
286
|
+
),
|
|
263
287
|
Effect.runPromise,
|
|
264
288
|
)
|
|
265
289
|
|
|
266
290
|
export const createStore = <
|
|
267
291
|
TSchema extends LiveStoreSchema = LiveStoreSchema.Any,
|
|
268
292
|
TContext = {},
|
|
269
|
-
TSyncPayloadSchema extends Schema.Schema
|
|
293
|
+
TSyncPayloadSchema extends Schema.Codec<Schema.Json, Schema.Json> = typeof Schema.Json,
|
|
270
294
|
>({
|
|
271
295
|
schema,
|
|
272
296
|
adapter,
|
|
@@ -288,6 +312,10 @@ export const createStore = <
|
|
|
288
312
|
Scope.Scope | OtelTracer.OtelTracer
|
|
289
313
|
> =>
|
|
290
314
|
Effect.gen(function* () {
|
|
315
|
+
const stateRebuildBatchSize = yield* Schema.decodeEffect(StateRebuildBatchSizeSchema)(
|
|
316
|
+
params?.stateRebuildBatchSize ?? STATE_REBUILD_BATCH_SIZE_DEFAULT,
|
|
317
|
+
).pipe(UnknownError.mapToUnknownError)
|
|
318
|
+
|
|
291
319
|
const lifetimeScope = yield* Scope.make()
|
|
292
320
|
|
|
293
321
|
yield* validateStoreId(storeId)
|
|
@@ -295,18 +323,20 @@ export const createStore = <
|
|
|
295
323
|
yield* Effect.addFinalizer((_) => Scope.close(lifetimeScope, _))
|
|
296
324
|
|
|
297
325
|
const debugInstanceId = debug?.instanceId ?? nanoid(10)
|
|
298
|
-
const resolvedSyncPayloadSchema = (syncPayloadSchema ?? Schema.
|
|
326
|
+
const resolvedSyncPayloadSchema = (syncPayloadSchema ?? Schema.Json) as TSyncPayloadSchema
|
|
299
327
|
|
|
300
328
|
return yield* Effect.gen(function* () {
|
|
301
329
|
const span = yield* OtelTracer.currentOtelSpan.pipe(Effect.orDie)
|
|
302
330
|
const otelRootSpanContext = otel.trace.setSpan(otel.context.active(), span)
|
|
303
331
|
const otelTracer = yield* OtelTracer.OtelTracer
|
|
304
332
|
|
|
305
|
-
const bootStatusQueue = yield* Queue.unbounded<BootStatus>()
|
|
333
|
+
const bootStatusQueue = yield* Effect.acquireRelease(Queue.unbounded<BootStatus>(), Queue.shutdown)
|
|
306
334
|
|
|
307
335
|
yield* Queue.take(bootStatusQueue).pipe(
|
|
308
336
|
Effect.tapSync((status) => onBootStatus?.(status)),
|
|
309
|
-
Effect.tap((status) =>
|
|
337
|
+
Effect.tap((status) =>
|
|
338
|
+
status.stage === 'done' ? Queue.shutdown(bootStatusQueue).pipe(Effect.asVoid) : Effect.void,
|
|
339
|
+
),
|
|
310
340
|
Effect.forever,
|
|
311
341
|
Effect.tapCauseLogPretty,
|
|
312
342
|
Effect.forkScoped,
|
|
@@ -316,25 +346,43 @@ export const createStore = <
|
|
|
316
346
|
|
|
317
347
|
const connectDevtoolsToStore_ = (storeDevtoolsChannel: ClientSessionDevtoolsChannel) =>
|
|
318
348
|
Effect.gen(function* () {
|
|
319
|
-
const store = yield* storeDeferred
|
|
349
|
+
const store = yield* Deferred.await(storeDeferred)
|
|
320
350
|
yield* connectDevtoolsToStore({ storeDevtoolsChannel, store })
|
|
321
351
|
})
|
|
322
352
|
|
|
323
|
-
const
|
|
353
|
+
const services = yield* Effect.context<Scope.Scope>()
|
|
354
|
+
let shutdownSyncProcessor: ((exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>) | undefined
|
|
324
355
|
|
|
325
356
|
const shutdown = (
|
|
326
|
-
exit: Exit.Exit<
|
|
327
|
-
IntentionalShutdownCause,
|
|
328
|
-
UnknownError | MaterializeError | BackendIdMismatchError
|
|
329
|
-
>,
|
|
357
|
+
exit: Exit.Exit<IntentionalShutdownCause, UnknownError | MaterializeError | BackendIdMismatchError>,
|
|
330
358
|
) =>
|
|
331
359
|
Effect.gen(function* () {
|
|
332
|
-
|
|
360
|
+
// Hard outer bound on the DETACHED teardown: the processor drain `awaitEmpty`s the
|
|
361
|
+
// leader-push worker, which never completes if the leader is dead/unresponsive. Without a
|
|
362
|
+
// bound the drain would block forever, so `Scope.close(lifetimeScope)` (in `ensuring`)
|
|
363
|
+
// would never run and the lifetime scope + its resources would leak indefinitely (a later
|
|
364
|
+
// `createStore`/registry dispose on the same `storeId` would observe a never-closed store).
|
|
365
|
+
// Drain up to the bound, then force-close the scope regardless. The bound exceeds the
|
|
366
|
+
// caller-side wait below so an in-flight (but progressing) push is not cut short.
|
|
367
|
+
const closeFiber = yield* (shutdownSyncProcessor?.(exit) ?? Effect.void).pipe(
|
|
368
|
+
Effect.timeoutOrElse({
|
|
369
|
+
duration: SHUTDOWN_DRAIN_HARD_TIMEOUT_MS,
|
|
370
|
+
orElse: () =>
|
|
371
|
+
Effect.logError(
|
|
372
|
+
`@livestore/livestore:shutdown: drain exceeded hard bound of ${SHUTDOWN_DRAIN_HARD_TIMEOUT_MS}ms; forcing scope close`,
|
|
373
|
+
),
|
|
374
|
+
}),
|
|
375
|
+
Effect.ensuring(Scope.close(lifetimeScope, exit)),
|
|
376
|
+
Effect.forkDetach,
|
|
377
|
+
)
|
|
378
|
+
// Caller-side soft wait: stop blocking the shutdown() caller after 1s without cancelling
|
|
379
|
+
// the detached teardown above (which remains bounded by SHUTDOWN_DRAIN_HARD_TIMEOUT_MS).
|
|
380
|
+
yield* Fiber.join(closeFiber).pipe(
|
|
333
381
|
Effect.logWarnIfTakesLongerThan({ label: '@livestore/livestore:shutdown', duration: 500 }),
|
|
334
|
-
Effect.
|
|
335
|
-
|
|
336
|
-
Effect.logError('@livestore/livestore:shutdown: Timed out after 1 second'),
|
|
337
|
-
),
|
|
382
|
+
Effect.timeoutOrElse({
|
|
383
|
+
duration: 1000,
|
|
384
|
+
orElse: () => Effect.logError('@livestore/livestore:shutdown: Timed out after 1 second'),
|
|
385
|
+
}),
|
|
338
386
|
)
|
|
339
387
|
|
|
340
388
|
if (shutdownDeferred !== undefined) {
|
|
@@ -344,7 +392,7 @@ export const createStore = <
|
|
|
344
392
|
yield* Effect.logDebug('LiveStore shutdown complete')
|
|
345
393
|
}).pipe(
|
|
346
394
|
Effect.withSpan('@livestore/livestore:shutdown'),
|
|
347
|
-
Effect.provide(
|
|
395
|
+
Effect.provide(services),
|
|
348
396
|
Effect.tapCauseLogPretty,
|
|
349
397
|
// Given that the shutdown flow might also interrupt the effect that is calling the shutdown,
|
|
350
398
|
// we want to detach the shutdown effect so it's not interrupted by itself
|
|
@@ -355,11 +403,12 @@ export const createStore = <
|
|
|
355
403
|
const syncPayloadEncoded =
|
|
356
404
|
syncPayload === undefined
|
|
357
405
|
? undefined
|
|
358
|
-
: yield* Schema.
|
|
406
|
+
: yield* Schema.encodeEffect(resolvedSyncPayloadSchema)(syncPayload).pipe(UnknownError.mapToUnknownError)
|
|
359
407
|
|
|
360
408
|
const clientSession: ClientSession = yield* adapter({
|
|
361
409
|
schema,
|
|
362
410
|
storeId,
|
|
411
|
+
params: { stateRebuildBatchSize },
|
|
363
412
|
devtoolsEnabled: getDevtoolsEnabled(disableDevtools),
|
|
364
413
|
bootStatusQueue,
|
|
365
414
|
shutdown,
|
|
@@ -385,10 +434,10 @@ export const createStore = <
|
|
|
385
434
|
schema,
|
|
386
435
|
context,
|
|
387
436
|
otelOptions: { tracer: otelTracer, rootSpanContext: otelRootSpanContext },
|
|
388
|
-
effectContext: { lifetimeScope,
|
|
437
|
+
effectContext: { lifetimeScope, services },
|
|
389
438
|
// TODO find a better way to detect if we're running LiveStore in the LiveStore devtools
|
|
390
439
|
// But for now this is a good enough approximation with little downsides
|
|
391
|
-
__runningInDevtools: !
|
|
440
|
+
__runningInDevtools: !getDevtoolsEnabled(disableDevtools),
|
|
392
441
|
confirmUnsavedChanges,
|
|
393
442
|
// NOTE during boot we're not yet executing events in a batched context
|
|
394
443
|
// but only set the provided `batchUpdates` function after boot
|
|
@@ -397,26 +446,32 @@ export const createStore = <
|
|
|
397
446
|
params: {
|
|
398
447
|
leaderPushBatchSize: params?.leaderPushBatchSize ?? STORE_DEFAULT_PARAMS.leaderPushBatchSize,
|
|
399
448
|
eventQueryBatchSize: params?.eventQueryBatchSize ?? STORE_DEFAULT_PARAMS.eventQueryBatchSize,
|
|
400
|
-
...omitUndefineds({ simulation: params?.simulation }),
|
|
401
449
|
},
|
|
402
450
|
})
|
|
451
|
+
shutdownSyncProcessor = store[StoreInternalsSymbol].syncProcessor.shutdown
|
|
403
452
|
|
|
404
453
|
// Starts background fibers (syncing, event processing, etc) for store
|
|
405
454
|
yield* store[StoreInternalsSymbol].boot
|
|
406
455
|
|
|
407
456
|
if (boot !== undefined) {
|
|
408
457
|
// TODO also incorporate `boot` function progress into `bootStatusQueue`
|
|
409
|
-
|
|
458
|
+
// @effect-diagnostics-next-line anyUnknownInErrorContext:off -- user boot errors are immediately normalized to LiveStore UnknownError
|
|
459
|
+
yield* Effect.trySyncOrPromiseOrEffect(() =>
|
|
410
460
|
boot(store, { migrationsReport: clientSession.leaderThread.initialState.migrationsReport, parentSpan: span }),
|
|
411
461
|
).pipe(
|
|
412
462
|
UnknownError.mapToUnknownError,
|
|
413
|
-
Effect.provide(
|
|
463
|
+
Effect.provide(
|
|
464
|
+
Layer.succeed(
|
|
465
|
+
LiveStoreContextRunning,
|
|
466
|
+
LiveStoreContextRunning.of({ stage: 'running', store: store as any as Store }),
|
|
467
|
+
),
|
|
468
|
+
),
|
|
414
469
|
Effect.withSpan('createStore:boot'),
|
|
415
470
|
)
|
|
416
471
|
}
|
|
417
472
|
|
|
418
473
|
// NOTE it's important to yield here to allow the forked Effect in the store constructor to run
|
|
419
|
-
yield* Effect.yieldNow
|
|
474
|
+
yield* Effect.yieldNow
|
|
420
475
|
|
|
421
476
|
if (batchUpdates !== undefined) {
|
|
422
477
|
// Replacing the default batchUpdates function with the provided one after boot
|
|
@@ -440,7 +495,7 @@ export const createStore = <
|
|
|
440
495
|
Effect.withSpan('createStore', { attributes: { debugInstanceId, storeId } }),
|
|
441
496
|
Effect.annotateLogs({ debugInstanceId, storeId }),
|
|
442
497
|
LS_DEV === true ? TaskTracing.withAsyncTaggingTracing((name) => (console as any).createTask(name)) : identity,
|
|
443
|
-
Scope.
|
|
498
|
+
Scope.provide(lifetimeScope),
|
|
444
499
|
)
|
|
445
500
|
})
|
|
446
501
|
|