@livestore/adapter-web 0.3.0-dev.33 → 0.3.0-dev.36

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.
@@ -19,55 +19,53 @@ export const readPersistedAppDbFromClientSession = ({
19
19
  storeId: string
20
20
  schema: LiveStoreSchema
21
21
  }) =>
22
- Effect.gen(function* () {
23
- return yield* Effect.promise(async () => {
24
- const directory = sanitizeOpfsDir(storageOptions.directory, storeId)
25
- const sahPoolOpaqueDir = await OpfsUtils.getDirHandle(directory).catch(() => undefined)
22
+ Effect.promise(async () => {
23
+ const directory = sanitizeOpfsDir(storageOptions.directory, storeId)
24
+ const sahPoolOpaqueDir = await OpfsUtils.getDirHandle(directory).catch(() => undefined)
26
25
 
27
- if (sahPoolOpaqueDir === undefined) {
28
- return undefined
29
- }
26
+ if (sahPoolOpaqueDir === undefined) {
27
+ return undefined
28
+ }
30
29
 
31
- const tryGetDbFile = async (fileHandle: FileSystemFileHandle) => {
32
- const file = await fileHandle.getFile()
33
- const fileName = await decodeSAHPoolFilename(file)
34
- return fileName ? { fileName, file } : undefined
35
- }
30
+ const tryGetDbFile = async (fileHandle: FileSystemFileHandle) => {
31
+ const file = await fileHandle.getFile()
32
+ const fileName = await decodeSAHPoolFilename(file)
33
+ return fileName ? { fileName, file } : undefined
34
+ }
36
35
 
37
- const getAllFiles = async (asyncIterator: AsyncIterable<FileSystemHandle>): Promise<FileSystemFileHandle[]> => {
38
- const results: FileSystemFileHandle[] = []
39
- for await (const value of asyncIterator) {
40
- if (value.kind === 'file') {
41
- results.push(value as FileSystemFileHandle)
42
- }
36
+ const getAllFiles = async (asyncIterator: AsyncIterable<FileSystemHandle>): Promise<FileSystemFileHandle[]> => {
37
+ const results: FileSystemFileHandle[] = []
38
+ for await (const value of asyncIterator) {
39
+ if (value.kind === 'file') {
40
+ results.push(value as FileSystemFileHandle)
43
41
  }
44
- return results
45
42
  }
43
+ return results
44
+ }
46
45
 
47
- const files = await getAllFiles(sahPoolOpaqueDir.values())
48
-
49
- const fileResults = await Promise.all(files.map(tryGetDbFile))
46
+ const files = await getAllFiles(sahPoolOpaqueDir.values())
50
47
 
51
- const appDbFileName = '/' + getAppDbFileName(schema)
48
+ const fileResults = await Promise.all(files.map(tryGetDbFile))
52
49
 
53
- const dbFileRes = fileResults.find((_) => _?.fileName === appDbFileName)
54
- // console.debug('fileResults', fileResults, 'dbFileRes', dbFileRes)
50
+ const appDbFileName = '/' + getStateDbFileName(schema)
55
51
 
56
- if (dbFileRes !== undefined) {
57
- const data = await dbFileRes.file.slice(HEADER_OFFSET_DATA).arrayBuffer()
58
- // console.debug('readPersistedAppDbFromClientSession', data.byteLength, data)
52
+ const dbFileRes = fileResults.find((_) => _?.fileName === appDbFileName)
53
+ // console.debug('fileResults', fileResults, 'dbFileRes', dbFileRes)
59
54
 
60
- // Given the SAH pool always eagerly creates files with empty non-header data,
61
- // we want to return undefined if the file exists but is empty
62
- if (data.byteLength === 0) {
63
- return undefined
64
- }
55
+ if (dbFileRes !== undefined) {
56
+ const data = await dbFileRes.file.slice(HEADER_OFFSET_DATA).arrayBuffer()
57
+ // console.debug('readPersistedAppDbFromClientSession', data.byteLength, data)
65
58
 
66
- return new Uint8Array(data)
59
+ // Given the SAH pool always eagerly creates files with empty non-header data,
60
+ // we want to return undefined if the file exists but is empty
61
+ if (data.byteLength === 0) {
62
+ return undefined
67
63
  }
68
64
 
69
- return undefined
70
- })
65
+ return new Uint8Array(data)
66
+ }
67
+
68
+ return undefined
71
69
  }).pipe(
72
70
  Effect.logWarnIfTakesLongerThan({
73
71
  duration: 1000,
@@ -138,7 +136,8 @@ export const sanitizeOpfsDir = (directory: string | undefined, storeId: string)
138
136
  return `${directory}@${liveStoreStorageFormatVersion}`
139
137
  }
140
138
 
141
- export const getAppDbFileName = (schema: LiveStoreSchema) => {
142
- const schemaHashSuffix = schema.migrationOptions.strategy === 'manual' ? 'fixed' : schema.hash.toString()
143
- return `app${schemaHashSuffix}.db`
139
+ export const getStateDbFileName = (schema: LiveStoreSchema) => {
140
+ const schemaHashSuffix =
141
+ schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString()
142
+ return `state${schemaHashSuffix}.db`
144
143
  }
@@ -27,7 +27,7 @@ import {
27
27
  import type * as otel from '@opentelemetry/api'
28
28
 
29
29
  import * as OpfsUtils from '../../opfs-utils.js'
30
- import { getAppDbFileName, sanitizeOpfsDir } from '../common/persisted-sqlite.js'
30
+ import { getStateDbFileName, sanitizeOpfsDir } from '../common/persisted-sqlite.js'
31
31
  import { makeShutdownChannel } from '../common/shutdown-channel.js'
32
32
  import * as WorkerSchema from '../common/worker-schema.js'
33
33
 
@@ -108,11 +108,11 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
108
108
  const makeSqliteDb = sqliteDbFactory({ sqlite3 })
109
109
  const runtime = yield* Effect.runtime<never>()
110
110
 
111
- const makeDb = (kind: 'app' | 'eventlog') =>
111
+ const makeDb = (kind: 'state' | 'eventlog') =>
112
112
  makeSqliteDb({
113
113
  _tag: 'opfs',
114
114
  opfsDirectory: sanitizeOpfsDir(storageOptions.directory, storeId),
115
- fileName: kind === 'app' ? getAppDbFileName(schema) : 'eventlog.db',
115
+ fileName: kind === 'state' ? getStateDbFileName(schema) : 'eventlog.db',
116
116
  configureDb: (db) =>
117
117
  configureConnection(db, {
118
118
  // The persisted databases use the AccessHandlePoolVFS which always uses a single database connection.
@@ -125,11 +125,11 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
125
125
  }).pipe(Effect.acquireRelease((db) => Effect.try(() => db.close()).pipe(Effect.ignoreLogged)))
126
126
 
127
127
  // Might involve some async work, so we're running them concurrently
128
- const [dbReadModel, dbEventlog] = yield* Effect.all([makeDb('app'), makeDb('eventlog')], {
128
+ const [dbState, dbEventlog] = yield* Effect.all([makeDb('state'), makeDb('eventlog')], {
129
129
  concurrency: 2,
130
130
  })
131
131
 
132
- const devtoolsOptions = yield* makeDevtoolsOptions({ devtoolsEnabled, dbReadModel, dbEventlog })
132
+ const devtoolsOptions = yield* makeDevtoolsOptions({ devtoolsEnabled, dbState, dbEventlog })
133
133
  const shutdownChannel = yield* makeShutdownChannel(storeId)
134
134
 
135
135
  return makeLeaderThreadLayer({
@@ -138,7 +138,7 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
138
138
  clientId,
139
139
  makeSqliteDb,
140
140
  syncOptions,
141
- dbReadModel,
141
+ dbState,
142
142
  dbEventlog,
143
143
  devtoolsOptions,
144
144
  shutdownChannel,
@@ -162,7 +162,7 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
162
162
 
163
163
  // return cachedSnapshot ?? workerCtx.db.export()
164
164
 
165
- const snapshot = workerCtx.dbReadModel.export()
165
+ const snapshot = workerCtx.dbState.export()
166
166
  return { snapshot, migrationsReport: workerCtx.initialState.migrationsReport }
167
167
  }).pipe(
168
168
  UnexpectedError.mapToUnexpectedError,
@@ -186,7 +186,7 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
186
186
  ),
187
187
  ).pipe(Effect.uninterruptible, Effect.withSpan('@livestore/adapter-web:worker:PushToLeader')),
188
188
  Export: () =>
189
- Effect.andThen(LeaderThreadCtx, (_) => _.dbReadModel.export()).pipe(
189
+ Effect.andThen(LeaderThreadCtx, (_) => _.dbState.export()).pipe(
190
190
  UnexpectedError.mapToUnexpectedError,
191
191
  Effect.withSpan('@livestore/adapter-web:worker:Export'),
192
192
  ),
@@ -228,11 +228,11 @@ const makeWorkerRunnerInner = ({ schema, sync: syncOptions }: WorkerOptions) =>
228
228
 
229
229
  const makeDevtoolsOptions = ({
230
230
  devtoolsEnabled,
231
- dbReadModel,
231
+ dbState,
232
232
  dbEventlog,
233
233
  }: {
234
234
  devtoolsEnabled: boolean
235
- dbReadModel: SqliteDb
235
+ dbState: SqliteDb
236
236
  dbEventlog: SqliteDb
237
237
  }): Effect.Effect<DevtoolsOptions, UnexpectedError, Scope.Scope | WebmeshWorker.CacheService> =>
238
238
  Effect.gen(function* () {
@@ -251,7 +251,7 @@ const makeDevtoolsOptions = ({
251
251
  schema: { listen: Devtools.Leader.MessageToApp, send: Devtools.Leader.MessageFromApp },
252
252
  }),
253
253
  persistenceInfo: {
254
- readModel: dbReadModel.metadata.persistenceInfo,
254
+ state: dbState.metadata.persistenceInfo,
255
255
  eventlog: dbEventlog.metadata.persistenceInfo,
256
256
  },
257
257
  }
package/.eslintrc.cjs DELETED
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['../.eslintrc.cjs'],
3
- rules: {
4
- 'unicorn/prefer-global-this': 'off',
5
- },
6
- }
package/tmp/pack.tgz DELETED
Binary file
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "resolveJsonModule": true,
7
- "tsBuildInfoFile": "./dist/.tsbuildinfo"
8
- },
9
- "include": ["./src"],
10
- "references": [
11
- { "path": "../common" },
12
- { "path": "../utils" },
13
- { "path": "../sqlite-wasm" },
14
- { "path": "../webmesh" },
15
- { "path": "../devtools-web-common" },
16
- ]
17
- }