@livestore/livestore 0.5.0-dev.1 → 0.5.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/QueryCache.js +1 -2
- package/dist/QueryCache.js.map +1 -1
- package/dist/ReactiveStateSqliteDb.d.ts +9 -0
- package/dist/ReactiveStateSqliteDb.d.ts.map +1 -0
- package/dist/ReactiveStateSqliteDb.js +6 -0
- package/dist/ReactiveStateSqliteDb.js.map +1 -0
- package/dist/SqliteDbWrapper.d.ts +11 -9
- package/dist/SqliteDbWrapper.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.js +65 -14
- package/dist/SqliteDbWrapper.js.map +1 -1
- package/dist/SqliteDbWrapper.test.js +18 -0
- package/dist/SqliteDbWrapper.test.js.map +1 -1
- package/dist/live-queries/db-query.test.js +4 -0
- package/dist/live-queries/db-query.test.js.map +1 -1
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/create-store.js +47 -17
- package/dist/store/create-store.js.map +1 -1
- package/dist/store/store-types.d.ts +2 -2
- package/dist/store/store-types.d.ts.map +1 -1
- package/dist/store/store-types.js.map +1 -1
- package/dist/store/store.d.ts +2 -2
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +32 -34
- package/dist/store/store.js.map +1 -1
- package/package.json +12 -12
- package/src/QueryCache.ts +1 -3
- package/src/ReactiveStateSqliteDb.ts +10 -0
- package/src/SqliteDbWrapper.test.ts +21 -0
- package/src/SqliteDbWrapper.ts +81 -19
- package/src/live-queries/__snapshots__/db-query.test.ts.snap +223 -278
- package/src/live-queries/db-query.test.ts +4 -0
- package/src/store/create-store.ts +68 -22
- package/src/store/store-types.ts +11 -2
- package/src/store/store.ts +101 -102
|
@@ -25,6 +25,10 @@ Vitest.describe('otel', () => {
|
|
|
25
25
|
if (key === 'code.stacktrace') {
|
|
26
26
|
return '<STACKTRACE>'
|
|
27
27
|
}
|
|
28
|
+
// Savepoint ids are process-local counters, not part of the query tracing contract.
|
|
29
|
+
if (key === 'sql.query' && typeof val === 'string') {
|
|
30
|
+
return val.replace(/livestore_savepoint_\d+/g, 'livestore_savepoint_<id>')
|
|
31
|
+
}
|
|
28
32
|
return val
|
|
29
33
|
})
|
|
30
34
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type ClientSession,
|
|
8
8
|
type ClientSessionDevtoolsChannel,
|
|
9
9
|
type IntentionalShutdownCause,
|
|
10
|
+
type MaterializationJournal,
|
|
10
11
|
type MaterializeError,
|
|
11
12
|
type MigrationsReport,
|
|
12
13
|
provideOtel,
|
|
@@ -320,7 +321,31 @@ export const createStore = <
|
|
|
320
321
|
|
|
321
322
|
yield* validateStoreId(storeId)
|
|
322
323
|
|
|
323
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Started at most once by either `store.shutdown()` or the closing of the scope `createStore` runs in, so both
|
|
326
|
+
* paths share one bounded drain. See `startTeardown` below.
|
|
327
|
+
*/
|
|
328
|
+
let teardownFiber: Fiber.Fiber<void> | undefined
|
|
329
|
+
let startTeardownRef: ((exit: Exit.Exit<unknown, unknown>) => Effect.Effect<Fiber.Fiber<void>>) | undefined
|
|
330
|
+
|
|
331
|
+
// Closing the caller's scope is an orderly shutdown too: queued session events are drained to the leader before
|
|
332
|
+
// the lifetime scope (and with it an in-process leader) closes. If creation failed before the teardown existed,
|
|
333
|
+
// there is nothing to drain and the lifetime scope is closed directly.
|
|
334
|
+
yield* Effect.addFinalizer((exit) => {
|
|
335
|
+
if (startTeardownRef === undefined) return Scope.close(lifetimeScope, exit)
|
|
336
|
+
// A teardown started earlier by `store.shutdown()` has already reported its outcome to that caller (and to
|
|
337
|
+
// `shutdownDeferred`). Only a drain started by this scope close is reported here, and never by failing the
|
|
338
|
+
// close itself.
|
|
339
|
+
const startedByScopeClose = teardownFiber === undefined
|
|
340
|
+
return startTeardownRef(exit).pipe(
|
|
341
|
+
Effect.flatMap(awaitTeardownWithSoftBound),
|
|
342
|
+
Effect.catchCause((cause) =>
|
|
343
|
+
startedByScopeClose === true
|
|
344
|
+
? Effect.logError('@livestore/livestore:shutdown: drain on scope close failed', cause)
|
|
345
|
+
: Effect.void,
|
|
346
|
+
),
|
|
347
|
+
)
|
|
348
|
+
})
|
|
324
349
|
|
|
325
350
|
const debugInstanceId = debug?.instanceId ?? nanoid(10)
|
|
326
351
|
const resolvedSyncPayloadSchema = (syncPayloadSchema ?? Schema.Json) as TSyncPayloadSchema
|
|
@@ -353,18 +378,18 @@ export const createStore = <
|
|
|
353
378
|
const services = yield* Effect.context<Scope.Scope>()
|
|
354
379
|
let shutdownSyncProcessor: ((exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>) | undefined
|
|
355
380
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
381
|
+
/**
|
|
382
|
+
* Drains the session sync processor (for a successful exit) and then closes the lifetime scope, detached so the
|
|
383
|
+
* shutdown flow cannot interrupt itself. The processor drain waits for the leader-push worker, which never
|
|
384
|
+
* completes if the leader is dead or unresponsive. Without a bound `Scope.close(lifetimeScope)` would never
|
|
385
|
+
* run and the lifetime scope would leak (a later `createStore`/registry dispose on the same `storeId` would
|
|
386
|
+
* observe a never-closed store). The bound exceeds the caller-side soft wait so an in-flight but progressing
|
|
387
|
+
* push is not cut short.
|
|
388
|
+
*/
|
|
389
|
+
const startTeardown = (exit: Exit.Exit<unknown, unknown>): Effect.Effect<Fiber.Fiber<void>> =>
|
|
390
|
+
Effect.suspend(() => {
|
|
391
|
+
if (teardownFiber !== undefined) return Effect.succeed(teardownFiber)
|
|
392
|
+
return (shutdownSyncProcessor?.(exit) ?? Effect.void).pipe(
|
|
368
393
|
Effect.timeoutOrElse({
|
|
369
394
|
duration: SHUTDOWN_DRAIN_HARD_TIMEOUT_MS,
|
|
370
395
|
orElse: () =>
|
|
@@ -373,17 +398,25 @@ export const createStore = <
|
|
|
373
398
|
),
|
|
374
399
|
}),
|
|
375
400
|
Effect.ensuring(Scope.close(lifetimeScope, exit)),
|
|
401
|
+
Effect.provide(services),
|
|
376
402
|
Effect.forkDetach,
|
|
403
|
+
Effect.tap((fiber) =>
|
|
404
|
+
Effect.sync(() => {
|
|
405
|
+
teardownFiber = fiber
|
|
406
|
+
}),
|
|
407
|
+
),
|
|
377
408
|
)
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
409
|
+
})
|
|
410
|
+
startTeardownRef = startTeardown
|
|
411
|
+
|
|
412
|
+
const shutdown = (
|
|
413
|
+
exit: Exit.Exit<
|
|
414
|
+
IntentionalShutdownCause,
|
|
415
|
+
UnknownError | MaterializeError | MaterializationJournal.MaterializationJournalError | BackendIdMismatchError
|
|
416
|
+
>,
|
|
417
|
+
) =>
|
|
418
|
+
Effect.gen(function* () {
|
|
419
|
+
yield* awaitTeardownWithSoftBound(yield* startTeardown(exit))
|
|
387
420
|
|
|
388
421
|
if (shutdownDeferred !== undefined) {
|
|
389
422
|
yield* Deferred.done(shutdownDeferred, exit)
|
|
@@ -523,3 +556,16 @@ const getDevtoolsEnabled = (disableDevtools: boolean | 'auto' | undefined) => {
|
|
|
523
556
|
|
|
524
557
|
return false
|
|
525
558
|
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Caller-side soft wait: stops blocking whoever triggered the shutdown after 1s without cancelling the detached
|
|
562
|
+
* teardown, which remains bounded by `SHUTDOWN_DRAIN_HARD_TIMEOUT_MS`.
|
|
563
|
+
*/
|
|
564
|
+
const awaitTeardownWithSoftBound = (teardown: Fiber.Fiber<void>) =>
|
|
565
|
+
Fiber.join(teardown).pipe(
|
|
566
|
+
Effect.logWarnIfTakesLongerThan({ label: '@livestore/livestore:shutdown', duration: 500 }),
|
|
567
|
+
Effect.timeoutOrElse({
|
|
568
|
+
duration: 1000,
|
|
569
|
+
orElse: () => Effect.logError('@livestore/livestore:shutdown: Timed out after 1 second'),
|
|
570
|
+
}),
|
|
571
|
+
)
|
package/src/store/store-types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type ClientSessionSyncProcessor,
|
|
7
7
|
type IntentionalShutdownCause,
|
|
8
8
|
isQueryBuilder,
|
|
9
|
+
type MaterializationJournal,
|
|
9
10
|
type MaterializeError,
|
|
10
11
|
type QueryBuilder,
|
|
11
12
|
type StoreInterrupted,
|
|
@@ -52,11 +53,19 @@ export type LiveStoreContext<TSchema extends LiveStoreSchema = LiveStoreSchema.A
|
|
|
52
53
|
|
|
53
54
|
export type ShutdownDeferred = Deferred.Deferred<
|
|
54
55
|
IntentionalShutdownCause,
|
|
55
|
-
|
|
56
|
+
| UnknownError
|
|
57
|
+
| StoreInterrupted
|
|
58
|
+
| MaterializeError
|
|
59
|
+
| MaterializationJournal.MaterializationJournalError
|
|
60
|
+
| BackendIdMismatchError
|
|
56
61
|
>
|
|
57
62
|
export const makeShutdownDeferred: Effect.Effect<ShutdownDeferred> = Deferred.make<
|
|
58
63
|
IntentionalShutdownCause,
|
|
59
|
-
|
|
64
|
+
| UnknownError
|
|
65
|
+
| StoreInterrupted
|
|
66
|
+
| MaterializeError
|
|
67
|
+
| MaterializationJournal.MaterializationJournalError
|
|
68
|
+
| BackendIdMismatchError
|
|
60
69
|
>()
|
|
61
70
|
|
|
62
71
|
/**
|
package/src/store/store.ts
CHANGED
|
@@ -10,13 +10,14 @@ import {
|
|
|
10
10
|
IntentionalShutdownCause,
|
|
11
11
|
isQueryBuilder,
|
|
12
12
|
liveStoreVersion,
|
|
13
|
+
MaterializationJournal,
|
|
13
14
|
MaterializeError,
|
|
14
15
|
MaterializerHashMismatchError,
|
|
15
16
|
makeClientSessionSyncProcessor,
|
|
16
17
|
prepareBindValues,
|
|
17
18
|
QueryBuilderAstSymbol,
|
|
18
19
|
resolveSessionIdSymbolInBindValues,
|
|
19
|
-
|
|
20
|
+
StateSqliteDb,
|
|
20
21
|
StateHead,
|
|
21
22
|
type StorageMode,
|
|
22
23
|
type SyncState,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
Exit,
|
|
34
35
|
Fiber,
|
|
35
36
|
Inspectable,
|
|
37
|
+
Layer,
|
|
36
38
|
Option,
|
|
37
39
|
OtelTracer,
|
|
38
40
|
Queue,
|
|
@@ -47,6 +49,7 @@ import { makeReactivityGraph } from '../live-queries/base-class.ts'
|
|
|
47
49
|
import { makeExecBeforeFirstRun } from '../live-queries/client-document-get-query.ts'
|
|
48
50
|
import { queryDb } from '../live-queries/db-query.ts'
|
|
49
51
|
import type { Ref } from '../reactive.ts'
|
|
52
|
+
import * as ReactiveStateSqliteDb from '../ReactiveStateSqliteDb.ts'
|
|
50
53
|
import { SqliteDbWrapper } from '../SqliteDbWrapper.ts'
|
|
51
54
|
import { ReferenceCountedSet } from '../utils/data-structures.ts'
|
|
52
55
|
import { downloadBlob, exposeDebugUtils } from '../utils/dev.ts'
|
|
@@ -209,100 +212,112 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
|
|
|
209
212
|
this.storageMode = clientSession.leaderThread.initialState.storageMode
|
|
210
213
|
|
|
211
214
|
const reactivityGraph = makeReactivityGraph()
|
|
212
|
-
const
|
|
215
|
+
const sqliteDbWrapper = new SqliteDbWrapper({ otel: otelOptions, db: clientSession.sqliteDb })
|
|
216
|
+
// Journal rollback must invalidate cached reads too. Both roles use the same connection, with the state
|
|
217
|
+
// services consuming its cache-aware adapter instead of bypassing it during changeset/savepoint rollback.
|
|
218
|
+
const stateDbLayer = StateSqliteDb.layer(sqliteDbWrapper.serviceDb)
|
|
219
|
+
const reactiveStateDbLayer = ReactiveStateSqliteDb.layer(sqliteDbWrapper)
|
|
220
|
+
const stateServicesLayer = Layer.mergeAll(MaterializationJournal.layer, StateHead.layer).pipe(
|
|
221
|
+
Layer.provide(stateDbLayer),
|
|
222
|
+
)
|
|
223
|
+
const materializationLayer = Layer.mergeAll(stateDbLayer, reactiveStateDbLayer, stateServicesLayer)
|
|
224
|
+
// Build the services once: `materializeEvent` runs for every committed and replayed event, and providing the layer
|
|
225
|
+
// there rebuilt the journal and state-head services each time. None of them owns resources, so the build scope can
|
|
226
|
+
// close right away.
|
|
227
|
+
const materializationContext = Layer.build(materializationLayer).pipe(
|
|
228
|
+
Effect.scoped,
|
|
229
|
+
Effect.runSyncWith(effectContext.services),
|
|
230
|
+
)
|
|
213
231
|
|
|
214
232
|
const syncProcessor = makeClientSessionSyncProcessor({
|
|
215
233
|
schema,
|
|
216
234
|
clientSession,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// Runtime schema doesn't know this event yet; skip materialization but
|
|
228
|
-
// keep the log entry so upgraded clients can replay it later.
|
|
229
|
-
yield* stateHead.set(eventEncoded.seqNum)
|
|
230
|
-
return {
|
|
231
|
-
writeTables: new Set<string>(),
|
|
232
|
-
sessionChangeset: { _tag: 'no-op' as const },
|
|
233
|
-
materializerHash: Option.none(),
|
|
234
|
-
}
|
|
235
|
-
}
|
|
235
|
+
// No span per event: the processor traces each commit batch and pull step, and replaying a rebase materializes
|
|
236
|
+
// every pending event, where a span per event was a large share of the step's main-thread time.
|
|
237
|
+
materializeEvent: (eventEncoded, { materializerHashLeader }) =>
|
|
238
|
+
Effect.gen(function* () {
|
|
239
|
+
const materializationJournal = yield* MaterializationJournal.MaterializationJournal
|
|
240
|
+
const dbState = yield* ReactiveStateSqliteDb.ReactiveStateSqliteDb
|
|
241
|
+
const resolution = yield* resolveEventDef(schema, {
|
|
242
|
+
operation: '@livestore/livestore:store:materializeEvent',
|
|
243
|
+
event: eventEncoded,
|
|
244
|
+
})
|
|
236
245
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
const materializerHash =
|
|
247
|
-
isDevEnv() === true ? Option.some(hashMaterializerResults(execArgsArr)) : Option.none()
|
|
248
|
-
|
|
249
|
-
// Hash mismatch detection only occurs during the pull path (when receiving events from the leader).
|
|
250
|
-
// During push path (local commits), materializerHashLeader is always Option.none(), so this condition
|
|
251
|
-
// will never be met. The check happens when the same event comes back from the leader during sync,
|
|
252
|
-
// allowing us to compare the leader's computed hash with our local re-materialization hash.
|
|
253
|
-
if (
|
|
254
|
-
materializerHashLeader._tag === 'Some' &&
|
|
255
|
-
materializerHash._tag === 'Some' &&
|
|
256
|
-
materializerHashLeader.value !== materializerHash.value
|
|
257
|
-
) {
|
|
258
|
-
return yield* MaterializerHashMismatchError.make({ eventName: eventEncoded.name })
|
|
246
|
+
if (resolution._tag === 'unknown') {
|
|
247
|
+
// Runtime schema doesn't know this event yet; skip materialization but
|
|
248
|
+
// keep the log entry so upgraded clients can replay it later.
|
|
249
|
+
yield* materializationJournal.record({ key: eventEncoded.seqNum, changeset: null })
|
|
250
|
+
return {
|
|
251
|
+
writeTables: new Set<string>(),
|
|
252
|
+
materializerHash: Option.none(),
|
|
259
253
|
}
|
|
254
|
+
}
|
|
260
255
|
|
|
261
|
-
|
|
262
|
-
const otelContext = otel.trace.setSpan(otel.context.active(), span)
|
|
263
|
-
|
|
264
|
-
const writeTablesForEvent = new Set<string>()
|
|
265
|
-
|
|
266
|
-
const exec = () => {
|
|
267
|
-
for (const {
|
|
268
|
-
statementSql,
|
|
269
|
-
bindValues,
|
|
270
|
-
writeTables = this[StoreInternalsSymbol].sqliteDbWrapper.getTablesUsed(statementSql),
|
|
271
|
-
} of execArgsArr) {
|
|
272
|
-
try {
|
|
273
|
-
this[StoreInternalsSymbol].sqliteDbWrapper.cachedExecute(statementSql, bindValues, {
|
|
274
|
-
otelContext,
|
|
275
|
-
writeTables,
|
|
276
|
-
})
|
|
277
|
-
} catch (cause) {
|
|
278
|
-
// TOOD refactor with `SqliteError`
|
|
279
|
-
throw UnknownError.make({
|
|
280
|
-
cause,
|
|
281
|
-
note: `Error executing materializer for event "${eventEncoded.name}".\nStatement: ${statementSql}\nBind values: ${JSON.stringify(bindValues)}`,
|
|
282
|
-
})
|
|
283
|
-
}
|
|
256
|
+
const { eventDef, materializer } = resolution
|
|
284
257
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
258
|
+
const execArgsArr = getExecStatementsFromMaterializer({
|
|
259
|
+
eventDef,
|
|
260
|
+
materializer,
|
|
261
|
+
dbState,
|
|
262
|
+
event: { decoded: undefined, encoded: eventEncoded },
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
const materializerHash =
|
|
266
|
+
isDevEnv() === true ? Option.some(hashMaterializerResults(execArgsArr)) : Option.none()
|
|
267
|
+
|
|
268
|
+
// Hash mismatch detection only occurs during the pull path (when receiving events from the leader).
|
|
269
|
+
// During push path (local commits), materializerHashLeader is always Option.none(), so this condition
|
|
270
|
+
// will never be met. The check happens when the same event comes back from the leader during sync,
|
|
271
|
+
// allowing us to compare the leader's computed hash with our local re-materialization hash.
|
|
272
|
+
if (
|
|
273
|
+
materializerHashLeader._tag === 'Some' &&
|
|
274
|
+
materializerHash._tag === 'Some' &&
|
|
275
|
+
materializerHashLeader.value !== materializerHash.value
|
|
276
|
+
) {
|
|
277
|
+
return yield* MaterializerHashMismatchError.make({ eventName: eventEncoded.name })
|
|
278
|
+
}
|
|
289
279
|
|
|
290
|
-
|
|
280
|
+
const span = yield* OtelTracer.currentOtelSpan.pipe(Effect.orDie)
|
|
281
|
+
const otelContext = otel.trace.setSpan(otel.context.active(), span)
|
|
282
|
+
|
|
283
|
+
const writeTablesForEvent = new Set<string>()
|
|
284
|
+
|
|
285
|
+
const exec = () => {
|
|
286
|
+
for (const { statementSql, bindValues, writeTables = dbState.getTablesUsed(statementSql) } of execArgsArr) {
|
|
287
|
+
try {
|
|
288
|
+
dbState.cachedExecute(statementSql, bindValues, {
|
|
289
|
+
otelContext,
|
|
290
|
+
writeTables,
|
|
291
|
+
})
|
|
292
|
+
} catch (cause) {
|
|
293
|
+
// TOOD refactor with `SqliteError`
|
|
294
|
+
throw UnknownError.make({
|
|
295
|
+
cause,
|
|
296
|
+
note: `Error executing materializer for event "${eventEncoded.name}".\nStatement: ${statementSql}\nBind values: ${JSON.stringify(bindValues)}`,
|
|
297
|
+
})
|
|
291
298
|
}
|
|
299
|
+
|
|
300
|
+
// durationMsTotal += durationMs
|
|
301
|
+
for (const table of writeTables) {
|
|
302
|
+
writeTablesForEvent.add(table)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
dbState.debug.head = eventEncoded.seqNum
|
|
292
306
|
}
|
|
307
|
+
}
|
|
293
308
|
|
|
294
|
-
|
|
295
|
-
|
|
309
|
+
const changeset = dbState.withChangeset(exec).changeset
|
|
310
|
+
yield* materializationJournal.record({ key: eventEncoded.seqNum, changeset })
|
|
296
311
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
312
|
+
return { writeTables: writeTablesForEvent, materializerHash }
|
|
313
|
+
}).pipe(
|
|
314
|
+
Effect.provideContext(materializationContext),
|
|
315
|
+
Effect.mapError((cause) =>
|
|
316
|
+
MaterializationJournal.isMaterializationJournalError(cause) === true
|
|
317
|
+
? cause
|
|
318
|
+
: MaterializeError.make({ cause }),
|
|
301
319
|
),
|
|
302
|
-
|
|
303
|
-
rollback: (changeset) => {
|
|
304
|
-
this[StoreInternalsSymbol].sqliteDbWrapper.rollback(changeset)
|
|
305
|
-
},
|
|
320
|
+
),
|
|
306
321
|
refreshTables: (tables) => {
|
|
307
322
|
const tablesToUpdate = [] as [Ref<null, ReactivityGraphContext, RefreshReason>, null][]
|
|
308
323
|
for (const tableName of tables) {
|
|
@@ -318,7 +333,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
|
|
|
318
333
|
}),
|
|
319
334
|
},
|
|
320
335
|
confirmUnsavedChanges,
|
|
321
|
-
}).pipe(Effect.
|
|
336
|
+
}).pipe(Effect.provideContext(materializationContext), Effect.runSyncWith(effectContext.services))
|
|
322
337
|
|
|
323
338
|
// TODO generalize the `tableRefs` concept to allow finer-grained refs
|
|
324
339
|
const tableRefs: { [key: string]: Ref<null, ReactivityGraphContext, RefreshReason> } = {}
|
|
@@ -387,9 +402,6 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
|
|
|
387
402
|
yield* syncProcessor.boot
|
|
388
403
|
})
|
|
389
404
|
|
|
390
|
-
// Build Sqlite wrapper last to avoid using getters before internals are set
|
|
391
|
-
const sqliteDbWrapper = new SqliteDbWrapper({ otel: otelOptions, db: clientSession.sqliteDb })
|
|
392
|
-
|
|
393
405
|
// Initialize internals bag
|
|
394
406
|
this[StoreInternalsSymbol] = {
|
|
395
407
|
eventSchema: LiveStoreEvent.Client.makeSchemaMemo(schema) as Schema.Codec<
|
|
@@ -871,22 +883,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
|
|
|
871
883
|
|
|
872
884
|
if (events.length === 0) return
|
|
873
885
|
|
|
874
|
-
const
|
|
875
|
-
|
|
876
|
-
const encodedEvents = yield* this[StoreInternalsSymbol].syncProcessor.encodeEvents(events)
|
|
877
|
-
|
|
878
|
-
const { writeTables } = yield* Effect.try({
|
|
879
|
-
try: () => {
|
|
880
|
-
const materialize = () =>
|
|
881
|
-
this[StoreInternalsSymbol].syncProcessor
|
|
882
|
-
.materializeEvents(encodedEvents)
|
|
883
|
-
.pipe(Effect.runSyncWith(localServices))
|
|
884
|
-
return events.length > 1 ? this[StoreInternalsSymbol].sqliteDbWrapper.txn(materialize) : materialize()
|
|
885
|
-
},
|
|
886
|
-
catch: (cause) => UnknownError.make({ cause }),
|
|
887
|
-
})
|
|
888
|
-
|
|
889
|
-
yield* this[StoreInternalsSymbol].syncProcessor.push(encodedEvents)
|
|
886
|
+
const { writeTables } = yield* this[StoreInternalsSymbol].syncProcessor.commit(events)
|
|
890
887
|
|
|
891
888
|
const tablesToUpdate: [Ref<null, ReactivityGraphContext, RefreshReason>, null][] = []
|
|
892
889
|
for (const tableName of writeTables) {
|
|
@@ -1159,7 +1156,9 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
|
|
|
1159
1156
|
*
|
|
1160
1157
|
* This is called automatically when the store was created using the React or Effect API.
|
|
1161
1158
|
*/
|
|
1162
|
-
shutdown = (
|
|
1159
|
+
shutdown = (
|
|
1160
|
+
cause?: Cause.Cause<UnknownError | MaterializeError | MaterializationJournal.MaterializationJournalError>,
|
|
1161
|
+
): Effect.Effect<void> => {
|
|
1163
1162
|
this[StoreInternalsSymbol].isShutdown = true
|
|
1164
1163
|
return this[StoreInternalsSymbol].clientSession.shutdown(
|
|
1165
1164
|
cause !== undefined ? Exit.failCause(cause) : Exit.succeed(IntentionalShutdownCause.make({ reason: 'manual' })),
|