@livestore/adapter-web 0.5.0-dev.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/common/connection.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.d.ts +2 -1
- package/dist/in-memory/in-memory-adapter.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.js +11 -8
- package/dist/in-memory/in-memory-adapter.js.map +1 -1
- package/dist/index.js +4 -4
- package/dist/single-tab/mod.js +1 -1
- package/dist/single-tab/single-tab-adapter.d.ts.map +1 -1
- package/dist/single-tab/single-tab-adapter.js +18 -19
- package/dist/single-tab/single-tab-adapter.js.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.js +2 -2
- package/dist/web-worker/client-session/devtools-web-channel.d.ts.map +1 -1
- package/dist/web-worker/client-session/persisted-adapter.d.ts.map +1 -1
- package/dist/web-worker/client-session/persisted-adapter.js +21 -23
- package/dist/web-worker/client-session/persisted-adapter.js.map +1 -1
- package/dist/web-worker/client-session/sqlite-loader.js.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.d.ts +17 -2
- package/dist/web-worker/common/persisted-sqlite.d.ts.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.js +15 -2
- package/dist/web-worker/common/persisted-sqlite.js.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.test-fixture.d.ts +2 -0
- package/dist/web-worker/common/persisted-sqlite.test-fixture.d.ts.map +1 -0
- package/dist/web-worker/common/persisted-sqlite.test-fixture.js +57 -0
- package/dist/web-worker/common/persisted-sqlite.test-fixture.js.map +1 -0
- package/dist/web-worker/common/rpc-worker.d.ts.map +1 -1
- package/dist/web-worker/common/shutdown-channel.d.ts.map +1 -1
- package/dist/web-worker/common/webmesh-node-names.d.ts.map +1 -1
- package/dist/web-worker/common/worker-disconnect-channel.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.d.ts +26 -17
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.js +3 -1
- package/dist/web-worker/common/worker-schema.js.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.js +8 -6
- package/dist/web-worker/leader-worker/make-leader-worker.js.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.js +6 -5
- package/dist/web-worker/shared-worker/make-shared-worker.js.map +1 -1
- package/package.json +36 -36
- package/src/in-memory/in-memory-adapter.ts +7 -0
- package/src/single-tab/single-tab-adapter.ts +18 -20
- package/src/web-worker/client-session/persisted-adapter.ts +18 -20
- package/src/web-worker/common/persisted-sqlite.test-fixture.ts +68 -0
- package/src/web-worker/common/persisted-sqlite.ts +31 -2
- package/src/web-worker/common/worker-schema.ts +4 -1
- package/src/web-worker/leader-worker/make-leader-worker.ts +5 -2
- package/src/web-worker/shared-worker/make-shared-worker.ts +2 -1
package/package.json
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$genie": {
|
|
3
|
+
"source": "package.json.genie.ts",
|
|
4
|
+
"warning": "DO NOT EDIT - changes will be overwritten",
|
|
5
|
+
"workspaceClosureDirs": [
|
|
6
|
+
"packages/@livestore/adapter-web",
|
|
7
|
+
"packages/@livestore/common",
|
|
8
|
+
"packages/@livestore/common-cf",
|
|
9
|
+
"packages/@livestore/sqlite-wasm",
|
|
10
|
+
"packages/@livestore/utils",
|
|
11
|
+
"packages/@livestore/utils-dev",
|
|
12
|
+
"packages/@livestore/wa-sqlite",
|
|
13
|
+
"packages/@livestore/webmesh"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
2
16
|
"name": "@livestore/adapter-web",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
17
|
+
"version": "0.5.0-dev.1",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
4
20
|
"license": "Apache-2.0",
|
|
5
21
|
"repository": {
|
|
6
22
|
"type": "git",
|
|
7
23
|
"url": "git+https://github.com/livestorejs/livestore.git"
|
|
8
24
|
},
|
|
9
|
-
"files": [
|
|
10
|
-
"dist",
|
|
11
|
-
"package.json",
|
|
12
|
-
"src"
|
|
13
|
-
],
|
|
14
|
-
"type": "module",
|
|
15
|
-
"sideEffects": false,
|
|
16
25
|
"exports": {
|
|
17
26
|
".": "./dist/index.js",
|
|
18
27
|
"./devtools-web-channel": "./dist/web-worker/client-session/devtools-web-channel.js",
|
|
@@ -20,15 +29,17 @@
|
|
|
20
29
|
"./worker-vite-dev-polyfill": "./dist/web-worker/vite-dev-polyfill.js",
|
|
21
30
|
"./shared-worker": "./dist/web-worker/shared-worker/make-shared-worker.js"
|
|
22
31
|
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"package.json",
|
|
35
|
+
"src"
|
|
36
|
+
],
|
|
26
37
|
"dependencies": {
|
|
27
|
-
"@
|
|
28
|
-
"@livestore/
|
|
29
|
-
"@livestore/
|
|
30
|
-
"@livestore/
|
|
31
|
-
"@
|
|
38
|
+
"@livestore/common": "^0.5.0-dev.1",
|
|
39
|
+
"@livestore/sqlite-wasm": "^0.5.0-dev.1",
|
|
40
|
+
"@livestore/utils": "^0.5.0-dev.1",
|
|
41
|
+
"@livestore/webmesh": "^0.5.0-dev.1",
|
|
42
|
+
"@opentelemetry/api": "1.9.0"
|
|
32
43
|
},
|
|
33
44
|
"devDependencies": {
|
|
34
45
|
"@types/chrome": "0.1.4",
|
|
@@ -36,30 +47,19 @@
|
|
|
36
47
|
"vitest": "4.1.9"
|
|
37
48
|
},
|
|
38
49
|
"peerDependencies": {
|
|
39
|
-
"@effect/opentelemetry": "^4.0.
|
|
40
|
-
"@effect/platform-browser": "^4.0.
|
|
41
|
-
"@effect/platform-bun": "^4.0.
|
|
42
|
-
"@effect/platform-node": "^4.0.
|
|
43
|
-
"@effect/platform-node-shared": "^4.0.
|
|
44
|
-
"@effect/vitest": "^4.0.
|
|
50
|
+
"@effect/opentelemetry": "^4.0.3",
|
|
51
|
+
"@effect/platform-browser": "^4.0.3",
|
|
52
|
+
"@effect/platform-bun": "^4.0.3",
|
|
53
|
+
"@effect/platform-node": "^4.0.3",
|
|
54
|
+
"@effect/platform-node-shared": "^4.0.3",
|
|
55
|
+
"@effect/vitest": "^4.0.3",
|
|
45
56
|
"@opentelemetry/api": "^1.9.0",
|
|
46
57
|
"@opentelemetry/resources": "^2.2.0",
|
|
47
58
|
"@standard-schema/spec": "^1.1.0",
|
|
48
|
-
"effect": "^4.0.
|
|
59
|
+
"effect": "^4.0.3"
|
|
49
60
|
},
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"warning": "DO NOT EDIT - changes will be overwritten",
|
|
53
|
-
"workspaceClosureDirs": [
|
|
54
|
-
"packages/@livestore/adapter-web",
|
|
55
|
-
"packages/@livestore/common",
|
|
56
|
-
"packages/@livestore/common-cf",
|
|
57
|
-
"packages/@livestore/sqlite-wasm",
|
|
58
|
-
"packages/@livestore/utils",
|
|
59
|
-
"packages/@livestore/utils-dev",
|
|
60
|
-
"packages/@livestore/wa-sqlite",
|
|
61
|
-
"packages/@livestore/webmesh"
|
|
62
|
-
]
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"test": "echo No tests yet"
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type Adapter,
|
|
3
|
+
type AdapterArgs,
|
|
3
4
|
ClientSessionLeaderThreadProxy,
|
|
4
5
|
Devtools,
|
|
5
6
|
type LockStatus,
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
Eventlog,
|
|
16
17
|
LeaderThreadCtx,
|
|
17
18
|
makeLeaderThreadLayer,
|
|
19
|
+
markStateAsCompleted,
|
|
18
20
|
streamEventsWithSyncState,
|
|
19
21
|
} from '@livestore/common/leader-thread'
|
|
20
22
|
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
@@ -157,6 +159,7 @@ export const makeInMemoryAdapter =
|
|
|
157
159
|
syncOptions: options.sync,
|
|
158
160
|
syncPayloadEncoded,
|
|
159
161
|
syncPayloadSchema,
|
|
162
|
+
params: adapterArgs.params,
|
|
160
163
|
importSnapshot: options.importSnapshot,
|
|
161
164
|
devtoolsEnabled,
|
|
162
165
|
sharedWorker: sharedWorkerClient === undefined ? undefined : makeWebmeshWorkerProxy(sharedWorkerClient),
|
|
@@ -216,6 +219,7 @@ export interface MakeLeaderThreadArgs {
|
|
|
216
219
|
importSnapshot: Uint8Array<ArrayBuffer> | undefined
|
|
217
220
|
devtoolsEnabled: boolean
|
|
218
221
|
sharedWorker: WebmeshWorkerProxy | undefined
|
|
222
|
+
params: AdapterArgs['params']
|
|
219
223
|
}
|
|
220
224
|
|
|
221
225
|
const makeLeaderThread = ({
|
|
@@ -229,6 +233,7 @@ const makeLeaderThread = ({
|
|
|
229
233
|
importSnapshot,
|
|
230
234
|
devtoolsEnabled,
|
|
231
235
|
sharedWorker,
|
|
236
|
+
params,
|
|
232
237
|
}: MakeLeaderThreadArgs) =>
|
|
233
238
|
Effect.gen(function* () {
|
|
234
239
|
const services = yield* Effect.context()
|
|
@@ -249,6 +254,7 @@ const makeLeaderThread = ({
|
|
|
249
254
|
dbState.import(importSnapshot)
|
|
250
255
|
|
|
251
256
|
const _migrationsReport = yield* migrateDb({ db: dbState, schema })
|
|
257
|
+
yield* markStateAsCompleted(dbState)
|
|
252
258
|
}
|
|
253
259
|
|
|
254
260
|
const devtoolsOptions = yield* makeDevtoolsOptions({
|
|
@@ -273,6 +279,7 @@ const makeLeaderThread = ({
|
|
|
273
279
|
shutdownChannel,
|
|
274
280
|
syncPayloadEncoded,
|
|
275
281
|
syncPayloadSchema: syncPayloadSchema as Schema.Decoder<Schema.Json, never> | undefined,
|
|
282
|
+
params,
|
|
276
283
|
}).pipe(Layer.provide(StateHead.layer({ dbState }))),
|
|
277
284
|
)
|
|
278
285
|
|
|
@@ -149,7 +149,7 @@ export const makeSingleTabAdapter =
|
|
|
149
149
|
(options: SingleTabAdapterOptions): Adapter =>
|
|
150
150
|
(adapterArgs) =>
|
|
151
151
|
Effect.gen(function* () {
|
|
152
|
-
const { schema, storeId, bootStatusQueue, shutdown, syncPayloadEncoded } = adapterArgs
|
|
152
|
+
const { schema, storeId, bootStatusQueue, shutdown, syncPayloadEncoded, params } = adapterArgs
|
|
153
153
|
// Note: devtoolsEnabled is ignored in single-tab mode (devtools require SharedWorker)
|
|
154
154
|
|
|
155
155
|
yield* ensureBrowserRequirements
|
|
@@ -157,6 +157,7 @@ export const makeSingleTabAdapter =
|
|
|
157
157
|
yield* Queue.offer(bootStatusQueue, { stage: 'loading' })
|
|
158
158
|
|
|
159
159
|
const sqlite3 = yield* Effect.promise(() => loadSqlite3())
|
|
160
|
+
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
160
161
|
|
|
161
162
|
const storageOptions = yield* Schema.decodeEffect(WorkerSchema.StorageType)(options.storage)
|
|
162
163
|
|
|
@@ -182,7 +183,7 @@ export const makeSingleTabAdapter =
|
|
|
182
183
|
const dataFromFile =
|
|
183
184
|
options.experimental?.disableFastPath === true || opfsWarning !== undefined
|
|
184
185
|
? undefined
|
|
185
|
-
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema }).pipe(
|
|
186
|
+
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema, makeSqliteDb }).pipe(
|
|
186
187
|
Effect.tapError((error) =>
|
|
187
188
|
Effect.logDebug('[@livestore/adapter-web:single-tab] Could not read persisted state db', error, {
|
|
188
189
|
storeId,
|
|
@@ -250,6 +251,7 @@ export const makeSingleTabAdapter =
|
|
|
250
251
|
devtoolsEnabled: false,
|
|
251
252
|
debugInstanceId: adapterArgs.debugInstanceId,
|
|
252
253
|
syncPayloadEncoded,
|
|
254
|
+
params,
|
|
253
255
|
}),
|
|
254
256
|
),
|
|
255
257
|
),
|
|
@@ -306,31 +308,27 @@ export const makeSingleTabAdapter =
|
|
|
306
308
|
)
|
|
307
309
|
|
|
308
310
|
// Get initial snapshot (either from fast-path or from worker)
|
|
309
|
-
const
|
|
311
|
+
const { sqliteDb, snapshotByteLength, migrationsReport } =
|
|
310
312
|
dataFromFile === undefined
|
|
311
|
-
? yield*
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}))
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
324
|
-
const sqliteDb = yield* makeSqliteDb({ _tag: 'in-memory' })
|
|
325
|
-
|
|
326
|
-
sqliteDb.import(initialResult.snapshot)
|
|
313
|
+
? yield* Effect.gen(function* () {
|
|
314
|
+
const { snapshot, migrationsReport } = yield* runInWorker(
|
|
315
|
+
'GetRecreateSnapshot',
|
|
316
|
+
innerWorker.GetRecreateSnapshot({}),
|
|
317
|
+
)
|
|
318
|
+
const sqliteDb = yield* Effect.acquireRelease(makeSqliteDb({ _tag: 'in-memory' }), (db) =>
|
|
319
|
+
Effect.sync(() => db.close()),
|
|
320
|
+
)
|
|
321
|
+
sqliteDb.import(snapshot)
|
|
322
|
+
return { sqliteDb, snapshotByteLength: snapshot.byteLength, migrationsReport }
|
|
323
|
+
})
|
|
324
|
+
: { ...dataFromFile, migrationsReport: { migrations: [] } }
|
|
327
325
|
|
|
328
326
|
const numberOfTables =
|
|
329
327
|
sqliteDb.select<{ count: number }>(`select count(*) as count from sqlite_master`)[0]?.count ?? 0
|
|
330
328
|
if (numberOfTables === 0) {
|
|
331
329
|
return yield* UnknownError.make({
|
|
332
330
|
cause: `Encountered empty or corrupted database`,
|
|
333
|
-
payload: { snapshotByteLength
|
|
331
|
+
payload: { snapshotByteLength, storageOptions: options.storage },
|
|
334
332
|
})
|
|
335
333
|
}
|
|
336
334
|
|
|
@@ -189,6 +189,7 @@ export const makePersistedAdapter =
|
|
|
189
189
|
shutdown,
|
|
190
190
|
syncPayloadSchema: _syncPayloadSchema,
|
|
191
191
|
syncPayloadEncoded,
|
|
192
|
+
params,
|
|
192
193
|
} = adapterArgs
|
|
193
194
|
|
|
194
195
|
// NOTE: The schema travels with the worker bundle (developers call
|
|
@@ -203,6 +204,7 @@ export const makePersistedAdapter =
|
|
|
203
204
|
yield* Queue.offer(bootStatusQueue, { stage: 'loading' })
|
|
204
205
|
|
|
205
206
|
const sqlite3 = yield* Effect.promise(() => loadSqlite3())
|
|
207
|
+
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
206
208
|
|
|
207
209
|
const LIVESTORE_TAB_LOCK = `livestore-tab-lock-${storeId}`
|
|
208
210
|
const LIVESTORE_SHARED_WORKER_TERMINATION_LOCK = `livestore-shared-worker-termination-lock-${storeId}`
|
|
@@ -233,11 +235,10 @@ export const makePersistedAdapter =
|
|
|
233
235
|
// we're here trying to get the snapshot directly from storage
|
|
234
236
|
// we usually speeds up the boot process by a lot.
|
|
235
237
|
// We need to be extra careful though to not run into any race conditions or inconsistencies.
|
|
236
|
-
// TODO also verify persisted data
|
|
237
238
|
const dataFromFile =
|
|
238
239
|
options.experimental?.disableFastPath === true || opfsWarning !== undefined
|
|
239
240
|
? undefined
|
|
240
|
-
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema }).pipe(
|
|
241
|
+
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema, makeSqliteDb }).pipe(
|
|
241
242
|
Effect.tapError((error) =>
|
|
242
243
|
Effect.logDebug('[@livestore/adapter-web:client-session] Could not read persisted state db', error, {
|
|
243
244
|
storeId,
|
|
@@ -350,6 +351,7 @@ export const makePersistedAdapter =
|
|
|
350
351
|
devtoolsEnabled,
|
|
351
352
|
debugInstanceId,
|
|
352
353
|
syncPayloadEncoded,
|
|
354
|
+
params,
|
|
353
355
|
},
|
|
354
356
|
})
|
|
355
357
|
.pipe(
|
|
@@ -432,31 +434,27 @@ export const makePersistedAdapter =
|
|
|
432
434
|
|
|
433
435
|
// TODO maybe bring back transfering the initially created in-memory db snapshot instead of
|
|
434
436
|
// re-exporting the db
|
|
435
|
-
const
|
|
437
|
+
const { sqliteDb, snapshotByteLength, migrationsReport } =
|
|
436
438
|
dataFromFile === undefined
|
|
437
|
-
? yield*
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}))
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
450
|
-
const sqliteDb = yield* makeSqliteDb({ _tag: 'in-memory' })
|
|
451
|
-
|
|
452
|
-
sqliteDb.import(initialResult.snapshot)
|
|
439
|
+
? yield* Effect.gen(function* () {
|
|
440
|
+
const { snapshot, migrationsReport } = yield* runInWorker(
|
|
441
|
+
'GetRecreateSnapshot',
|
|
442
|
+
sharedWorker.GetRecreateSnapshot({}),
|
|
443
|
+
)
|
|
444
|
+
const sqliteDb = yield* Effect.acquireRelease(makeSqliteDb({ _tag: 'in-memory' }), (db) =>
|
|
445
|
+
Effect.sync(() => db.close()),
|
|
446
|
+
)
|
|
447
|
+
sqliteDb.import(snapshot)
|
|
448
|
+
return { sqliteDb, snapshotByteLength: snapshot.byteLength, migrationsReport }
|
|
449
|
+
})
|
|
450
|
+
: { ...dataFromFile, migrationsReport: { migrations: [] } }
|
|
453
451
|
|
|
454
452
|
const numberOfTables =
|
|
455
453
|
sqliteDb.select<{ count: number }>(`select count(*) as count from sqlite_master`)[0]?.count ?? 0
|
|
456
454
|
if (numberOfTables === 0) {
|
|
457
455
|
return yield* UnknownError.make({
|
|
458
456
|
cause: `Encountered empty or corrupted database`,
|
|
459
|
-
payload: { snapshotByteLength
|
|
457
|
+
payload: { snapshotByteLength, storageOptions: options.storage },
|
|
460
458
|
})
|
|
461
459
|
}
|
|
462
460
|
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { migrateDb } from '@livestore/common'
|
|
2
|
+
import { configureConnection } from '@livestore/common/leader-thread'
|
|
3
|
+
import { makeSchema, State, SystemTables } from '@livestore/common/schema'
|
|
4
|
+
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
5
|
+
import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
|
|
6
|
+
import { Effect } from '@livestore/utils/effect'
|
|
7
|
+
import { Opfs } from '@livestore/utils/effect/browser'
|
|
8
|
+
|
|
9
|
+
import { getStateDbFileName, readPersistedStateDbFromClientSession, sanitizeOpfsDir } from './persisted-sqlite.ts'
|
|
10
|
+
|
|
11
|
+
/** Exercise the browser snapshot reader against a real AccessHandlePoolVFS file. */
|
|
12
|
+
const run = Effect.gen(function* () {
|
|
13
|
+
const sqlite3 = yield* Effect.promise(() => loadSqlite3Wasm())
|
|
14
|
+
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
15
|
+
const services = yield* Effect.context()
|
|
16
|
+
const storeId = `rebuild-snapshot-${crypto.randomUUID()}`
|
|
17
|
+
const storageOptions = { type: 'opfs' as const }
|
|
18
|
+
const todos = State.SQLite.table({ name: 'todos', columns: { id: State.SQLite.text({ primaryKey: true }) } })
|
|
19
|
+
const schema = makeSchema({ events: [], state: State.SQLite.makeState({ tables: { todos }, materializers: {} }) })
|
|
20
|
+
const persisted = yield* Effect.acquireRelease(
|
|
21
|
+
makeSqliteDb({
|
|
22
|
+
_tag: 'opfs',
|
|
23
|
+
opfsDirectory: yield* sanitizeOpfsDir(undefined, storeId),
|
|
24
|
+
fileName: getStateDbFileName(schema),
|
|
25
|
+
configureDb: (db) => configureConnection(db, { foreignKeys: true }).pipe(Effect.runSyncWith(services)),
|
|
26
|
+
}),
|
|
27
|
+
(db) => Effect.sync(() => db.close()),
|
|
28
|
+
)
|
|
29
|
+
const closeCounts: number[] = []
|
|
30
|
+
const makeSnapshotDb = (input: { _tag: 'in-memory' }) =>
|
|
31
|
+
makeSqliteDb(input).pipe(
|
|
32
|
+
Effect.map((db) => {
|
|
33
|
+
const index = closeCounts.push(0) - 1
|
|
34
|
+
return {
|
|
35
|
+
...db,
|
|
36
|
+
close: () => {
|
|
37
|
+
closeCounts[index] = (closeCounts[index] ?? 0) + 1
|
|
38
|
+
db.close()
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}),
|
|
42
|
+
)
|
|
43
|
+
yield* migrateDb({ db: persisted, schema })
|
|
44
|
+
persisted.execute(todos.insert({ id: 'partial' }))
|
|
45
|
+
const read = readPersistedStateDbFromClientSession({ storeId, storageOptions, schema, makeSqliteDb: makeSnapshotDb })
|
|
46
|
+
const readStatus = read.pipe(Effect.match({ onSuccess: () => 'accepted', onFailure: (error) => error._tag }))
|
|
47
|
+
const incomplete = yield* readStatus
|
|
48
|
+
persisted.execute(`DROP TABLE ${SystemTables.REBUILD_META_TABLE}`)
|
|
49
|
+
const missingMarkerTable = yield* readStatus
|
|
50
|
+
yield* migrateDb({ db: persisted, schema })
|
|
51
|
+
persisted.execute(todos.insert({ id: 'complete' }))
|
|
52
|
+
persisted.execute(`INSERT INTO ${SystemTables.REBUILD_META_TABLE} (id) VALUES (1)`)
|
|
53
|
+
const { sqliteDb } = yield* read
|
|
54
|
+
return {
|
|
55
|
+
incomplete,
|
|
56
|
+
missingMarkerTable,
|
|
57
|
+
complete: 'accepted',
|
|
58
|
+
rows: sqliteDb.select(todos.orderBy('id', 'asc')),
|
|
59
|
+
// Check before the outer scope closes: rejection must release memory immediately.
|
|
60
|
+
closeCountsBeforeScopeExit: [...closeCounts],
|
|
61
|
+
closeCountsAfterScopeExit: closeCounts,
|
|
62
|
+
}
|
|
63
|
+
}).pipe(Effect.scoped, Effect.provide(Opfs.layer))
|
|
64
|
+
|
|
65
|
+
void Effect.runPromise(run).then(
|
|
66
|
+
(result) => postMessage({ result }),
|
|
67
|
+
(error) => postMessage({ error: String(error) }),
|
|
68
|
+
)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { liveStoreStorageFormatVersion } from '@livestore/common'
|
|
1
|
+
import { liveStoreStorageFormatVersion, type MakeSqliteDb } from '@livestore/common'
|
|
2
|
+
import { hasCompletedState } from '@livestore/common/leader-thread'
|
|
2
3
|
import { getStateDbBaseName, type LiveStoreSchema } from '@livestore/common/schema'
|
|
3
4
|
import {
|
|
4
5
|
decodeAccessHandlePoolFilename,
|
|
@@ -34,10 +35,12 @@ export const readPersistedStateDbFromClientSession = Effect.fn(
|
|
|
34
35
|
storageOptions,
|
|
35
36
|
storeId,
|
|
36
37
|
schema,
|
|
38
|
+
makeSqliteDb,
|
|
37
39
|
}: {
|
|
38
40
|
storageOptions: WorkerSchema.StorageType
|
|
39
41
|
storeId: string
|
|
40
42
|
schema: LiveStoreSchema
|
|
43
|
+
makeSqliteDb: (input: { _tag: 'in-memory' }) => ReturnType<MakeSqliteDb>
|
|
41
44
|
}) {
|
|
42
45
|
const accessHandlePoolDirString = yield* sanitizeOpfsDir(storageOptions.directory, storeId)
|
|
43
46
|
|
|
@@ -81,7 +84,33 @@ export const readPersistedStateDbFromClientSession = Effect.fn(
|
|
|
81
84
|
})
|
|
82
85
|
}
|
|
83
86
|
|
|
84
|
-
|
|
87
|
+
const snapshot = new Uint8Array(stateDbBuffer)
|
|
88
|
+
const sqliteDb = yield* Effect.acquireRelease(
|
|
89
|
+
makeSqliteDb({ _tag: 'in-memory' }).pipe(
|
|
90
|
+
Effect.tap((db) =>
|
|
91
|
+
Effect.try({
|
|
92
|
+
try: () => {
|
|
93
|
+
db.import(snapshot)
|
|
94
|
+
return hasCompletedState(db)
|
|
95
|
+
},
|
|
96
|
+
catch: (cause) => new PersistedSqliteError({ message: 'Could not load persisted state snapshot', cause }),
|
|
97
|
+
}).pipe(
|
|
98
|
+
Effect.filterOrFail(
|
|
99
|
+
(completed) => completed,
|
|
100
|
+
() =>
|
|
101
|
+
new PersistedSqliteError({
|
|
102
|
+
message: 'Persisted state rebuild is incomplete; waiting for leader recovery',
|
|
103
|
+
}),
|
|
104
|
+
),
|
|
105
|
+
// A failed acquisition has no scope finalizer yet, so release it here.
|
|
106
|
+
Effect.onError(() => Effect.sync(() => db.close())),
|
|
107
|
+
),
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
(db) => Effect.sync(() => db.close()),
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
return { sqliteDb, snapshotByteLength: snapshot.byteLength }
|
|
85
114
|
},
|
|
86
115
|
Effect.logWarnIfTakesLongerThan({
|
|
87
116
|
duration: 1000,
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
SyncState,
|
|
9
9
|
UnknownError,
|
|
10
10
|
} from '@livestore/common'
|
|
11
|
-
import { StreamEventsOptionsFields } from '@livestore/common/leader-thread'
|
|
11
|
+
import { StateRebuildBatchSizeSchema, StreamEventsOptionsFields } from '@livestore/common/leader-thread'
|
|
12
12
|
import { EventSequenceNumber, LiveStoreEvent } from '@livestore/common/schema'
|
|
13
13
|
import { Rpc, RpcGroup, Schema, Transferable } from '@livestore/utils/effect'
|
|
14
14
|
import * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
@@ -53,6 +53,8 @@ export class LeaderWorkerOuterInitialMessage extends Rpc.make('InitialMessage',
|
|
|
53
53
|
|
|
54
54
|
export class LeaderWorkerOuterRpcs extends RpcGroup.make(LeaderWorkerOuterInitialMessage) {}
|
|
55
55
|
|
|
56
|
+
export const LeaderWorkerParams = Schema.Struct({ stateRebuildBatchSize: StateRebuildBatchSizeSchema })
|
|
57
|
+
|
|
56
58
|
// TODO unify this code with schema from node adapter
|
|
57
59
|
export class LeaderWorkerInnerInitialMessage extends Rpc.make('InitialMessage', {
|
|
58
60
|
payload: {
|
|
@@ -62,6 +64,7 @@ export class LeaderWorkerInnerInitialMessage extends Rpc.make('InitialMessage',
|
|
|
62
64
|
clientId: Schema.String,
|
|
63
65
|
debugInstanceId: Schema.String,
|
|
64
66
|
syncPayloadEncoded: Schema.UndefinedOr(Schema.Json),
|
|
67
|
+
params: LeaderWorkerParams,
|
|
65
68
|
},
|
|
66
69
|
success: Schema.Void,
|
|
67
70
|
error: UnknownError,
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
OtelTracer,
|
|
25
25
|
Queue,
|
|
26
26
|
References,
|
|
27
|
+
RpcSerialization,
|
|
27
28
|
RpcServer,
|
|
28
29
|
RpcWorker,
|
|
29
30
|
Schema,
|
|
@@ -132,12 +133,13 @@ const makeMessagePortRpcServerProtocol = (port: MessagePort): Layer.Layer<RpcSer
|
|
|
132
133
|
send: (_clientId, response, transferables) =>
|
|
133
134
|
Effect.sync(() => port.postMessage([1, response], { transfer: (transferables ?? []) as Transferable[] })),
|
|
134
135
|
end: () => Effect.void,
|
|
135
|
-
clientIds: Effect.
|
|
136
|
+
clientIds: Effect.succeed(clientIds),
|
|
136
137
|
initialMessage: Effect.asSome(Deferred.await(initialMessage)),
|
|
137
138
|
supportsAck: true,
|
|
138
139
|
supportsTransferables: true,
|
|
139
140
|
supportsSpanPropagation: true,
|
|
140
141
|
supportsNotifications: true,
|
|
142
|
+
codecFor: RpcSerialization.json.codecFor,
|
|
141
143
|
run: (writeRequest) =>
|
|
142
144
|
Effect.gen(function* () {
|
|
143
145
|
const context = yield* Effect.context<never>()
|
|
@@ -189,7 +191,7 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions, syncPayloadSchema }:
|
|
|
189
191
|
|
|
190
192
|
const leaderThreadContextOnce = yield* Effect.cached(
|
|
191
193
|
Effect.gen(function* () {
|
|
192
|
-
const { storageOptions, storeId, clientId, devtoolsEnabled, debugInstanceId, syncPayloadEncoded } =
|
|
194
|
+
const { storageOptions, storeId, clientId, devtoolsEnabled, debugInstanceId, syncPayloadEncoded, params } =
|
|
193
195
|
yield* RpcWorker.initialMessage(WorkerSchema.LeaderWorkerInnerInitialMessage.payloadSchema)
|
|
194
196
|
|
|
195
197
|
const sqlite3 = yield* Effect.promise(() => loadSqlite3Wasm())
|
|
@@ -280,6 +282,7 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions, syncPayloadSchema }:
|
|
|
280
282
|
shutdownChannel,
|
|
281
283
|
syncPayloadEncoded,
|
|
282
284
|
syncPayloadSchema: syncPayloadSchema as Schema.Decoder<Schema.Json, never> | undefined,
|
|
285
|
+
params,
|
|
283
286
|
...(bootWarning !== undefined ? { bootWarning } : {}),
|
|
284
287
|
}).pipe(Layer.provide(StateHead.layer({ dbState }))),
|
|
285
288
|
leaderThreadScope,
|
|
@@ -132,6 +132,7 @@ const makeWorkerRunner = Effect.gen(function* () {
|
|
|
132
132
|
})
|
|
133
133
|
|
|
134
134
|
// Cache first-applied invariants to enforce stability across leader transitions
|
|
135
|
+
// Per-client runtime params are intentionally excluded so a new leader can apply its own tuning.
|
|
135
136
|
const InvariantsSchema = Schema.Struct({
|
|
136
137
|
storeId: Schema.String,
|
|
137
138
|
storageOptions: WorkerSchema.StorageType,
|
|
@@ -148,7 +149,7 @@ const makeWorkerRunner = Effect.gen(function* () {
|
|
|
148
149
|
// sends a new MessagePort to the shared worker which proxies messages to the new leader thread.
|
|
149
150
|
UpdateMessagePort: ({ port, initial, liveStoreVersion: clientLiveStoreVersion }) =>
|
|
150
151
|
Effect.gen(function* () {
|
|
151
|
-
// Enforce
|
|
152
|
+
// Enforce configuration stability across leader transitions.
|
|
152
153
|
const invariants: Invariants = {
|
|
153
154
|
storeId: initial.storeId,
|
|
154
155
|
storageOptions: initial.storageOptions,
|