@livestore/adapter-web 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/common/connection.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.d.ts +7 -8
- package/dist/in-memory/in-memory-adapter.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.js +37 -30
- 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 +63 -83
- package/dist/single-tab/single-tab-adapter.js.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.d.ts +3 -4
- package/dist/web-worker/client-session/client-session-devtools.d.ts.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.js +7 -5
- package/dist/web-worker/client-session/client-session-devtools.js.map +1 -1
- package/dist/web-worker/client-session/devtools-web-channel.d.ts +20 -0
- package/dist/web-worker/client-session/devtools-web-channel.d.ts.map +1 -0
- package/dist/web-worker/client-session/devtools-web-channel.js +34 -0
- package/dist/web-worker/client-session/devtools-web-channel.js.map +1 -0
- package/dist/web-worker/client-session/persisted-adapter.d.ts.map +1 -1
- package/dist/web-worker/client-session/persisted-adapter.js +76 -85
- 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 +23 -10
- package/dist/web-worker/common/persisted-sqlite.d.ts.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.js +29 -25
- 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 +14 -0
- package/dist/web-worker/common/rpc-worker.d.ts.map +1 -0
- package/dist/web-worker/common/rpc-worker.js +8 -0
- package/dist/web-worker/common/rpc-worker.js.map +1 -0
- package/dist/web-worker/common/shutdown-channel.d.ts.map +1 -1
- package/dist/web-worker/common/webmesh-node-names.d.ts +4 -0
- package/dist/web-worker/common/webmesh-node-names.d.ts.map +1 -0
- package/dist/web-worker/common/webmesh-node-names.js +2 -0
- package/dist/web-worker/common/webmesh-node-names.js.map +1 -0
- package/dist/web-worker/common/worker-disconnect-channel.d.ts +1 -4
- package/dist/web-worker/common/worker-disconnect-channel.d.ts.map +1 -1
- package/dist/web-worker/common/worker-disconnect-channel.js +1 -2
- package/dist/web-worker/common/worker-disconnect-channel.js.map +1 -1
- package/dist/web-worker/common/worker-schema.d.ts +275 -211
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.js +71 -52
- package/dist/web-worker/common/worker-schema.js.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts +6 -8
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.js +147 -94
- package/dist/web-worker/leader-worker/make-leader-worker.js.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts +2 -2
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.js +54 -66
- package/dist/web-worker/shared-worker/make-shared-worker.js.map +1 -1
- package/dist/web-worker/vite-dev-polyfill.js +1 -2
- package/dist/web-worker/vite-dev-polyfill.js.map +1 -1
- package/package.json +38 -48
- package/src/in-memory/in-memory-adapter.ts +73 -58
- package/src/single-tab/single-tab-adapter.ts +112 -114
- package/src/web-worker/client-session/client-session-devtools.ts +16 -8
- package/src/web-worker/client-session/devtools-web-channel.ts +57 -0
- package/src/web-worker/client-session/persisted-adapter.ts +122 -124
- package/src/web-worker/common/persisted-sqlite.test-fixture.ts +68 -0
- package/src/web-worker/common/persisted-sqlite.ts +72 -50
- package/src/web-worker/common/rpc-worker.ts +39 -0
- package/src/web-worker/common/webmesh-node-names.ts +1 -0
- package/src/web-worker/common/worker-disconnect-channel.ts +1 -1
- package/src/web-worker/common/worker-schema.ts +132 -162
- package/src/web-worker/leader-worker/make-leader-worker.ts +347 -219
- package/src/web-worker/shared-worker/make-shared-worker.ts +95 -85
- package/src/web-worker/vite-dev-polyfill.ts +1 -2
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { Devtools, liveStoreVersion } from '@livestore/common'
|
|
2
2
|
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
3
|
-
import * as DevtoolsWeb from '@livestore/devtools-web-common/web-channel'
|
|
4
3
|
import { isDevEnv } from '@livestore/utils'
|
|
5
|
-
import type { Worker } from '@livestore/utils/effect'
|
|
6
4
|
import { Effect, Stream } from '@livestore/utils/effect'
|
|
7
5
|
import { WebChannelBrowser } from '@livestore/utils/effect/browser'
|
|
8
6
|
import * as Webmesh from '@livestore/webmesh'
|
|
7
|
+
import * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
8
|
+
|
|
9
|
+
import type { WebmeshWorkerProxy } from '../common/rpc-worker.ts'
|
|
10
|
+
import { makeSharedWorkerNodeName } from '../common/webmesh-node-names.ts'
|
|
11
|
+
import * as DevtoolsWeb from './devtools-web-channel.ts'
|
|
9
12
|
|
|
10
13
|
export const logDevtoolsUrl = Effect.fn('@livestore/adapter-web:client-session:devtools:logDevtoolsUrl')(function* ({
|
|
11
14
|
schema,
|
|
@@ -19,7 +22,8 @@ export const logDevtoolsUrl = Effect.fn('@livestore/adapter-web:client-session:d
|
|
|
19
22
|
sessionId: string
|
|
20
23
|
}) {
|
|
21
24
|
if (isDevEnv() === true) {
|
|
22
|
-
const devtoolsPath =
|
|
25
|
+
const devtoolsPath =
|
|
26
|
+
(globalThis as unknown as { LIVESTORE_DEVTOOLS_PATH?: string }).LIVESTORE_DEVTOOLS_PATH ?? `/_livestore`
|
|
23
27
|
const devtoolsBaseUrl = `${location.origin}${devtoolsPath}`
|
|
24
28
|
|
|
25
29
|
// Check whether devtools are available and then log the URL
|
|
@@ -57,7 +61,7 @@ export const connectWebmeshNodeClientSession = Effect.fn(function* ({
|
|
|
57
61
|
}: {
|
|
58
62
|
webmeshNode: Webmesh.MeshNode
|
|
59
63
|
sessionInfo: Devtools.SessionInfo.SessionInfo
|
|
60
|
-
sharedWorker:
|
|
64
|
+
sharedWorker: WebmeshWorkerProxy
|
|
61
65
|
devtoolsEnabled: boolean
|
|
62
66
|
schema: LiveStoreSchema
|
|
63
67
|
}) {
|
|
@@ -79,9 +83,13 @@ export const connectWebmeshNodeClientSession = Effect.fn(function* ({
|
|
|
79
83
|
DevtoolsWeb.ClientSessionContentscriptMainReq.make({ clientId, sessionId, storeId }),
|
|
80
84
|
)
|
|
81
85
|
|
|
82
|
-
const { tabId } = yield* clientSessionStaticChannel.listen.pipe(
|
|
86
|
+
const { tabId } = yield* clientSessionStaticChannel.listen.pipe(
|
|
87
|
+
Stream.mapEffect(Effect.fromResult),
|
|
88
|
+
Stream.runHead,
|
|
89
|
+
Effect.flatMap(Effect.fromOption),
|
|
90
|
+
)
|
|
83
91
|
|
|
84
|
-
const contentscriptMainNodeName = DevtoolsWeb.
|
|
92
|
+
const contentscriptMainNodeName = DevtoolsWeb.makeBrowserExtensionNodeName.contentscriptMain(tabId)
|
|
85
93
|
|
|
86
94
|
const contentscriptMainChannel = yield* WebChannelBrowser.windowChannel({
|
|
87
95
|
listenWindow: window,
|
|
@@ -97,9 +105,9 @@ export const connectWebmeshNodeClientSession = Effect.fn(function* ({
|
|
|
97
105
|
Effect.forkScoped,
|
|
98
106
|
)
|
|
99
107
|
|
|
100
|
-
yield*
|
|
108
|
+
yield* WebmeshWorker.connectViaWorker({
|
|
101
109
|
node: webmeshNode,
|
|
102
|
-
target:
|
|
110
|
+
target: makeSharedWorkerNodeName({ storeId }),
|
|
103
111
|
worker: sharedWorker,
|
|
104
112
|
})
|
|
105
113
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Devtools } from '@livestore/common'
|
|
2
|
+
import { type Scope, type WebChannel, Effect, Schema } from '@livestore/utils/effect'
|
|
3
|
+
import { WebChannelBrowser } from '@livestore/utils/effect/browser'
|
|
4
|
+
|
|
5
|
+
export const makeSessionInfoBroadcastChannel: Effect.Effect<
|
|
6
|
+
WebChannel.WebChannel<Devtools.SessionInfo.Message, Devtools.SessionInfo.Message>,
|
|
7
|
+
never,
|
|
8
|
+
Scope.Scope
|
|
9
|
+
> = WebChannelBrowser.broadcastChannel({
|
|
10
|
+
channelName: 'session-info',
|
|
11
|
+
schema: Devtools.SessionInfo.Message,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export const makeBrowserExtensionNodeName = {
|
|
15
|
+
contentscriptMain: (tabId: number) => `contentscript-main-${tabId}`,
|
|
16
|
+
contentscriptIframe: (tabId: number) => `contentscript-iframe-${tabId}`,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ClientSessionContentscriptMainReq = Schema.TaggedStruct('ClientSessionContentscriptMainReq', {
|
|
20
|
+
storeId: Schema.String,
|
|
21
|
+
clientId: Schema.String,
|
|
22
|
+
sessionId: Schema.String,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export const ClientSessionContentscriptMainRes = Schema.TaggedStruct('ClientSessionContentscriptMainRes', {
|
|
26
|
+
tabId: Schema.Finite,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export const makeStaticClientSessionChannel: {
|
|
30
|
+
contentscriptMain: Effect.Effect<
|
|
31
|
+
WebChannel.WebChannel<typeof ClientSessionContentscriptMainReq.Type, typeof ClientSessionContentscriptMainRes.Type>,
|
|
32
|
+
never,
|
|
33
|
+
Scope.Scope
|
|
34
|
+
>
|
|
35
|
+
clientSession: Effect.Effect<
|
|
36
|
+
WebChannel.WebChannel<typeof ClientSessionContentscriptMainRes.Type, typeof ClientSessionContentscriptMainReq.Type>,
|
|
37
|
+
never,
|
|
38
|
+
Scope.Scope
|
|
39
|
+
>
|
|
40
|
+
} = {
|
|
41
|
+
contentscriptMain: Effect.suspend(() =>
|
|
42
|
+
WebChannelBrowser.windowChannel({
|
|
43
|
+
listenWindow: window,
|
|
44
|
+
sendWindow: window,
|
|
45
|
+
schema: { listen: ClientSessionContentscriptMainReq, send: ClientSessionContentscriptMainRes },
|
|
46
|
+
ids: { own: 'contentscript-main-static', other: 'client-session-static' },
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
clientSession: Effect.suspend(() =>
|
|
50
|
+
WebChannelBrowser.windowChannel({
|
|
51
|
+
listenWindow: window,
|
|
52
|
+
sendWindow: window,
|
|
53
|
+
schema: { listen: ClientSessionContentscriptMainRes, send: ClientSessionContentscriptMainReq },
|
|
54
|
+
ids: { own: 'client-session-static', other: 'contentscript-main-static' },
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
}
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import type { Adapter, BootWarningReason, ClientSession, LockStatus } from '@livestore/common'
|
|
2
2
|
import {
|
|
3
3
|
IntentionalShutdownCause,
|
|
4
|
-
isWorkerTransportError,
|
|
5
4
|
liveStoreVersion,
|
|
6
5
|
makeClientSession,
|
|
6
|
+
StateHead,
|
|
7
7
|
StoreInterrupted,
|
|
8
|
-
sessionChangesetMetaTable,
|
|
9
8
|
UnknownError,
|
|
10
9
|
} from '@livestore/common'
|
|
11
10
|
// TODO bring back - this currently doesn't work due to https://github.com/vitejs/vite/issues/8427
|
|
12
11
|
// NOTE We're using a non-relative import here for Vite to properly resolve the import during app builds
|
|
13
12
|
// import LiveStoreSharedWorker from '@livestore/adapter-web/internal-shared-worker?sharedworker'
|
|
14
|
-
import { EventSequenceNumber } from '@livestore/common/schema'
|
|
15
13
|
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
16
14
|
import { isDevEnv, omitUndefineds, shouldNeverHappen, tryAsFunctionAndNew } from '@livestore/utils'
|
|
17
15
|
import {
|
|
@@ -21,13 +19,13 @@ import {
|
|
|
21
19
|
Exit,
|
|
22
20
|
Fiber,
|
|
23
21
|
Layer,
|
|
24
|
-
Option,
|
|
25
22
|
Queue,
|
|
23
|
+
RpcClient,
|
|
24
|
+
RpcWorker,
|
|
26
25
|
Schema,
|
|
27
26
|
Stream,
|
|
28
27
|
Subscribable,
|
|
29
28
|
SubscriptionRef,
|
|
30
|
-
Worker,
|
|
31
29
|
} from '@livestore/utils/effect'
|
|
32
30
|
import { BrowserWorker, Opfs, WebError, WebLock } from '@livestore/utils/effect/browser'
|
|
33
31
|
import { nanoid } from '@livestore/utils/nanoid'
|
|
@@ -37,6 +35,12 @@ import {
|
|
|
37
35
|
readPersistedStateDbFromClientSession,
|
|
38
36
|
resetPersistedDataFromClientSession,
|
|
39
37
|
} from '../common/persisted-sqlite.ts'
|
|
38
|
+
import {
|
|
39
|
+
type WithoutRpcClientError,
|
|
40
|
+
dieOnRpcClientError,
|
|
41
|
+
dieOnRpcClientErrorStream,
|
|
42
|
+
makeWebmeshWorkerProxy,
|
|
43
|
+
} from '../common/rpc-worker.ts'
|
|
40
44
|
import { makeShutdownChannel } from '../common/shutdown-channel.ts'
|
|
41
45
|
import { DedicatedWorkerDisconnectBroadcast, makeWorkerDisconnectChannel } from '../common/worker-disconnect-channel.ts'
|
|
42
46
|
import * as WorkerSchema from '../common/worker-schema.ts'
|
|
@@ -78,9 +82,7 @@ export type WebAdapterOptions = {
|
|
|
78
82
|
*/
|
|
79
83
|
sharedWorker:
|
|
80
84
|
| ((options: { name: string }) => globalThis.SharedWorker)
|
|
81
|
-
| (new (options: {
|
|
82
|
-
name: string
|
|
83
|
-
}) => globalThis.SharedWorker)
|
|
85
|
+
| (new (options: { name: string }) => globalThis.SharedWorker)
|
|
84
86
|
/**
|
|
85
87
|
* Specifies where to persist data for this adapter
|
|
86
88
|
*/
|
|
@@ -187,6 +189,7 @@ export const makePersistedAdapter =
|
|
|
187
189
|
shutdown,
|
|
188
190
|
syncPayloadSchema: _syncPayloadSchema,
|
|
189
191
|
syncPayloadEncoded,
|
|
192
|
+
params,
|
|
190
193
|
} = adapterArgs
|
|
191
194
|
|
|
192
195
|
// NOTE: The schema travels with the worker bundle (developers call
|
|
@@ -201,11 +204,12 @@ export const makePersistedAdapter =
|
|
|
201
204
|
yield* Queue.offer(bootStatusQueue, { stage: 'loading' })
|
|
202
205
|
|
|
203
206
|
const sqlite3 = yield* Effect.promise(() => loadSqlite3())
|
|
207
|
+
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
204
208
|
|
|
205
209
|
const LIVESTORE_TAB_LOCK = `livestore-tab-lock-${storeId}`
|
|
206
210
|
const LIVESTORE_SHARED_WORKER_TERMINATION_LOCK = `livestore-shared-worker-termination-lock-${storeId}`
|
|
207
211
|
|
|
208
|
-
const storageOptions = yield* Schema.
|
|
212
|
+
const storageOptions = yield* Schema.decodeEffect(WorkerSchema.StorageType)(options.storage)
|
|
209
213
|
|
|
210
214
|
const shutdownChannel = yield* makeShutdownChannel(storeId)
|
|
211
215
|
|
|
@@ -231,11 +235,10 @@ export const makePersistedAdapter =
|
|
|
231
235
|
// we're here trying to get the snapshot directly from storage
|
|
232
236
|
// we usually speeds up the boot process by a lot.
|
|
233
237
|
// We need to be extra careful though to not run into any race conditions or inconsistencies.
|
|
234
|
-
// TODO also verify persisted data
|
|
235
238
|
const dataFromFile =
|
|
236
239
|
options.experimental?.disableFastPath === true || opfsWarning !== undefined
|
|
237
240
|
? undefined
|
|
238
|
-
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema }).pipe(
|
|
241
|
+
: yield* readPersistedStateDbFromClientSession({ storageOptions, storeId, schema, makeSqliteDb }).pipe(
|
|
239
242
|
Effect.tapError((error) =>
|
|
240
243
|
Effect.logDebug('[@livestore/adapter-web:client-session] Could not read persisted state db', error, {
|
|
241
244
|
storeId,
|
|
@@ -253,7 +256,7 @@ export const makePersistedAdapter =
|
|
|
253
256
|
const workerDisconnectChannel = yield* makeWorkerDisconnectChannel(storeId)
|
|
254
257
|
|
|
255
258
|
yield* shutdownChannel.listen.pipe(
|
|
256
|
-
Stream.
|
|
259
|
+
Stream.mapEffect(Effect.fromResult),
|
|
257
260
|
Stream.tap((cause) =>
|
|
258
261
|
shutdown(cause._tag === 'IntentionalShutdownCause' ? Exit.succeed(cause) : Exit.fail(cause)),
|
|
259
262
|
),
|
|
@@ -271,17 +274,17 @@ export const makePersistedAdapter =
|
|
|
271
274
|
yield* Effect.addFinalizer(() => WebLock.waitForLock(LIVESTORE_SHARED_WORKER_TERMINATION_LOCK))
|
|
272
275
|
}
|
|
273
276
|
|
|
274
|
-
const sharedWorkerContext = yield* Layer.build(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
277
|
+
const sharedWorkerContext = yield* Layer.build(
|
|
278
|
+
RpcClient.layerProtocolWorker({ size: 1, concurrency: 100 }).pipe(
|
|
279
|
+
Layer.provide(BrowserWorker.layer(() => sharedWebWorker)),
|
|
280
|
+
),
|
|
281
|
+
)
|
|
282
|
+
const sharedWorker = yield* RpcClient.make(WorkerSchema.SharedWorkerRpcs).pipe(
|
|
279
283
|
Effect.provide(sharedWorkerContext),
|
|
280
284
|
Effect.tapCauseLogPretty,
|
|
281
|
-
|
|
282
|
-
Effect.
|
|
285
|
+
UnknownError.mapToUnknownError,
|
|
286
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
283
287
|
Effect.withSpan('@livestore/adapter-web:client-session:setupSharedWorker'),
|
|
284
|
-
Effect.forkScoped,
|
|
285
288
|
)
|
|
286
289
|
|
|
287
290
|
const lockDeferred = yield* Deferred.make<void>()
|
|
@@ -316,38 +319,44 @@ export const makePersistedAdapter =
|
|
|
316
319
|
// and adding the `sessionId` to make it easier to debug which session a worker belongs to in logs
|
|
317
320
|
const worker = tryAsFunctionAndNew(options.worker, { name: `livestore-worker-${storeId}-${sessionId}` })
|
|
318
321
|
|
|
319
|
-
yield*
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
322
|
+
const outerWorkerContext = yield* Layer.build(
|
|
323
|
+
RpcClient.layerProtocolWorker({ size: 1 }).pipe(
|
|
324
|
+
Layer.provide(BrowserWorker.layer(() => worker)),
|
|
325
|
+
Layer.provide(
|
|
326
|
+
RpcWorker.layerInitialMessage(
|
|
327
|
+
WorkerSchema.LeaderWorkerOuterInitialMessage.payloadSchema,
|
|
328
|
+
Effect.succeed({ port: mc.port1, storeId, clientId }),
|
|
329
|
+
),
|
|
330
|
+
),
|
|
331
|
+
),
|
|
332
|
+
)
|
|
333
|
+
yield* RpcClient.make(WorkerSchema.LeaderWorkerOuterRpcs).pipe(
|
|
334
|
+
Effect.provide(outerWorkerContext),
|
|
323
335
|
UnknownError.mapToUnknownError,
|
|
324
|
-
Effect.
|
|
336
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
325
337
|
Effect.withSpan('@livestore/adapter-web:client-session:setupDedicatedWorker'),
|
|
326
338
|
Effect.tapCauseLogPretty,
|
|
327
|
-
Effect.forkScoped,
|
|
328
339
|
)
|
|
329
340
|
|
|
330
341
|
yield* workerDisconnectChannel.send(DedicatedWorkerDisconnectBroadcast.make({}))
|
|
331
342
|
|
|
332
|
-
const sharedWorker = yield* Fiber.join(sharedWorkerFiber)
|
|
333
343
|
yield* sharedWorker
|
|
334
|
-
.
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
)
|
|
344
|
+
.UpdateMessagePort({
|
|
345
|
+
port: mc.port2,
|
|
346
|
+
liveStoreVersion,
|
|
347
|
+
initial: {
|
|
348
|
+
storageOptions,
|
|
349
|
+
storeId,
|
|
350
|
+
clientId,
|
|
351
|
+
devtoolsEnabled,
|
|
352
|
+
debugInstanceId,
|
|
353
|
+
syncPayloadEncoded,
|
|
354
|
+
params,
|
|
355
|
+
},
|
|
356
|
+
})
|
|
348
357
|
.pipe(
|
|
349
358
|
UnknownError.mapToUnknownError,
|
|
350
|
-
Effect.
|
|
359
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
351
360
|
)
|
|
352
361
|
|
|
353
362
|
yield* Deferred.succeed(waitForSharedWorkerInitialized, undefined)
|
|
@@ -375,114 +384,100 @@ export const makePersistedAdapter =
|
|
|
375
384
|
yield* runLocked.pipe(Effect.interruptible, Effect.tapCauseLogPretty, Effect.forkScoped)
|
|
376
385
|
}
|
|
377
386
|
|
|
378
|
-
const runInWorker = <A,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
387
|
+
const runInWorker = <A, E, R>(
|
|
388
|
+
tag: string,
|
|
389
|
+
effect: Effect.Effect<A, E, R>,
|
|
390
|
+
): Effect.Effect<A, WithoutRpcClientError<E>, R> =>
|
|
391
|
+
Deferred.await(waitForSharedWorkerInitialized).pipe(
|
|
382
392
|
// NOTE we need to wait for the shared worker to be initialized before we can send requests to it
|
|
383
|
-
Effect.
|
|
384
|
-
|
|
385
|
-
Effect.catchIf(isWorkerTransportError, (e) => Effect.die(e)),
|
|
393
|
+
Effect.andThen(effect),
|
|
394
|
+
dieOnRpcClientError,
|
|
386
395
|
// NOTE we want to treat worker requests as atomic and therefore not allow them to be interrupted
|
|
387
396
|
// Interruption usually only happens during leader re-election or store shutdown
|
|
388
397
|
// Effect.uninterruptible,
|
|
389
398
|
Effect.logWarnIfTakesLongerThan({
|
|
390
|
-
label: `@livestore/adapter-web:client-session:runInWorker:${
|
|
399
|
+
label: `@livestore/adapter-web:client-session:runInWorker:${tag}`,
|
|
391
400
|
duration: 2000,
|
|
392
401
|
}),
|
|
393
|
-
Effect.withSpan(`@livestore/adapter-web:client-session:runInWorker:${
|
|
402
|
+
Effect.withSpan(`@livestore/adapter-web:client-session:runInWorker:${tag}`),
|
|
394
403
|
)
|
|
395
404
|
|
|
396
|
-
const runInWorkerStream = <A,
|
|
397
|
-
|
|
398
|
-
|
|
405
|
+
const runInWorkerStream = <A, E, R>(
|
|
406
|
+
tag: string,
|
|
407
|
+
stream: Stream.Stream<A, E, R>,
|
|
408
|
+
): Stream.Stream<A, WithoutRpcClientError<E>, R> =>
|
|
399
409
|
Effect.gen(function* () {
|
|
400
|
-
|
|
401
|
-
return
|
|
402
|
-
|
|
403
|
-
Stream.withSpan(`@livestore/adapter-web:client-session:runInWorkerStream:${
|
|
410
|
+
yield* Deferred.await(waitForSharedWorkerInitialized)
|
|
411
|
+
return stream.pipe(
|
|
412
|
+
dieOnRpcClientErrorStream,
|
|
413
|
+
Stream.withSpan(`@livestore/adapter-web:client-session:runInWorkerStream:${tag}`),
|
|
404
414
|
)
|
|
405
415
|
}).pipe(Stream.unwrap)
|
|
406
416
|
|
|
407
|
-
const bootStatusFiber = yield* runInWorkerStream(
|
|
417
|
+
const bootStatusFiber = yield* runInWorkerStream('BootStatusStream', sharedWorker.BootStatusStream({})).pipe(
|
|
408
418
|
Stream.tap((_) => Queue.offer(bootStatusQueue, _)),
|
|
409
419
|
Stream.runDrain,
|
|
410
|
-
Effect.
|
|
411
|
-
Cause.
|
|
420
|
+
Effect.tapCause((cause) =>
|
|
421
|
+
Cause.hasInterruptsOnly(cause) === true ? Effect.void : shutdown(Exit.failCause(cause)),
|
|
412
422
|
),
|
|
413
423
|
Effect.interruptible,
|
|
414
424
|
Effect.tapCauseLogPretty,
|
|
415
425
|
Effect.forkScoped,
|
|
416
426
|
)
|
|
417
427
|
|
|
418
|
-
yield* Queue.
|
|
428
|
+
yield* Queue.await(bootStatusQueue).pipe(
|
|
419
429
|
Effect.andThen(Fiber.interrupt(bootStatusFiber)),
|
|
430
|
+
Effect.interruptible,
|
|
420
431
|
Effect.tapCauseLogPretty,
|
|
421
432
|
Effect.forkScoped,
|
|
422
433
|
)
|
|
423
434
|
|
|
424
435
|
// TODO maybe bring back transfering the initially created in-memory db snapshot instead of
|
|
425
436
|
// re-exporting the db
|
|
426
|
-
const
|
|
437
|
+
const { sqliteDb, snapshotByteLength, migrationsReport } =
|
|
427
438
|
dataFromFile === undefined
|
|
428
|
-
? yield*
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
}))
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
441
|
-
const sqliteDb = yield* makeSqliteDb({ _tag: 'in-memory' })
|
|
442
|
-
|
|
443
|
-
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: [] } }
|
|
444
451
|
|
|
445
452
|
const numberOfTables =
|
|
446
453
|
sqliteDb.select<{ count: number }>(`select count(*) as count from sqlite_master`)[0]?.count ?? 0
|
|
447
454
|
if (numberOfTables === 0) {
|
|
448
455
|
return yield* UnknownError.make({
|
|
449
456
|
cause: `Encountered empty or corrupted database`,
|
|
450
|
-
payload: { snapshotByteLength
|
|
457
|
+
payload: { snapshotByteLength, storageOptions: options.storage },
|
|
451
458
|
})
|
|
452
459
|
}
|
|
453
460
|
|
|
454
|
-
//
|
|
455
|
-
//
|
|
456
|
-
const
|
|
457
|
-
sessionChangesetMetaTable
|
|
458
|
-
.select('seqNumClient', 'seqNumGlobal', 'seqNumRebaseGeneration')
|
|
459
|
-
.orderBy([
|
|
460
|
-
{ col: 'seqNumGlobal', direction: 'desc' },
|
|
461
|
-
{ col: 'seqNumClient', direction: 'desc' },
|
|
462
|
-
])
|
|
463
|
-
.first(),
|
|
464
|
-
)
|
|
465
|
-
|
|
466
|
-
const initialLeaderHead =
|
|
467
|
-
initialLeaderHeadRes !== undefined
|
|
468
|
-
? EventSequenceNumber.Client.Composite.make({
|
|
469
|
-
global: initialLeaderHeadRes.seqNumGlobal,
|
|
470
|
-
client: initialLeaderHeadRes.seqNumClient,
|
|
471
|
-
rebaseGeneration: initialLeaderHeadRes.seqNumRebaseGeneration,
|
|
472
|
-
})
|
|
473
|
-
: EventSequenceNumber.Client.ROOT
|
|
461
|
+
// The state snapshot carries its own cursor, so the fast path does not
|
|
462
|
+
// need to export or transfer the eventlog database.
|
|
463
|
+
const initialLeaderHead = yield* StateHead.make({ dbState: sqliteDb }).get
|
|
474
464
|
|
|
475
465
|
// console.debug('[@livestore/adapter-web:client-session] initialLeaderHead', initialLeaderHead)
|
|
476
466
|
|
|
477
|
-
yield* Effect.addFinalizer((ex) =>
|
|
467
|
+
yield* Effect.addFinalizer((ex: Exit.Exit<unknown, unknown>) =>
|
|
478
468
|
Effect.gen(function* () {
|
|
479
|
-
if (
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
469
|
+
if (Exit.isFailure(ex) === true) {
|
|
470
|
+
const cause = ex.cause
|
|
471
|
+
const shouldLogAsError =
|
|
472
|
+
Exit.hasInterrupts(ex) === false &&
|
|
473
|
+
Schema.is(IntentionalShutdownCause)(Cause.squash(cause)) === false &&
|
|
474
|
+
Schema.is(StoreInterrupted)(Cause.squash(cause)) === false
|
|
475
|
+
|
|
476
|
+
if (shouldLogAsError === true) {
|
|
477
|
+
yield* Effect.logError('[@livestore/adapter-web:client-session] client-session shutdown', cause)
|
|
478
|
+
} else {
|
|
479
|
+
yield* Effect.logDebug('[@livestore/adapter-web:client-session] client-session shutdown', gotLocky, ex)
|
|
480
|
+
}
|
|
486
481
|
} else {
|
|
487
482
|
yield* Effect.logDebug('[@livestore/adapter-web:client-session] client-session shutdown', gotLocky, ex)
|
|
488
483
|
}
|
|
@@ -494,22 +489,21 @@ export const makePersistedAdapter =
|
|
|
494
489
|
)
|
|
495
490
|
|
|
496
491
|
const leaderThread: ClientSession['leaderThread'] = {
|
|
497
|
-
export: runInWorker(
|
|
492
|
+
export: runInWorker('Export', sharedWorker.Export({})).pipe(
|
|
498
493
|
Effect.timeoutOrDie(10_000),
|
|
499
494
|
Effect.withSpan('@livestore/adapter-web:client-session:export'),
|
|
500
495
|
),
|
|
501
496
|
|
|
502
497
|
events: {
|
|
503
|
-
pull: ({ cursor }) =>
|
|
504
|
-
runInWorkerStream(new WorkerSchema.LeaderWorkerInnerPullStream({ cursor })).pipe(Stream.orDie),
|
|
498
|
+
pull: ({ cursor }) => runInWorkerStream('PullStream', sharedWorker.PullStream({ cursor })).pipe(Stream.orDie),
|
|
505
499
|
push: (batch) =>
|
|
506
|
-
runInWorker(
|
|
500
|
+
runInWorker('PushToLeader', sharedWorker.PushToLeader({ batch })).pipe(
|
|
507
501
|
Effect.withSpan('@livestore/adapter-web:client-session:pushToLeader', {
|
|
508
502
|
attributes: { batchSize: batch.length },
|
|
509
503
|
}),
|
|
510
504
|
),
|
|
511
505
|
stream: (options) =>
|
|
512
|
-
runInWorkerStream(
|
|
506
|
+
runInWorkerStream('StreamEvents', sharedWorker.StreamEvents(options)).pipe(
|
|
513
507
|
Stream.withSpan('@livestore/adapter-web:client-session:streamEvents'),
|
|
514
508
|
Stream.orDie,
|
|
515
509
|
),
|
|
@@ -521,30 +515,28 @@ export const makePersistedAdapter =
|
|
|
521
515
|
storageMode: opfsWarning === undefined ? 'persisted' : 'in-memory',
|
|
522
516
|
},
|
|
523
517
|
|
|
524
|
-
getEventlogData: runInWorker(
|
|
518
|
+
getEventlogData: runInWorker('ExportEventlog', sharedWorker.ExportEventlog({})).pipe(
|
|
525
519
|
Effect.timeoutOrDie(10_000),
|
|
526
520
|
Effect.withSpan('@livestore/adapter-web:client-session:getEventlogData'),
|
|
527
521
|
),
|
|
528
522
|
|
|
529
523
|
syncState: Subscribable.make({
|
|
530
|
-
get: runInWorker(
|
|
524
|
+
get: runInWorker('GetLeaderSyncState', sharedWorker.GetLeaderSyncState({})).pipe(
|
|
531
525
|
Effect.withSpan('@livestore/adapter-web:client-session:getLeaderSyncState'),
|
|
532
526
|
),
|
|
533
|
-
changes: runInWorkerStream(
|
|
527
|
+
changes: runInWorkerStream('SyncStateStream', sharedWorker.SyncStateStream({})).pipe(Stream.orDie),
|
|
534
528
|
}),
|
|
535
529
|
|
|
536
530
|
sendDevtoolsMessage: (message) =>
|
|
537
|
-
runInWorker(
|
|
531
|
+
runInWorker('ExtraDevtoolsMessage', sharedWorker.ExtraDevtoolsMessage({ message })).pipe(
|
|
538
532
|
Effect.withSpan('@livestore/adapter-web:client-session:devtoolsMessageForLeader'),
|
|
539
533
|
),
|
|
540
534
|
networkStatus: Subscribable.make({
|
|
541
|
-
get: runInWorker(
|
|
542
|
-
changes: runInWorkerStream(
|
|
535
|
+
get: runInWorker('GetNetworkStatus', sharedWorker.GetNetworkStatus({})),
|
|
536
|
+
changes: runInWorkerStream('NetworkStatusStream', sharedWorker.NetworkStatusStream({})),
|
|
543
537
|
}),
|
|
544
538
|
}
|
|
545
539
|
|
|
546
|
-
const sharedWorker = yield* Fiber.join(sharedWorkerFiber)
|
|
547
|
-
|
|
548
540
|
const clientSession = yield* makeClientSession({
|
|
549
541
|
...adapterArgs,
|
|
550
542
|
sqliteDb,
|
|
@@ -557,7 +549,13 @@ export const makePersistedAdapter =
|
|
|
557
549
|
// Can be undefined in Node.js
|
|
558
550
|
origin: globalThis.location?.origin,
|
|
559
551
|
connectWebmeshNode: ({ sessionInfo, webmeshNode }) =>
|
|
560
|
-
connectWebmeshNodeClientSession({
|
|
552
|
+
connectWebmeshNodeClientSession({
|
|
553
|
+
webmeshNode,
|
|
554
|
+
sessionInfo,
|
|
555
|
+
sharedWorker: makeWebmeshWorkerProxy(sharedWorker),
|
|
556
|
+
devtoolsEnabled,
|
|
557
|
+
schema,
|
|
558
|
+
}),
|
|
561
559
|
registerBeforeUnload: (onBeforeUnload) => {
|
|
562
560
|
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
|
563
561
|
window.addEventListener('beforeunload', onBeforeUnload)
|
|
@@ -569,7 +567,7 @@ export const makePersistedAdapter =
|
|
|
569
567
|
})
|
|
570
568
|
|
|
571
569
|
return clientSession
|
|
572
|
-
}).pipe(Effect.provide(Opfs.
|
|
570
|
+
}).pipe(Effect.provide(Opfs.layer), UnknownError.mapToUnknownError)
|
|
573
571
|
|
|
574
572
|
// NOTE for `local` storage we could also use the eventlog db to store the data
|
|
575
573
|
const getPersistedId = (key: string, storageType: 'session' | 'local') => {
|
|
@@ -633,7 +631,7 @@ const checkOpfsAvailability = Effect.gen(function* () {
|
|
|
633
631
|
const opfs = yield* Opfs.Opfs
|
|
634
632
|
return yield* opfs.getRootDirectoryHandle.pipe(
|
|
635
633
|
Effect.as(undefined),
|
|
636
|
-
Effect.
|
|
634
|
+
Effect.catch((error) => {
|
|
637
635
|
const reason: BootWarningReason =
|
|
638
636
|
Schema.is(WebError.SecurityError)(error) === true || Schema.is(WebError.NotAllowedError)(error) === true
|
|
639
637
|
? 'private-browsing'
|
|
@@ -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
|
+
)
|