@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
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type LockStatus,
|
|
6
6
|
makeClientSession,
|
|
7
7
|
migrateDb,
|
|
8
|
+
StateHead,
|
|
8
9
|
type SyncOptions,
|
|
9
10
|
UnknownError,
|
|
10
11
|
} from '@livestore/common'
|
|
@@ -18,20 +19,34 @@ import {
|
|
|
18
19
|
} from '@livestore/common/leader-thread'
|
|
19
20
|
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
20
21
|
import { LiveStoreEvent } from '@livestore/common/schema'
|
|
21
|
-
import * as DevtoolsWeb from '@livestore/devtools-web-common/web-channel'
|
|
22
|
-
import type * as WebmeshWorker from '@livestore/devtools-web-common/worker'
|
|
23
22
|
import type { MakeWebSqliteDb } from '@livestore/sqlite-wasm/browser'
|
|
24
23
|
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
25
24
|
import { tryAsFunctionAndNew } from '@livestore/utils'
|
|
26
|
-
import
|
|
27
|
-
|
|
25
|
+
import {
|
|
26
|
+
type Scope,
|
|
27
|
+
Effect,
|
|
28
|
+
FetchHttpClient,
|
|
29
|
+
Layer,
|
|
30
|
+
Queue,
|
|
31
|
+
RpcClient,
|
|
32
|
+
type Schema,
|
|
33
|
+
SubscriptionRef,
|
|
34
|
+
} from '@livestore/utils/effect'
|
|
28
35
|
import { BrowserWorker } from '@livestore/utils/effect/browser'
|
|
29
36
|
import { nanoid } from '@livestore/utils/nanoid'
|
|
30
37
|
import * as Webmesh from '@livestore/webmesh'
|
|
38
|
+
import * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
31
39
|
|
|
32
40
|
import { connectWebmeshNodeClientSession } from '../web-worker/client-session/client-session-devtools.ts'
|
|
33
41
|
import { loadSqlite3 } from '../web-worker/client-session/sqlite-loader.ts'
|
|
42
|
+
import {
|
|
43
|
+
dieOnRpcClientError,
|
|
44
|
+
makeWebmeshWorkerProxy,
|
|
45
|
+
type WebmeshWorkerProxy,
|
|
46
|
+
} from '../web-worker/common/rpc-worker.ts'
|
|
34
47
|
import { makeShutdownChannel } from '../web-worker/common/shutdown-channel.ts'
|
|
48
|
+
import { makeSharedWorkerNodeName } from '../web-worker/common/webmesh-node-names.ts'
|
|
49
|
+
import * as WorkerSchema from '../web-worker/common/worker-schema.ts'
|
|
35
50
|
|
|
36
51
|
export interface InMemoryAdapterOptions {
|
|
37
52
|
importSnapshot?: Uint8Array<ArrayBuffer>
|
|
@@ -53,9 +68,7 @@ export interface InMemoryAdapterOptions {
|
|
|
53
68
|
devtools?: {
|
|
54
69
|
sharedWorker:
|
|
55
70
|
| ((options: { name: string }) => globalThis.SharedWorker)
|
|
56
|
-
| (new (options: {
|
|
57
|
-
name: string
|
|
58
|
-
}) => globalThis.SharedWorker)
|
|
71
|
+
| (new (options: { name: string }) => globalThis.SharedWorker)
|
|
59
72
|
}
|
|
60
73
|
}
|
|
61
74
|
|
|
@@ -115,23 +128,26 @@ export const makeInMemoryAdapter =
|
|
|
115
128
|
const clientId = options.clientId ?? nanoid(6)
|
|
116
129
|
const sessionId = options.sessionId ?? nanoid(6)
|
|
117
130
|
|
|
118
|
-
const sharedWebWorker =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
131
|
+
const sharedWebWorker =
|
|
132
|
+
options.devtools?.sharedWorker !== undefined
|
|
133
|
+
? tryAsFunctionAndNew(options.devtools.sharedWorker, {
|
|
134
|
+
name: `livestore-shared-worker-${storeId}`,
|
|
135
|
+
})
|
|
136
|
+
: undefined
|
|
137
|
+
|
|
138
|
+
const sharedWorkerClient =
|
|
139
|
+
sharedWebWorker !== undefined
|
|
140
|
+
? yield* RpcClient.make(WorkerSchema.SharedWorkerRpcs).pipe(
|
|
141
|
+
Effect.provide(
|
|
142
|
+
Layer.provideMerge(
|
|
143
|
+
RpcClient.layerProtocolWorker({ size: 1, concurrency: 100 }),
|
|
144
|
+
BrowserWorker.layer(() => sharedWebWorker),
|
|
145
|
+
),
|
|
146
|
+
),
|
|
147
|
+
Effect.tapCauseLogPretty,
|
|
148
|
+
UnknownError.mapToUnknownError,
|
|
149
|
+
)
|
|
150
|
+
: undefined
|
|
135
151
|
|
|
136
152
|
const { leaderThread, initialSnapshot } = yield* makeLeaderThread({
|
|
137
153
|
schema,
|
|
@@ -143,7 +159,7 @@ export const makeInMemoryAdapter =
|
|
|
143
159
|
syncPayloadSchema,
|
|
144
160
|
importSnapshot: options.importSnapshot,
|
|
145
161
|
devtoolsEnabled,
|
|
146
|
-
|
|
162
|
+
sharedWorker: sharedWorkerClient === undefined ? undefined : makeWebmeshWorkerProxy(sharedWorkerClient),
|
|
147
163
|
})
|
|
148
164
|
|
|
149
165
|
sqliteDb.import(initialSnapshot)
|
|
@@ -164,13 +180,17 @@ export const makeInMemoryAdapter =
|
|
|
164
180
|
origin: globalThis.location?.origin,
|
|
165
181
|
connectWebmeshNode: ({ sessionInfo, webmeshNode }) =>
|
|
166
182
|
Effect.gen(function* () {
|
|
167
|
-
if (
|
|
183
|
+
if (sharedWorkerClient === undefined || devtoolsEnabled === false) {
|
|
168
184
|
return
|
|
169
185
|
}
|
|
170
186
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
187
|
+
yield* connectWebmeshNodeClientSession({
|
|
188
|
+
webmeshNode,
|
|
189
|
+
sessionInfo,
|
|
190
|
+
sharedWorker: makeWebmeshWorkerProxy(sharedWorkerClient),
|
|
191
|
+
devtoolsEnabled,
|
|
192
|
+
schema,
|
|
193
|
+
})
|
|
174
194
|
}),
|
|
175
195
|
registerBeforeUnload: (onBeforeUnload) => {
|
|
176
196
|
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
|
@@ -191,11 +211,11 @@ export interface MakeLeaderThreadArgs {
|
|
|
191
211
|
clientId: string
|
|
192
212
|
makeSqliteDb: MakeWebSqliteDb
|
|
193
213
|
syncOptions: SyncOptions | undefined
|
|
194
|
-
syncPayloadEncoded: Schema.
|
|
195
|
-
syncPayloadSchema: Schema.
|
|
214
|
+
syncPayloadEncoded: Schema.Json | undefined
|
|
215
|
+
syncPayloadSchema: Schema.Top | undefined
|
|
196
216
|
importSnapshot: Uint8Array<ArrayBuffer> | undefined
|
|
197
217
|
devtoolsEnabled: boolean
|
|
198
|
-
|
|
218
|
+
sharedWorker: WebmeshWorkerProxy | undefined
|
|
199
219
|
}
|
|
200
220
|
|
|
201
221
|
const makeLeaderThread = ({
|
|
@@ -208,16 +228,15 @@ const makeLeaderThread = ({
|
|
|
208
228
|
syncPayloadSchema,
|
|
209
229
|
importSnapshot,
|
|
210
230
|
devtoolsEnabled,
|
|
211
|
-
|
|
231
|
+
sharedWorker,
|
|
212
232
|
}: MakeLeaderThreadArgs) =>
|
|
213
233
|
Effect.gen(function* () {
|
|
214
|
-
const
|
|
234
|
+
const services = yield* Effect.context()
|
|
215
235
|
|
|
216
236
|
const makeDb = (_kind: 'state' | 'eventlog') => {
|
|
217
237
|
return makeSqliteDb({
|
|
218
238
|
_tag: 'in-memory',
|
|
219
|
-
configureDb: (db) =>
|
|
220
|
-
configureConnection(db, { foreignKeys: true }).pipe(Effect.provide(runtime), Effect.runSync),
|
|
239
|
+
configureDb: (db) => configureConnection(db, { foreignKeys: true }).pipe(Effect.runSyncWith(services)),
|
|
221
240
|
})
|
|
222
241
|
}
|
|
223
242
|
|
|
@@ -234,7 +253,7 @@ const makeLeaderThread = ({
|
|
|
234
253
|
|
|
235
254
|
const devtoolsOptions = yield* makeDevtoolsOptions({
|
|
236
255
|
devtoolsEnabled,
|
|
237
|
-
|
|
256
|
+
sharedWorker,
|
|
238
257
|
dbState,
|
|
239
258
|
dbEventlog,
|
|
240
259
|
storeId,
|
|
@@ -253,8 +272,8 @@ const makeLeaderThread = ({
|
|
|
253
272
|
devtoolsOptions,
|
|
254
273
|
shutdownChannel,
|
|
255
274
|
syncPayloadEncoded,
|
|
256
|
-
syncPayloadSchema,
|
|
257
|
-
}),
|
|
275
|
+
syncPayloadSchema: syncPayloadSchema as Schema.Decoder<Schema.Json, never> | undefined,
|
|
276
|
+
}).pipe(Layer.provide(StateHead.layer({ dbState }))),
|
|
258
277
|
)
|
|
259
278
|
|
|
260
279
|
return yield* Effect.gen(function* () {
|
|
@@ -266,11 +285,7 @@ const makeLeaderThread = ({
|
|
|
266
285
|
const leaderThread = ClientSessionLeaderThreadProxy.of({
|
|
267
286
|
events: {
|
|
268
287
|
pull: ({ cursor }) => syncProcessor.pull({ cursor }),
|
|
269
|
-
push: (batch) =>
|
|
270
|
-
syncProcessor.push(
|
|
271
|
-
batch.map((item) => new LiveStoreEvent.Client.EncodedWithMeta(item)),
|
|
272
|
-
{ waitForProcessing: true },
|
|
273
|
-
),
|
|
288
|
+
push: (batch) => syncProcessor.push(batch.map((item) => new LiveStoreEvent.Client.EncodedWithMeta(item))),
|
|
274
289
|
stream: (options) =>
|
|
275
290
|
streamEventsWithSyncState({
|
|
276
291
|
dbEventlog,
|
|
@@ -286,7 +301,7 @@ const makeLeaderThread = ({
|
|
|
286
301
|
export: Effect.sync(() => dbState.export()),
|
|
287
302
|
getEventlogData: Effect.sync(() => dbEventlog.export()),
|
|
288
303
|
syncState: syncProcessor.syncState,
|
|
289
|
-
sendDevtoolsMessage: (message) =>
|
|
304
|
+
sendDevtoolsMessage: (message) => Queue.offer(extraIncomingMessagesQueue, message),
|
|
290
305
|
networkStatus,
|
|
291
306
|
})
|
|
292
307
|
|
|
@@ -296,28 +311,23 @@ const makeLeaderThread = ({
|
|
|
296
311
|
}).pipe(Effect.provide(layer))
|
|
297
312
|
})
|
|
298
313
|
|
|
299
|
-
type SharedWorkerFiber = Fiber.Fiber<
|
|
300
|
-
Worker.SerializedWorkerPool<typeof WebmeshWorker.Schema.Request.Type>,
|
|
301
|
-
UnknownError
|
|
302
|
-
>
|
|
303
|
-
|
|
304
314
|
const makeDevtoolsOptions = ({
|
|
305
315
|
devtoolsEnabled,
|
|
306
|
-
|
|
316
|
+
sharedWorker,
|
|
307
317
|
dbState,
|
|
308
318
|
dbEventlog,
|
|
309
319
|
storeId,
|
|
310
320
|
clientId,
|
|
311
321
|
}: {
|
|
312
322
|
devtoolsEnabled: boolean
|
|
313
|
-
|
|
323
|
+
sharedWorker: WebmeshWorkerProxy | undefined
|
|
314
324
|
dbState: LeaderSqliteDb
|
|
315
325
|
dbEventlog: LeaderSqliteDb
|
|
316
326
|
storeId: string
|
|
317
327
|
clientId: string
|
|
318
328
|
}): Effect.Effect<DevtoolsOptions, UnknownError, Scope.Scope> =>
|
|
319
329
|
Effect.gen(function* () {
|
|
320
|
-
if (devtoolsEnabled === false ||
|
|
330
|
+
if (devtoolsEnabled === false || sharedWorker === undefined) {
|
|
321
331
|
return { enabled: false }
|
|
322
332
|
}
|
|
323
333
|
|
|
@@ -333,17 +343,15 @@ const makeDevtoolsOptions = ({
|
|
|
333
343
|
// @ts-expect-error TODO type this
|
|
334
344
|
globalThis.__debugWebmeshNodeLeader = node
|
|
335
345
|
|
|
336
|
-
const sharedWorker = yield* sharedWorkerFiber.pipe(Fiber.join)
|
|
337
|
-
|
|
338
346
|
// TODO also make this work with the browser extension
|
|
339
347
|
// basic idea: instead of also connecting to the shared worker,
|
|
340
348
|
// connect to the client session node above which will already connect to the shared worker + browser extension
|
|
341
349
|
|
|
342
|
-
yield*
|
|
350
|
+
yield* WebmeshWorker.connectViaWorker({
|
|
343
351
|
node,
|
|
344
352
|
worker: sharedWorker,
|
|
345
|
-
target:
|
|
346
|
-
}).pipe(Effect.tapCauseLogPretty, Effect.forkScoped)
|
|
353
|
+
target: makeSharedWorkerNodeName({ storeId }),
|
|
354
|
+
}).pipe(Effect.tapCauseLogPretty, dieOnRpcClientError, Effect.forkScoped)
|
|
347
355
|
|
|
348
356
|
return { node, persistenceInfo, mode: 'direct' }
|
|
349
357
|
}),
|
|
@@ -16,13 +16,11 @@
|
|
|
16
16
|
import type { Adapter, BootWarningReason, ClientSession, LockStatus } from '@livestore/common'
|
|
17
17
|
import {
|
|
18
18
|
IntentionalShutdownCause,
|
|
19
|
-
isWorkerTransportError,
|
|
20
19
|
makeClientSession,
|
|
20
|
+
StateHead,
|
|
21
21
|
StoreInterrupted,
|
|
22
|
-
sessionChangesetMetaTable,
|
|
23
22
|
UnknownError,
|
|
24
23
|
} from '@livestore/common'
|
|
25
|
-
import { EventSequenceNumber } from '@livestore/common/schema'
|
|
26
24
|
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
27
25
|
import { shouldNeverHappen, tryAsFunctionAndNew } from '@livestore/utils'
|
|
28
26
|
import {
|
|
@@ -31,13 +29,13 @@ import {
|
|
|
31
29
|
Exit,
|
|
32
30
|
Fiber,
|
|
33
31
|
Layer,
|
|
34
|
-
Option,
|
|
35
32
|
Queue,
|
|
33
|
+
RpcClient,
|
|
34
|
+
RpcWorker,
|
|
36
35
|
Schema,
|
|
37
36
|
Stream,
|
|
38
37
|
Subscribable,
|
|
39
38
|
SubscriptionRef,
|
|
40
|
-
Worker,
|
|
41
39
|
} from '@livestore/utils/effect'
|
|
42
40
|
import { BrowserWorker, Opfs, WebError } from '@livestore/utils/effect/browser'
|
|
43
41
|
import { nanoid } from '@livestore/utils/nanoid'
|
|
@@ -47,6 +45,11 @@ import {
|
|
|
47
45
|
readPersistedStateDbFromClientSession,
|
|
48
46
|
resetPersistedDataFromClientSession,
|
|
49
47
|
} from '../web-worker/common/persisted-sqlite.ts'
|
|
48
|
+
import {
|
|
49
|
+
type WithoutRpcClientError,
|
|
50
|
+
dieOnRpcClientError,
|
|
51
|
+
dieOnRpcClientErrorStream,
|
|
52
|
+
} from '../web-worker/common/rpc-worker.ts'
|
|
50
53
|
import { makeShutdownChannel } from '../web-worker/common/shutdown-channel.ts'
|
|
51
54
|
import * as WorkerSchema from '../web-worker/common/worker-schema.ts'
|
|
52
55
|
|
|
@@ -155,7 +158,7 @@ export const makeSingleTabAdapter =
|
|
|
155
158
|
|
|
156
159
|
const sqlite3 = yield* Effect.promise(() => loadSqlite3())
|
|
157
160
|
|
|
158
|
-
const storageOptions = yield* Schema.
|
|
161
|
+
const storageOptions = yield* Schema.decodeEffect(WorkerSchema.StorageType)(options.storage)
|
|
159
162
|
|
|
160
163
|
const shutdownChannel = yield* makeShutdownChannel(storeId)
|
|
161
164
|
|
|
@@ -192,7 +195,7 @@ export const makeSingleTabAdapter =
|
|
|
192
195
|
const sessionId = options.sessionId ?? getPersistedId(`sessionId:${storeId}`, 'session')
|
|
193
196
|
|
|
194
197
|
yield* shutdownChannel.listen.pipe(
|
|
195
|
-
Stream.
|
|
198
|
+
Stream.mapEffect(Effect.fromResult),
|
|
196
199
|
Stream.tap((cause) =>
|
|
197
200
|
shutdown(cause._tag === 'IntentionalShutdownCause' ? Exit.succeed(cause) : Exit.fail(cause)),
|
|
198
201
|
),
|
|
@@ -212,81 +215,92 @@ export const makeSingleTabAdapter =
|
|
|
212
215
|
const worker = tryAsFunctionAndNew(options.worker, { name: `livestore-worker-${storeId}-${sessionId}` })
|
|
213
216
|
|
|
214
217
|
// Set up communication with the dedicated worker via the outer protocol
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
218
|
+
const outerWorkerContext = yield* Layer.build(
|
|
219
|
+
RpcClient.layerProtocolWorker({ size: 1 }).pipe(
|
|
220
|
+
Layer.provide(BrowserWorker.layer(() => worker)),
|
|
221
|
+
Layer.provide(
|
|
222
|
+
RpcWorker.layerInitialMessage(
|
|
223
|
+
WorkerSchema.LeaderWorkerOuterInitialMessage.payloadSchema,
|
|
224
|
+
Effect.succeed({ port: mc.port1, storeId, clientId }),
|
|
225
|
+
),
|
|
226
|
+
),
|
|
227
|
+
),
|
|
228
|
+
)
|
|
229
|
+
yield* RpcClient.make(WorkerSchema.LeaderWorkerOuterRpcs).pipe(
|
|
230
|
+
Effect.provide(outerWorkerContext),
|
|
219
231
|
UnknownError.mapToUnknownError,
|
|
220
|
-
Effect.
|
|
232
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
221
233
|
Effect.withSpan('@livestore/adapter-web:single-tab:setupDedicatedWorker'),
|
|
222
234
|
Effect.tapCauseLogPretty,
|
|
223
|
-
Effect.forkScoped,
|
|
224
235
|
)
|
|
225
236
|
|
|
226
237
|
// Set up the inner worker communication via port2 (like SharedWorker would do)
|
|
227
238
|
// BrowserWorker.layer accepts a MessagePort as well as a Worker
|
|
228
|
-
const innerWorkerContext = yield* Layer.build(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
239
|
+
const innerWorkerContext = yield* Layer.build(
|
|
240
|
+
RpcClient.layerProtocolWorker({ size: 1, concurrency: 100 }).pipe(
|
|
241
|
+
Layer.provide(BrowserWorker.layer(() => mc.port2)),
|
|
242
|
+
Layer.provide(
|
|
243
|
+
RpcWorker.layerInitialMessage(
|
|
244
|
+
WorkerSchema.LeaderWorkerInnerInitialMessage.payloadSchema,
|
|
245
|
+
Effect.succeed({
|
|
246
|
+
storageOptions,
|
|
247
|
+
storeId,
|
|
248
|
+
clientId,
|
|
249
|
+
// Devtools disabled in single-tab mode (requires SharedWorker)
|
|
250
|
+
devtoolsEnabled: false,
|
|
251
|
+
debugInstanceId: adapterArgs.debugInstanceId,
|
|
252
|
+
syncPayloadEncoded,
|
|
253
|
+
}),
|
|
254
|
+
),
|
|
255
|
+
),
|
|
256
|
+
),
|
|
257
|
+
)
|
|
258
|
+
const innerWorker = yield* RpcClient.make(WorkerSchema.LeaderWorkerInnerRpcs).pipe(
|
|
243
259
|
Effect.provide(innerWorkerContext),
|
|
244
260
|
Effect.tapCauseLogPretty,
|
|
245
|
-
|
|
246
|
-
Effect.
|
|
261
|
+
UnknownError.mapToUnknownError,
|
|
262
|
+
Effect.tapCause((cause) => shutdown(Exit.failCause(cause))),
|
|
247
263
|
Effect.withSpan('@livestore/adapter-web:single-tab:setupInnerWorker'),
|
|
248
|
-
Effect.forkScoped,
|
|
249
264
|
)
|
|
250
265
|
|
|
251
266
|
// Helper to run requests against the worker
|
|
252
|
-
const runInWorker = <A,
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
267
|
+
const runInWorker = <A, E, R>(
|
|
268
|
+
tag: string,
|
|
269
|
+
effect: Effect.Effect<A, E, R>,
|
|
270
|
+
): Effect.Effect<A, WithoutRpcClientError<E>, R> =>
|
|
271
|
+
effect.pipe(
|
|
272
|
+
dieOnRpcClientError,
|
|
258
273
|
Effect.logWarnIfTakesLongerThan({
|
|
259
|
-
label: `@livestore/adapter-web:single-tab:runInWorker:${
|
|
274
|
+
label: `@livestore/adapter-web:single-tab:runInWorker:${tag}`,
|
|
260
275
|
duration: 2000,
|
|
261
276
|
}),
|
|
262
|
-
Effect.withSpan(`@livestore/adapter-web:single-tab:runInWorker:${
|
|
277
|
+
Effect.withSpan(`@livestore/adapter-web:single-tab:runInWorker:${tag}`),
|
|
263
278
|
)
|
|
264
279
|
|
|
265
|
-
const runInWorkerStream = <A,
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
)
|
|
274
|
-
}).pipe(Stream.unwrap)
|
|
280
|
+
const runInWorkerStream = <A, E, R>(
|
|
281
|
+
tag: string,
|
|
282
|
+
stream: Stream.Stream<A, E, R>,
|
|
283
|
+
): Stream.Stream<A, WithoutRpcClientError<E>, R> =>
|
|
284
|
+
stream.pipe(
|
|
285
|
+
dieOnRpcClientErrorStream,
|
|
286
|
+
Stream.withSpan(`@livestore/adapter-web:single-tab:runInWorkerStream:${tag}`),
|
|
287
|
+
)
|
|
275
288
|
|
|
276
289
|
// Forward boot status from worker
|
|
277
|
-
const bootStatusFiber = yield* runInWorkerStream(
|
|
290
|
+
const bootStatusFiber = yield* runInWorkerStream('BootStatusStream', innerWorker.BootStatusStream({})).pipe(
|
|
278
291
|
Stream.tap((_) => Queue.offer(bootStatusQueue, _)),
|
|
279
292
|
Stream.runDrain,
|
|
280
|
-
Effect.
|
|
281
|
-
Cause.
|
|
293
|
+
Effect.tapCause((cause) =>
|
|
294
|
+
Cause.hasInterruptsOnly(cause) === true ? Effect.void : shutdown(Exit.failCause(cause)),
|
|
282
295
|
),
|
|
283
296
|
Effect.interruptible,
|
|
284
297
|
Effect.tapCauseLogPretty,
|
|
285
298
|
Effect.forkScoped,
|
|
286
299
|
)
|
|
287
300
|
|
|
288
|
-
yield* Queue.
|
|
301
|
+
yield* Queue.await(bootStatusQueue).pipe(
|
|
289
302
|
Effect.andThen(Fiber.interrupt(bootStatusFiber)),
|
|
303
|
+
Effect.interruptible,
|
|
290
304
|
Effect.tapCauseLogPretty,
|
|
291
305
|
Effect.forkScoped,
|
|
292
306
|
)
|
|
@@ -294,7 +308,7 @@ export const makeSingleTabAdapter =
|
|
|
294
308
|
// Get initial snapshot (either from fast-path or from worker)
|
|
295
309
|
const initialResult =
|
|
296
310
|
dataFromFile === undefined
|
|
297
|
-
? yield* runInWorker(
|
|
311
|
+
? yield* runInWorker('GetRecreateSnapshot', innerWorker.GetRecreateSnapshot({})).pipe(
|
|
298
312
|
Effect.map(({ snapshot, migrationsReport }) => ({
|
|
299
313
|
_tag: 'from-leader-worker' as const,
|
|
300
314
|
snapshot,
|
|
@@ -320,58 +334,44 @@ export const makeSingleTabAdapter =
|
|
|
320
334
|
})
|
|
321
335
|
}
|
|
322
336
|
|
|
323
|
-
//
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
.select('seqNumClient', 'seqNumGlobal', 'seqNumRebaseGeneration')
|
|
327
|
-
.orderBy([
|
|
328
|
-
{ col: 'seqNumGlobal', direction: 'desc' },
|
|
329
|
-
{ col: 'seqNumClient', direction: 'desc' },
|
|
330
|
-
])
|
|
331
|
-
.first(),
|
|
332
|
-
)
|
|
337
|
+
// The state snapshot carries its own cursor, so the fast path does not
|
|
338
|
+
// need to export or transfer the eventlog database.
|
|
339
|
+
const initialLeaderHead = yield* StateHead.make({ dbState: sqliteDb }).get
|
|
333
340
|
|
|
334
|
-
|
|
335
|
-
initialLeaderHeadRes !== undefined
|
|
336
|
-
? EventSequenceNumber.Client.Composite.make({
|
|
337
|
-
global: initialLeaderHeadRes.seqNumGlobal,
|
|
338
|
-
client: initialLeaderHeadRes.seqNumClient,
|
|
339
|
-
rebaseGeneration: initialLeaderHeadRes.seqNumRebaseGeneration,
|
|
340
|
-
})
|
|
341
|
-
: EventSequenceNumber.Client.ROOT
|
|
342
|
-
|
|
343
|
-
yield* Effect.addFinalizer((ex) =>
|
|
341
|
+
yield* Effect.addFinalizer((ex: Exit.Exit<unknown, unknown>) =>
|
|
344
342
|
Effect.gen(function* () {
|
|
345
|
-
if (
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
343
|
+
if (Exit.isFailure(ex) === true) {
|
|
344
|
+
const cause = ex.cause
|
|
345
|
+
if (
|
|
346
|
+
Exit.hasInterrupts(ex) === false &&
|
|
347
|
+
Schema.is(IntentionalShutdownCause)(Cause.squash(cause)) === false &&
|
|
348
|
+
Schema.is(StoreInterrupted)(Cause.squash(cause)) === false
|
|
349
|
+
) {
|
|
350
|
+
yield* Effect.logError('[@livestore/adapter-web:single-tab] client-session shutdown', cause)
|
|
351
|
+
return
|
|
352
|
+
}
|
|
354
353
|
}
|
|
354
|
+
|
|
355
|
+
yield* Effect.logDebug('[@livestore/adapter-web:single-tab] client-session shutdown', ex)
|
|
355
356
|
}).pipe(Effect.tapCauseLogPretty, Effect.orDie),
|
|
356
357
|
)
|
|
357
358
|
|
|
358
359
|
const leaderThread: ClientSession['leaderThread'] = {
|
|
359
|
-
export: runInWorker(
|
|
360
|
+
export: runInWorker('Export', innerWorker.Export({})).pipe(
|
|
360
361
|
Effect.timeoutOrDie(10_000),
|
|
361
362
|
Effect.withSpan('@livestore/adapter-web:single-tab:export'),
|
|
362
363
|
),
|
|
363
364
|
|
|
364
365
|
events: {
|
|
365
|
-
pull: ({ cursor }) =>
|
|
366
|
-
runInWorkerStream(new WorkerSchema.LeaderWorkerInnerPullStream({ cursor })).pipe(Stream.orDie),
|
|
366
|
+
pull: ({ cursor }) => runInWorkerStream('PullStream', innerWorker.PullStream({ cursor })).pipe(Stream.orDie),
|
|
367
367
|
push: (batch) =>
|
|
368
|
-
runInWorker(
|
|
368
|
+
runInWorker('PushToLeader', innerWorker.PushToLeader({ batch })).pipe(
|
|
369
369
|
Effect.withSpan('@livestore/adapter-web:single-tab:pushToLeader', {
|
|
370
370
|
attributes: { batchSize: batch.length },
|
|
371
371
|
}),
|
|
372
372
|
),
|
|
373
373
|
stream: (options) =>
|
|
374
|
-
runInWorkerStream(
|
|
374
|
+
runInWorkerStream('StreamEvents', innerWorker.StreamEvents(options)).pipe(
|
|
375
375
|
Stream.withSpan('@livestore/adapter-web:single-tab:streamEvents'),
|
|
376
376
|
Stream.orDie,
|
|
377
377
|
),
|
|
@@ -383,16 +383,16 @@ export const makeSingleTabAdapter =
|
|
|
383
383
|
storageMode: opfsWarning === undefined ? 'persisted' : 'in-memory',
|
|
384
384
|
},
|
|
385
385
|
|
|
386
|
-
getEventlogData: runInWorker(
|
|
386
|
+
getEventlogData: runInWorker('ExportEventlog', innerWorker.ExportEventlog({})).pipe(
|
|
387
387
|
Effect.timeoutOrDie(10_000),
|
|
388
388
|
Effect.withSpan('@livestore/adapter-web:single-tab:getEventlogData'),
|
|
389
389
|
),
|
|
390
390
|
|
|
391
391
|
syncState: Subscribable.make({
|
|
392
|
-
get: runInWorker(
|
|
392
|
+
get: runInWorker('GetLeaderSyncState', innerWorker.GetLeaderSyncState({})).pipe(
|
|
393
393
|
Effect.withSpan('@livestore/adapter-web:single-tab:getLeaderSyncState'),
|
|
394
394
|
),
|
|
395
|
-
changes: runInWorkerStream(
|
|
395
|
+
changes: runInWorkerStream('SyncStateStream', innerWorker.SyncStateStream({})).pipe(Stream.orDie),
|
|
396
396
|
}),
|
|
397
397
|
|
|
398
398
|
sendDevtoolsMessage: (_message) =>
|
|
@@ -400,8 +400,8 @@ export const makeSingleTabAdapter =
|
|
|
400
400
|
Effect.void,
|
|
401
401
|
|
|
402
402
|
networkStatus: Subscribable.make({
|
|
403
|
-
get: runInWorker(
|
|
404
|
-
changes: runInWorkerStream(
|
|
403
|
+
get: runInWorker('GetNetworkStatus', innerWorker.GetNetworkStatus({})).pipe(Effect.orDie),
|
|
404
|
+
changes: runInWorkerStream('NetworkStatusStream', innerWorker.NetworkStatusStream({})).pipe(Stream.orDie),
|
|
405
405
|
}),
|
|
406
406
|
}
|
|
407
407
|
|
|
@@ -427,7 +427,7 @@ export const makeSingleTabAdapter =
|
|
|
427
427
|
})
|
|
428
428
|
|
|
429
429
|
return clientSession
|
|
430
|
-
}).pipe(Effect.provide(Opfs.
|
|
430
|
+
}).pipe(Effect.provide(Opfs.layer), UnknownError.mapToUnknownError)
|
|
431
431
|
|
|
432
432
|
/** Persists clientId/sessionId to storage */
|
|
433
433
|
const getPersistedId = (key: string, storageType: 'session' | 'local') => {
|
|
@@ -484,7 +484,7 @@ const checkOpfsAvailability = Effect.gen(function* () {
|
|
|
484
484
|
const opfs = yield* Opfs.Opfs
|
|
485
485
|
return yield* opfs.getRootDirectoryHandle.pipe(
|
|
486
486
|
Effect.as(undefined),
|
|
487
|
-
Effect.
|
|
487
|
+
Effect.catch((error) => {
|
|
488
488
|
const reason: BootWarningReason =
|
|
489
489
|
Schema.is(WebError.SecurityError)(error) === true || Schema.is(WebError.NotAllowedError)(error) === true
|
|
490
490
|
? 'private-browsing'
|
|
@@ -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
|
}
|