@livestore/adapter-web 0.4.0 → 0.5.0-dev.0
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/in-memory/in-memory-adapter.d.ts +6 -8
- package/dist/in-memory/in-memory-adapter.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.js +30 -26
- package/dist/in-memory/in-memory-adapter.js.map +1 -1
- package/dist/single-tab/single-tab-adapter.d.ts.map +1 -1
- package/dist/single-tab/single-tab-adapter.js +47 -66
- 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 +57 -64
- package/dist/web-worker/client-session/persisted-adapter.js.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.d.ts +8 -10
- package/dist/web-worker/common/persisted-sqlite.d.ts.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.js +15 -24
- package/dist/web-worker/common/persisted-sqlite.js.map +1 -1
- 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/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 +264 -209
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.js +68 -51
- 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 +142 -91
- 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 +50 -63
- 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 +14 -24
- package/src/in-memory/in-memory-adapter.ts +66 -58
- package/src/single-tab/single-tab-adapter.ts +95 -95
- 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 +105 -105
- package/src/web-worker/common/persisted-sqlite.ts +41 -48
- 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 +128 -161
- package/src/web-worker/leader-worker/make-leader-worker.ts +344 -219
- package/src/web-worker/shared-worker/make-shared-worker.ts +93 -84
- package/src/web-worker/vite-dev-polyfill.ts +1 -2
|
@@ -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
|
*/
|
|
@@ -205,7 +207,7 @@ export const makePersistedAdapter =
|
|
|
205
207
|
const LIVESTORE_TAB_LOCK = `livestore-tab-lock-${storeId}`
|
|
206
208
|
const LIVESTORE_SHARED_WORKER_TERMINATION_LOCK = `livestore-shared-worker-termination-lock-${storeId}`
|
|
207
209
|
|
|
208
|
-
const storageOptions = yield* Schema.
|
|
210
|
+
const storageOptions = yield* Schema.decodeEffect(WorkerSchema.StorageType)(options.storage)
|
|
209
211
|
|
|
210
212
|
const shutdownChannel = yield* makeShutdownChannel(storeId)
|
|
211
213
|
|
|
@@ -253,7 +255,7 @@ export const makePersistedAdapter =
|
|
|
253
255
|
const workerDisconnectChannel = yield* makeWorkerDisconnectChannel(storeId)
|
|
254
256
|
|
|
255
257
|
yield* shutdownChannel.listen.pipe(
|
|
256
|
-
Stream.
|
|
258
|
+
Stream.mapEffect(Effect.fromResult),
|
|
257
259
|
Stream.tap((cause) =>
|
|
258
260
|
shutdown(cause._tag === 'IntentionalShutdownCause' ? Exit.succeed(cause) : Exit.fail(cause)),
|
|
259
261
|
),
|
|
@@ -271,17 +273,17 @@ export const makePersistedAdapter =
|
|
|
271
273
|
yield* Effect.addFinalizer(() => WebLock.waitForLock(LIVESTORE_SHARED_WORKER_TERMINATION_LOCK))
|
|
272
274
|
}
|
|
273
275
|
|
|
274
|
-
const sharedWorkerContext = yield* Layer.build(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
276
|
+
const sharedWorkerContext = yield* Layer.build(
|
|
277
|
+
RpcClient.layerProtocolWorker({ size: 1, concurrency: 100 }).pipe(
|
|
278
|
+
Layer.provide(BrowserWorker.layer(() => sharedWebWorker)),
|
|
279
|
+
),
|
|
280
|
+
)
|
|
281
|
+
const sharedWorker = yield* RpcClient.make(WorkerSchema.SharedWorkerRpcs).pipe(
|
|
279
282
|
Effect.provide(sharedWorkerContext),
|
|
280
283
|
Effect.tapCauseLogPretty,
|
|
281
|
-
|
|
282
|
-
Effect.
|
|
284
|
+
UnknownError.mapToUnknownError,
|
|
285
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
283
286
|
Effect.withSpan('@livestore/adapter-web:client-session:setupSharedWorker'),
|
|
284
|
-
Effect.forkScoped,
|
|
285
287
|
)
|
|
286
288
|
|
|
287
289
|
const lockDeferred = yield* Deferred.make<void>()
|
|
@@ -316,38 +318,43 @@ export const makePersistedAdapter =
|
|
|
316
318
|
// and adding the `sessionId` to make it easier to debug which session a worker belongs to in logs
|
|
317
319
|
const worker = tryAsFunctionAndNew(options.worker, { name: `livestore-worker-${storeId}-${sessionId}` })
|
|
318
320
|
|
|
319
|
-
yield*
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
321
|
+
const outerWorkerContext = yield* Layer.build(
|
|
322
|
+
RpcClient.layerProtocolWorker({ size: 1 }).pipe(
|
|
323
|
+
Layer.provide(BrowserWorker.layer(() => worker)),
|
|
324
|
+
Layer.provide(
|
|
325
|
+
RpcWorker.layerInitialMessage(
|
|
326
|
+
WorkerSchema.LeaderWorkerOuterInitialMessage.payloadSchema,
|
|
327
|
+
Effect.succeed({ port: mc.port1, storeId, clientId }),
|
|
328
|
+
),
|
|
329
|
+
),
|
|
330
|
+
),
|
|
331
|
+
)
|
|
332
|
+
yield* RpcClient.make(WorkerSchema.LeaderWorkerOuterRpcs).pipe(
|
|
333
|
+
Effect.provide(outerWorkerContext),
|
|
323
334
|
UnknownError.mapToUnknownError,
|
|
324
|
-
Effect.
|
|
335
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
325
336
|
Effect.withSpan('@livestore/adapter-web:client-session:setupDedicatedWorker'),
|
|
326
337
|
Effect.tapCauseLogPretty,
|
|
327
|
-
Effect.forkScoped,
|
|
328
338
|
)
|
|
329
339
|
|
|
330
340
|
yield* workerDisconnectChannel.send(DedicatedWorkerDisconnectBroadcast.make({}))
|
|
331
341
|
|
|
332
|
-
const sharedWorker = yield* Fiber.join(sharedWorkerFiber)
|
|
333
342
|
yield* sharedWorker
|
|
334
|
-
.
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}),
|
|
347
|
-
)
|
|
343
|
+
.UpdateMessagePort({
|
|
344
|
+
port: mc.port2,
|
|
345
|
+
liveStoreVersion,
|
|
346
|
+
initial: {
|
|
347
|
+
storageOptions,
|
|
348
|
+
storeId,
|
|
349
|
+
clientId,
|
|
350
|
+
devtoolsEnabled,
|
|
351
|
+
debugInstanceId,
|
|
352
|
+
syncPayloadEncoded,
|
|
353
|
+
},
|
|
354
|
+
})
|
|
348
355
|
.pipe(
|
|
349
356
|
UnknownError.mapToUnknownError,
|
|
350
|
-
Effect.
|
|
357
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
351
358
|
)
|
|
352
359
|
|
|
353
360
|
yield* Deferred.succeed(waitForSharedWorkerInitialized, undefined)
|
|
@@ -375,48 +382,50 @@ export const makePersistedAdapter =
|
|
|
375
382
|
yield* runLocked.pipe(Effect.interruptible, Effect.tapCauseLogPretty, Effect.forkScoped)
|
|
376
383
|
}
|
|
377
384
|
|
|
378
|
-
const runInWorker = <A,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
385
|
+
const runInWorker = <A, E, R>(
|
|
386
|
+
tag: string,
|
|
387
|
+
effect: Effect.Effect<A, E, R>,
|
|
388
|
+
): Effect.Effect<A, WithoutRpcClientError<E>, R> =>
|
|
389
|
+
Deferred.await(waitForSharedWorkerInitialized).pipe(
|
|
382
390
|
// 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)),
|
|
391
|
+
Effect.andThen(effect),
|
|
392
|
+
dieOnRpcClientError,
|
|
386
393
|
// NOTE we want to treat worker requests as atomic and therefore not allow them to be interrupted
|
|
387
394
|
// Interruption usually only happens during leader re-election or store shutdown
|
|
388
395
|
// Effect.uninterruptible,
|
|
389
396
|
Effect.logWarnIfTakesLongerThan({
|
|
390
|
-
label: `@livestore/adapter-web:client-session:runInWorker:${
|
|
397
|
+
label: `@livestore/adapter-web:client-session:runInWorker:${tag}`,
|
|
391
398
|
duration: 2000,
|
|
392
399
|
}),
|
|
393
|
-
Effect.withSpan(`@livestore/adapter-web:client-session:runInWorker:${
|
|
400
|
+
Effect.withSpan(`@livestore/adapter-web:client-session:runInWorker:${tag}`),
|
|
394
401
|
)
|
|
395
402
|
|
|
396
|
-
const runInWorkerStream = <A,
|
|
397
|
-
|
|
398
|
-
|
|
403
|
+
const runInWorkerStream = <A, E, R>(
|
|
404
|
+
tag: string,
|
|
405
|
+
stream: Stream.Stream<A, E, R>,
|
|
406
|
+
): Stream.Stream<A, WithoutRpcClientError<E>, R> =>
|
|
399
407
|
Effect.gen(function* () {
|
|
400
|
-
|
|
401
|
-
return
|
|
402
|
-
|
|
403
|
-
Stream.withSpan(`@livestore/adapter-web:client-session:runInWorkerStream:${
|
|
408
|
+
yield* Deferred.await(waitForSharedWorkerInitialized)
|
|
409
|
+
return stream.pipe(
|
|
410
|
+
dieOnRpcClientErrorStream,
|
|
411
|
+
Stream.withSpan(`@livestore/adapter-web:client-session:runInWorkerStream:${tag}`),
|
|
404
412
|
)
|
|
405
413
|
}).pipe(Stream.unwrap)
|
|
406
414
|
|
|
407
|
-
const bootStatusFiber = yield* runInWorkerStream(
|
|
415
|
+
const bootStatusFiber = yield* runInWorkerStream('BootStatusStream', sharedWorker.BootStatusStream({})).pipe(
|
|
408
416
|
Stream.tap((_) => Queue.offer(bootStatusQueue, _)),
|
|
409
417
|
Stream.runDrain,
|
|
410
|
-
Effect.
|
|
411
|
-
Cause.
|
|
418
|
+
Effect.tapCause((cause) =>
|
|
419
|
+
Cause.hasInterruptsOnly(cause) === true ? Effect.void : shutdown(Exit.failCause(cause)),
|
|
412
420
|
),
|
|
413
421
|
Effect.interruptible,
|
|
414
422
|
Effect.tapCauseLogPretty,
|
|
415
423
|
Effect.forkScoped,
|
|
416
424
|
)
|
|
417
425
|
|
|
418
|
-
yield* Queue.
|
|
426
|
+
yield* Queue.await(bootStatusQueue).pipe(
|
|
419
427
|
Effect.andThen(Fiber.interrupt(bootStatusFiber)),
|
|
428
|
+
Effect.interruptible,
|
|
420
429
|
Effect.tapCauseLogPretty,
|
|
421
430
|
Effect.forkScoped,
|
|
422
431
|
)
|
|
@@ -425,7 +434,7 @@ export const makePersistedAdapter =
|
|
|
425
434
|
// re-exporting the db
|
|
426
435
|
const initialResult =
|
|
427
436
|
dataFromFile === undefined
|
|
428
|
-
? yield* runInWorker(
|
|
437
|
+
? yield* runInWorker('GetRecreateSnapshot', sharedWorker.GetRecreateSnapshot({})).pipe(
|
|
429
438
|
Effect.map(({ snapshot, migrationsReport }) => ({
|
|
430
439
|
_tag: 'from-leader-worker' as const,
|
|
431
440
|
snapshot,
|
|
@@ -451,38 +460,26 @@ export const makePersistedAdapter =
|
|
|
451
460
|
})
|
|
452
461
|
}
|
|
453
462
|
|
|
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
|
|
463
|
+
// The state snapshot carries its own cursor, so the fast path does not
|
|
464
|
+
// need to export or transfer the eventlog database.
|
|
465
|
+
const initialLeaderHead = yield* StateHead.make({ dbState: sqliteDb }).get
|
|
474
466
|
|
|
475
467
|
// console.debug('[@livestore/adapter-web:client-session] initialLeaderHead', initialLeaderHead)
|
|
476
468
|
|
|
477
|
-
yield* Effect.addFinalizer((ex) =>
|
|
469
|
+
yield* Effect.addFinalizer((ex: Exit.Exit<unknown, unknown>) =>
|
|
478
470
|
Effect.gen(function* () {
|
|
479
|
-
if (
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
471
|
+
if (Exit.isFailure(ex) === true) {
|
|
472
|
+
const cause = ex.cause
|
|
473
|
+
const shouldLogAsError =
|
|
474
|
+
Exit.hasInterrupts(ex) === false &&
|
|
475
|
+
Schema.is(IntentionalShutdownCause)(Cause.squash(cause)) === false &&
|
|
476
|
+
Schema.is(StoreInterrupted)(Cause.squash(cause)) === false
|
|
477
|
+
|
|
478
|
+
if (shouldLogAsError === true) {
|
|
479
|
+
yield* Effect.logError('[@livestore/adapter-web:client-session] client-session shutdown', cause)
|
|
480
|
+
} else {
|
|
481
|
+
yield* Effect.logDebug('[@livestore/adapter-web:client-session] client-session shutdown', gotLocky, ex)
|
|
482
|
+
}
|
|
486
483
|
} else {
|
|
487
484
|
yield* Effect.logDebug('[@livestore/adapter-web:client-session] client-session shutdown', gotLocky, ex)
|
|
488
485
|
}
|
|
@@ -494,22 +491,21 @@ export const makePersistedAdapter =
|
|
|
494
491
|
)
|
|
495
492
|
|
|
496
493
|
const leaderThread: ClientSession['leaderThread'] = {
|
|
497
|
-
export: runInWorker(
|
|
494
|
+
export: runInWorker('Export', sharedWorker.Export({})).pipe(
|
|
498
495
|
Effect.timeoutOrDie(10_000),
|
|
499
496
|
Effect.withSpan('@livestore/adapter-web:client-session:export'),
|
|
500
497
|
),
|
|
501
498
|
|
|
502
499
|
events: {
|
|
503
|
-
pull: ({ cursor }) =>
|
|
504
|
-
runInWorkerStream(new WorkerSchema.LeaderWorkerInnerPullStream({ cursor })).pipe(Stream.orDie),
|
|
500
|
+
pull: ({ cursor }) => runInWorkerStream('PullStream', sharedWorker.PullStream({ cursor })).pipe(Stream.orDie),
|
|
505
501
|
push: (batch) =>
|
|
506
|
-
runInWorker(
|
|
502
|
+
runInWorker('PushToLeader', sharedWorker.PushToLeader({ batch })).pipe(
|
|
507
503
|
Effect.withSpan('@livestore/adapter-web:client-session:pushToLeader', {
|
|
508
504
|
attributes: { batchSize: batch.length },
|
|
509
505
|
}),
|
|
510
506
|
),
|
|
511
507
|
stream: (options) =>
|
|
512
|
-
runInWorkerStream(
|
|
508
|
+
runInWorkerStream('StreamEvents', sharedWorker.StreamEvents(options)).pipe(
|
|
513
509
|
Stream.withSpan('@livestore/adapter-web:client-session:streamEvents'),
|
|
514
510
|
Stream.orDie,
|
|
515
511
|
),
|
|
@@ -521,30 +517,28 @@ export const makePersistedAdapter =
|
|
|
521
517
|
storageMode: opfsWarning === undefined ? 'persisted' : 'in-memory',
|
|
522
518
|
},
|
|
523
519
|
|
|
524
|
-
getEventlogData: runInWorker(
|
|
520
|
+
getEventlogData: runInWorker('ExportEventlog', sharedWorker.ExportEventlog({})).pipe(
|
|
525
521
|
Effect.timeoutOrDie(10_000),
|
|
526
522
|
Effect.withSpan('@livestore/adapter-web:client-session:getEventlogData'),
|
|
527
523
|
),
|
|
528
524
|
|
|
529
525
|
syncState: Subscribable.make({
|
|
530
|
-
get: runInWorker(
|
|
526
|
+
get: runInWorker('GetLeaderSyncState', sharedWorker.GetLeaderSyncState({})).pipe(
|
|
531
527
|
Effect.withSpan('@livestore/adapter-web:client-session:getLeaderSyncState'),
|
|
532
528
|
),
|
|
533
|
-
changes: runInWorkerStream(
|
|
529
|
+
changes: runInWorkerStream('SyncStateStream', sharedWorker.SyncStateStream({})).pipe(Stream.orDie),
|
|
534
530
|
}),
|
|
535
531
|
|
|
536
532
|
sendDevtoolsMessage: (message) =>
|
|
537
|
-
runInWorker(
|
|
533
|
+
runInWorker('ExtraDevtoolsMessage', sharedWorker.ExtraDevtoolsMessage({ message })).pipe(
|
|
538
534
|
Effect.withSpan('@livestore/adapter-web:client-session:devtoolsMessageForLeader'),
|
|
539
535
|
),
|
|
540
536
|
networkStatus: Subscribable.make({
|
|
541
|
-
get: runInWorker(
|
|
542
|
-
changes: runInWorkerStream(
|
|
537
|
+
get: runInWorker('GetNetworkStatus', sharedWorker.GetNetworkStatus({})),
|
|
538
|
+
changes: runInWorkerStream('NetworkStatusStream', sharedWorker.NetworkStatusStream({})),
|
|
543
539
|
}),
|
|
544
540
|
}
|
|
545
541
|
|
|
546
|
-
const sharedWorker = yield* Fiber.join(sharedWorkerFiber)
|
|
547
|
-
|
|
548
542
|
const clientSession = yield* makeClientSession({
|
|
549
543
|
...adapterArgs,
|
|
550
544
|
sqliteDb,
|
|
@@ -557,7 +551,13 @@ export const makePersistedAdapter =
|
|
|
557
551
|
// Can be undefined in Node.js
|
|
558
552
|
origin: globalThis.location?.origin,
|
|
559
553
|
connectWebmeshNode: ({ sessionInfo, webmeshNode }) =>
|
|
560
|
-
connectWebmeshNodeClientSession({
|
|
554
|
+
connectWebmeshNodeClientSession({
|
|
555
|
+
webmeshNode,
|
|
556
|
+
sessionInfo,
|
|
557
|
+
sharedWorker: makeWebmeshWorkerProxy(sharedWorker),
|
|
558
|
+
devtoolsEnabled,
|
|
559
|
+
schema,
|
|
560
|
+
}),
|
|
561
561
|
registerBeforeUnload: (onBeforeUnload) => {
|
|
562
562
|
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
|
563
563
|
window.addEventListener('beforeunload', onBeforeUnload)
|
|
@@ -569,7 +569,7 @@ export const makePersistedAdapter =
|
|
|
569
569
|
})
|
|
570
570
|
|
|
571
571
|
return clientSession
|
|
572
|
-
}).pipe(Effect.provide(Opfs.
|
|
572
|
+
}).pipe(Effect.provide(Opfs.layer), UnknownError.mapToUnknownError)
|
|
573
573
|
|
|
574
574
|
// NOTE for `local` storage we could also use the eventlog db to store the data
|
|
575
575
|
const getPersistedId = (key: string, storageType: 'session' | 'local') => {
|
|
@@ -633,7 +633,7 @@ const checkOpfsAvailability = Effect.gen(function* () {
|
|
|
633
633
|
const opfs = yield* Opfs.Opfs
|
|
634
634
|
return yield* opfs.getRootDirectoryHandle.pipe(
|
|
635
635
|
Effect.as(undefined),
|
|
636
|
-
Effect.
|
|
636
|
+
Effect.catch((error) => {
|
|
637
637
|
const reason: BootWarningReason =
|
|
638
638
|
Schema.is(WebError.SecurityError)(error) === true || Schema.is(WebError.NotAllowedError)(error) === true
|
|
639
639
|
? 'private-browsing'
|
|
@@ -1,60 +1,66 @@
|
|
|
1
1
|
import { liveStoreStorageFormatVersion } from '@livestore/common'
|
|
2
|
-
import type
|
|
2
|
+
import { getStateDbBaseName, type LiveStoreSchema } from '@livestore/common/schema'
|
|
3
3
|
import {
|
|
4
4
|
decodeAccessHandlePoolFilename,
|
|
5
5
|
HEADER_OFFSET_DATA,
|
|
6
6
|
type WebDatabaseMetadataOpfs,
|
|
7
7
|
} from '@livestore/sqlite-wasm/browser'
|
|
8
8
|
import { isDevEnv } from '@livestore/utils'
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
Effect,
|
|
11
|
+
Option,
|
|
12
|
+
Order,
|
|
13
|
+
pipe,
|
|
14
|
+
ReadonlyArray as EffectArray,
|
|
15
|
+
Schedule,
|
|
16
|
+
Schema,
|
|
17
|
+
Stream,
|
|
18
|
+
} from '@livestore/utils/effect'
|
|
10
19
|
import { Opfs, type WebError } from '@livestore/utils/effect/browser'
|
|
11
20
|
|
|
12
21
|
import type * as WorkerSchema from './worker-schema.ts'
|
|
13
22
|
|
|
14
|
-
export class PersistedSqliteError extends Schema.TaggedError<PersistedSqliteError>(
|
|
23
|
+
export class PersistedSqliteError extends Schema.TaggedError<PersistedSqliteError>(
|
|
24
|
+
'~@livestore/adapter-web/PersistedSqliteError',
|
|
25
|
+
)('PersistedSqliteError', {
|
|
15
26
|
message: Schema.String,
|
|
16
|
-
cause: Schema.optional(Schema.Defect),
|
|
27
|
+
cause: Schema.optional(Schema.Defect()),
|
|
17
28
|
}) {}
|
|
18
29
|
|
|
19
|
-
export const readPersistedStateDbFromClientSession
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
| WebError.TypeMismatchError
|
|
32
|
-
| WebError.SecurityError
|
|
33
|
-
| Opfs.OpfsError,
|
|
34
|
-
Opfs.Opfs
|
|
35
|
-
> = Effect.fn('@livestore/adapter-web:readPersistedStateDbFromClientSession')(
|
|
36
|
-
function* ({ storageOptions, storeId, schema }) {
|
|
30
|
+
export const readPersistedStateDbFromClientSession = Effect.fn(
|
|
31
|
+
'@livestore/adapter-web:readPersistedStateDbFromClientSession',
|
|
32
|
+
)(
|
|
33
|
+
function* ({
|
|
34
|
+
storageOptions,
|
|
35
|
+
storeId,
|
|
36
|
+
schema,
|
|
37
|
+
}: {
|
|
38
|
+
storageOptions: WorkerSchema.StorageType
|
|
39
|
+
storeId: string
|
|
40
|
+
schema: LiveStoreSchema
|
|
41
|
+
}) {
|
|
37
42
|
const accessHandlePoolDirString = yield* sanitizeOpfsDir(storageOptions.directory, storeId)
|
|
38
43
|
|
|
39
44
|
const accessHandlePoolDirHandle = yield* Opfs.getDirectoryHandleByPath(accessHandlePoolDirString)
|
|
40
45
|
|
|
41
46
|
const stateDbFileName = `/${getStateDbFileName(schema)}`
|
|
42
47
|
|
|
43
|
-
const
|
|
48
|
+
const opfs = yield* Opfs.Opfs
|
|
49
|
+
const handlesStream = opfs.values(accessHandlePoolDirHandle)
|
|
44
50
|
|
|
45
51
|
const stateDbFileOption = yield* handlesStream.pipe(
|
|
46
52
|
Stream.filter((handle): handle is FileSystemFileHandle => handle.kind === 'file'),
|
|
47
53
|
Stream.mapEffect(
|
|
48
54
|
(fileHandle) =>
|
|
49
55
|
Effect.gen(function* () {
|
|
50
|
-
const file = yield*
|
|
56
|
+
const file = yield* opfs.getFile(fileHandle)
|
|
51
57
|
const fileName = yield* Effect.promise(() => decodeAccessHandlePoolFilename(file))
|
|
52
58
|
return { file, fileName }
|
|
53
59
|
}),
|
|
54
60
|
{ concurrency: 'unbounded' },
|
|
55
61
|
),
|
|
56
|
-
Stream.
|
|
57
|
-
Stream.
|
|
62
|
+
Stream.filter(({ fileName }) => fileName === stateDbFileName),
|
|
63
|
+
Stream.runFirst,
|
|
58
64
|
)
|
|
59
65
|
|
|
60
66
|
if (Option.isNone(stateDbFileOption) === true) {
|
|
@@ -116,11 +122,7 @@ export const sanitizeOpfsDir = Effect.fn('@livestore/adapter-web:sanitizeOpfsDir
|
|
|
116
122
|
return `${directory}@${liveStoreStorageFormatVersion}`
|
|
117
123
|
})
|
|
118
124
|
|
|
119
|
-
export const getStateDbFileName = (schema: LiveStoreSchema) => {
|
|
120
|
-
const schemaHashSuffix =
|
|
121
|
-
schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString()
|
|
122
|
-
return `state${schemaHashSuffix}.db`
|
|
123
|
-
}
|
|
125
|
+
export const getStateDbFileName = (schema: LiveStoreSchema) => `${getStateDbBaseName(schema)}.db`
|
|
124
126
|
|
|
125
127
|
export const MAX_ARCHIVED_STATE_DBS_IN_DEV = 3
|
|
126
128
|
export const ARCHIVE_DIR_NAME = 'archive'
|
|
@@ -159,16 +161,6 @@ export const cleanupOldStateDbFiles: (options: {
|
|
|
159
161
|
| PersistedSqliteError,
|
|
160
162
|
Opfs.Opfs
|
|
161
163
|
> = Effect.fn('@livestore/adapter-web:cleanupOldStateDbFiles')(function* ({ vfs, currentSchema, opfsDirectory }) {
|
|
162
|
-
// Only cleanup for auto migration strategy because:
|
|
163
|
-
// - Auto strategy: Creates new database files per schema change (e.g., state123.db, state456.db)
|
|
164
|
-
// which accumulate over time and can exhaust OPFS file pool capacity
|
|
165
|
-
// - Manual strategy: Always reuses the same database file (statefixed.db) across schema changes,
|
|
166
|
-
// so there are never multiple old files to clean up
|
|
167
|
-
if (currentSchema.state.sqlite.migrations.strategy === 'manual') {
|
|
168
|
-
yield* Effect.logDebug('Skipping state db cleanup - manual migration strategy uses fixed filename')
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
164
|
const isDev = isDevEnv()
|
|
173
165
|
const currentDbFileName = getStateDbFileName(currentSchema)
|
|
174
166
|
const currentPath = `/${currentDbFileName}`
|
|
@@ -243,22 +235,23 @@ const pruneArchiveDirectory = Effect.fn('@livestore/adapter-web:pruneArchiveDire
|
|
|
243
235
|
keep: number
|
|
244
236
|
}) {
|
|
245
237
|
const archiveDirHandle = yield* Opfs.getDirectoryHandleByPath(archiveDirectory)
|
|
246
|
-
const
|
|
238
|
+
const opfs = yield* Opfs.Opfs
|
|
239
|
+
const handlesStream = opfs.values(archiveDirHandle)
|
|
247
240
|
const filesWithMetadata = yield* handlesStream.pipe(
|
|
248
241
|
Stream.filter((handle): handle is FileSystemFileHandle => handle.kind === 'file'),
|
|
249
242
|
Stream.mapEffect((fileHandle) => Opfs.getMetadata(fileHandle)),
|
|
250
243
|
Stream.runCollect,
|
|
251
244
|
)
|
|
252
|
-
const filesToDelete =
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
245
|
+
const filesToDelete = pipe(
|
|
246
|
+
filesWithMetadata,
|
|
247
|
+
// oxlint-disable-next-line eslint-plugin-unicorn(no-array-sort) -- EffectArray has no toSorted helper in the current Effect version; sort returns a sorted copy.
|
|
248
|
+
EffectArray.sort(Order.mapInput(Order.Number, (entry: { lastModified: number }) => entry.lastModified)),
|
|
249
|
+
EffectArray.drop(keep),
|
|
257
250
|
)
|
|
258
251
|
|
|
259
252
|
if (filesToDelete.length === 0) return
|
|
260
253
|
|
|
261
|
-
yield* Effect.forEach(filesToDelete, ({ name }) =>
|
|
254
|
+
yield* Effect.forEach(filesToDelete, ({ name }) => opfs.removeEntry(archiveDirHandle, name))
|
|
262
255
|
|
|
263
256
|
yield* Effect.logDebug(`Pruned ${filesToDelete.length} old database file(s) from archive directory`)
|
|
264
257
|
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Effect, RpcClientError, Schema, Stream } from '@livestore/utils/effect'
|
|
2
|
+
import type * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
3
|
+
|
|
4
|
+
export type WebmeshWorkerProxy = Parameters<typeof WebmeshWorker.connectViaWorker>[0]['worker']
|
|
5
|
+
export type WithoutRpcClientError<E> = E extends { readonly _tag: infer Tag }
|
|
6
|
+
? 'RpcClientError' extends Tag
|
|
7
|
+
? never
|
|
8
|
+
: E
|
|
9
|
+
: E
|
|
10
|
+
export type WebmeshWorkerRpcClient = {
|
|
11
|
+
readonly ['WebmeshWorker.CreateConnection']: (
|
|
12
|
+
request: typeof WebmeshWorker.Schema.CreateConnection.Type,
|
|
13
|
+
) => Stream.Stream<{}, RpcClientError.RpcClientError>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const isRpcClientError = (error: unknown): error is RpcClientError.RpcClientError =>
|
|
17
|
+
Schema.is(RpcClientError.RpcClientError)(error)
|
|
18
|
+
|
|
19
|
+
export const dieOnRpcClientError = <A, E, R>(
|
|
20
|
+
effect: Effect.Effect<A, E, R>,
|
|
21
|
+
): Effect.Effect<A, WithoutRpcClientError<E>, R> =>
|
|
22
|
+
(effect as Effect.Effect<A, RpcClientError.RpcClientError | WithoutRpcClientError<E>, R>).pipe(
|
|
23
|
+
Effect.catchIf(isRpcClientError, (error) => Effect.die(error)),
|
|
24
|
+
) as Effect.Effect<A, WithoutRpcClientError<E>, R>
|
|
25
|
+
|
|
26
|
+
export const dieOnRpcClientErrorStream = <A, E, R>(
|
|
27
|
+
stream: Stream.Stream<A, E, R>,
|
|
28
|
+
): Stream.Stream<A, WithoutRpcClientError<E>, R> =>
|
|
29
|
+
(stream as Stream.Stream<A, RpcClientError.RpcClientError | WithoutRpcClientError<E>, R>).pipe(
|
|
30
|
+
Stream.catchIf(
|
|
31
|
+
isRpcClientError,
|
|
32
|
+
(error) => Stream.die(error),
|
|
33
|
+
(error) => Stream.fail(error),
|
|
34
|
+
),
|
|
35
|
+
) as Stream.Stream<A, WithoutRpcClientError<E>, R>
|
|
36
|
+
|
|
37
|
+
export const makeWebmeshWorkerProxy = (client: WebmeshWorkerRpcClient): WebmeshWorkerProxy => ({
|
|
38
|
+
execute: (request) => dieOnRpcClientErrorStream(client['WebmeshWorker.CreateConnection'](request)),
|
|
39
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const makeSharedWorkerNodeName = ({ storeId }: { storeId: string }) => `shared-worker-${storeId}`
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Effect, Schema, type Scope, type WebChannel } from '@livestore/utils/effect'
|
|
2
2
|
import { WebChannelBrowser } from '@livestore/utils/effect/browser'
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export const DedicatedWorkerDisconnectBroadcast = Schema.TaggedStruct('DedicatedWorkerDisconnectBroadcast', {})
|
|
5
5
|
|
|
6
6
|
/** Used across workers for leader election purposes */
|
|
7
7
|
export const makeWorkerDisconnectChannel = (
|