@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type * as otel from '@opentelemetry/api'
|
|
2
2
|
|
|
3
|
-
import type { BootStatus, BootWarningReason, SqliteDb, SyncOptions } from '@livestore/common'
|
|
4
|
-
import { Devtools,
|
|
3
|
+
import type { BootStatus, BootWarningReason, LogConfig, SqliteDb, SyncOptions } from '@livestore/common'
|
|
4
|
+
import { Devtools, StateHead, UnknownError } from '@livestore/common'
|
|
5
5
|
import type { DevtoolsOptions, StreamEventsOptions } from '@livestore/common/leader-thread'
|
|
6
6
|
import {
|
|
7
7
|
configureConnection,
|
|
@@ -10,26 +10,29 @@ import {
|
|
|
10
10
|
makeLeaderThreadLayer,
|
|
11
11
|
streamEventsWithSyncState,
|
|
12
12
|
} from '@livestore/common/leader-thread'
|
|
13
|
-
import type
|
|
14
|
-
import { LiveStoreEvent } from '@livestore/common/schema'
|
|
15
|
-
import * as WebmeshWorker from '@livestore/devtools-web-common/worker'
|
|
13
|
+
import { LiveStoreEvent, type LiveStoreSchema } from '@livestore/common/schema'
|
|
16
14
|
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
17
15
|
import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
|
|
18
16
|
import { isDevEnv, LS_DEV } from '@livestore/utils'
|
|
19
|
-
import type { HttpClient, Scope, WorkerError } from '@livestore/utils/effect'
|
|
20
17
|
import {
|
|
18
|
+
Cause,
|
|
19
|
+
Deferred,
|
|
21
20
|
Effect,
|
|
22
21
|
FetchHttpClient,
|
|
23
22
|
identity,
|
|
24
23
|
Layer,
|
|
25
24
|
OtelTracer,
|
|
26
|
-
|
|
25
|
+
Queue,
|
|
26
|
+
References,
|
|
27
|
+
RpcServer,
|
|
28
|
+
RpcWorker,
|
|
27
29
|
Schema,
|
|
30
|
+
Scope,
|
|
28
31
|
Stream,
|
|
29
32
|
TaskTracing,
|
|
30
|
-
WorkerRunner,
|
|
31
33
|
} from '@livestore/utils/effect'
|
|
32
34
|
import { BrowserWorkerRunner, Opfs, WebError } from '@livestore/utils/effect/browser'
|
|
35
|
+
import * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
33
36
|
|
|
34
37
|
import { cleanupOldStateDbFiles, getStateDbFileName, sanitizeOpfsDir } from '../common/persisted-sqlite.ts'
|
|
35
38
|
import { makeShutdownChannel } from '../common/shutdown-channel.ts'
|
|
@@ -38,11 +41,11 @@ import * as WorkerSchema from '../common/worker-schema.ts'
|
|
|
38
41
|
export type WorkerOptions = {
|
|
39
42
|
schema: LiveStoreSchema
|
|
40
43
|
sync?: SyncOptions
|
|
41
|
-
syncPayloadSchema?: Schema.
|
|
44
|
+
syncPayloadSchema?: Schema.Top
|
|
42
45
|
otelOptions?: {
|
|
43
46
|
tracer?: otel.Tracer
|
|
44
47
|
}
|
|
45
|
-
} & LogConfig.
|
|
48
|
+
} & LogConfig.LoggerOptions
|
|
46
49
|
|
|
47
50
|
if (isDevEnv() === true) {
|
|
48
51
|
globalThis.__debugLiveStoreUtils = {
|
|
@@ -55,239 +58,361 @@ if (isDevEnv() === true) {
|
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
export const makeWorker = (options: WorkerOptions) => {
|
|
58
|
-
makeWorkerEffect(options)
|
|
61
|
+
Effect.runFork(makeWorkerEffect(options))
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
export const makeWorkerEffect = (options: WorkerOptions) => {
|
|
62
|
-
const TracingLive =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
const TracingLive =
|
|
66
|
+
options.otelOptions?.tracer !== undefined
|
|
67
|
+
? OtelTracer.layerWithoutOtelTracer.pipe(
|
|
68
|
+
Layer.provideMerge(Layer.succeed(OtelTracer.OtelTracer, options.otelOptions.tracer)),
|
|
69
|
+
)
|
|
70
|
+
: Layer.empty
|
|
67
71
|
|
|
68
|
-
const runtimeLayer = Layer.mergeAll(
|
|
72
|
+
const runtimeLayer = Layer.mergeAll(
|
|
73
|
+
FetchHttpClient.layer,
|
|
74
|
+
TracingLive,
|
|
75
|
+
Layer.succeed(References.MaxOpsBeforeYield, 4096),
|
|
76
|
+
)
|
|
69
77
|
|
|
70
78
|
return makeWorkerRunnerOuter(options).pipe(
|
|
71
|
-
|
|
72
|
-
WorkerRunner.launch,
|
|
79
|
+
Effect.provide(Layer.provideMerge(RpcServer.layerProtocolWorkerRunner, BrowserWorkerRunner.layer)),
|
|
73
80
|
Effect.scoped,
|
|
74
81
|
Effect.tapCauseLogPretty,
|
|
75
82
|
Effect.annotateLogs({ thread: self.name }),
|
|
76
83
|
Effect.provide(runtimeLayer),
|
|
77
84
|
LS_DEV === true ? TaskTracing.withAsyncTaggingTracing((name) => (console as any).createTask(name)) : identity,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
LogConfig.withLoggerConfig({ logger: options.logger, logLevel: options.logLevel }, { threadName: self.name }),
|
|
85
|
+
Effect.provide(
|
|
86
|
+
Layer.mergeAll(
|
|
87
|
+
options.logger ?? Layer.empty,
|
|
88
|
+
Layer.succeed(References.MinimumLogLevel, options.logLevel ?? (isDevEnv() === true ? 'Debug' : 'Info')),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
85
91
|
)
|
|
86
92
|
}
|
|
87
93
|
|
|
88
|
-
const makeWorkerRunnerOuter = (
|
|
94
|
+
const makeWorkerRunnerOuter = Effect.fn('@livestore/adapter-web:worker:wrapper:InitialMessage')(function* (
|
|
89
95
|
workerOptions: WorkerOptions,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
WorkerRunner.launch,
|
|
98
|
-
Effect.scoped,
|
|
99
|
-
Effect.withSpan('@livestore/adapter-web:worker:wrapper:InitialMessage:innerFiber'),
|
|
100
|
-
Effect.tapCauseLogPretty,
|
|
101
|
-
Effect.provide(
|
|
102
|
-
Layer.mergeAll(
|
|
103
|
-
Opfs.Opfs.Default,
|
|
104
|
-
WebmeshWorker.CacheService.layer({
|
|
105
|
-
nodeName: Devtools.makeNodeName.client.leader({ storeId, clientId }),
|
|
106
|
-
}),
|
|
107
|
-
),
|
|
108
|
-
),
|
|
109
|
-
Effect.forkScoped,
|
|
110
|
-
)
|
|
96
|
+
) {
|
|
97
|
+
// Port coming from client session and forwarded via the shared worker.
|
|
98
|
+
const {
|
|
99
|
+
port: incomingRequestsPort,
|
|
100
|
+
storeId,
|
|
101
|
+
clientId,
|
|
102
|
+
} = yield* RpcWorker.initialMessage(WorkerSchema.LeaderWorkerOuterInitialMessage.payloadSchema)
|
|
111
103
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
104
|
+
return yield* RpcServer.make(WorkerSchema.LeaderWorkerInnerRpcs).pipe(
|
|
105
|
+
Effect.provide(
|
|
106
|
+
Layer.provideMerge(makeWorkerRunnerInner(workerOptions), makeMessagePortRpcServerProtocol(incomingRequestsPort)),
|
|
107
|
+
),
|
|
108
|
+
Effect.withSpan('@livestore/adapter-web:worker:wrapper:InitialMessage:innerFiber'),
|
|
109
|
+
Effect.tapCauseLogPretty,
|
|
110
|
+
Effect.provide(
|
|
111
|
+
Layer.mergeAll(
|
|
112
|
+
Opfs.layer,
|
|
113
|
+
WebmeshWorker.CacheService.layer({
|
|
114
|
+
nodeName: Devtools.makeNodeName.client.leader({ storeId, clientId }),
|
|
115
|
+
}),
|
|
116
|
+
),
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const makeMessagePortRpcServerProtocol = (port: MessagePort): Layer.Layer<RpcServer.Protocol> =>
|
|
122
|
+
Layer.effect(
|
|
123
|
+
RpcServer.Protocol,
|
|
124
|
+
Effect.gen(function* () {
|
|
125
|
+
const disconnects = yield* Queue.unbounded<number>()
|
|
126
|
+
const initialMessage = yield* Deferred.make<unknown>()
|
|
127
|
+
const closed = yield* Deferred.make<void>()
|
|
128
|
+
const clientIds = new Set<number>()
|
|
129
|
+
|
|
130
|
+
return RpcServer.Protocol.of({
|
|
131
|
+
disconnects,
|
|
132
|
+
send: (_clientId, response, transferables) =>
|
|
133
|
+
Effect.sync(() => port.postMessage([1, response], { transfer: (transferables ?? []) as Transferable[] })),
|
|
134
|
+
end: () => Effect.void,
|
|
135
|
+
clientIds: Effect.sync(() => clientIds),
|
|
136
|
+
initialMessage: Effect.asSome(Deferred.await(initialMessage)),
|
|
137
|
+
supportsAck: true,
|
|
138
|
+
supportsTransferables: true,
|
|
139
|
+
supportsSpanPropagation: true,
|
|
140
|
+
supportsNotifications: true,
|
|
141
|
+
run: (writeRequest) =>
|
|
142
|
+
Effect.gen(function* () {
|
|
143
|
+
const context = yield* Effect.context<never>()
|
|
144
|
+
const runFork = Effect.runForkWith(context)
|
|
145
|
+
const onMessage = (event: MessageEvent) => {
|
|
146
|
+
const message = event.data as readonly [0 | 1, unknown?]
|
|
147
|
+
if (message[0] === 1) {
|
|
148
|
+
runFork(
|
|
149
|
+
Effect.gen(function* () {
|
|
150
|
+
yield* Queue.offer(disconnects, 0)
|
|
151
|
+
yield* Deferred.succeed(closed, undefined)
|
|
152
|
+
}),
|
|
153
|
+
)
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const request = message[1] as { readonly _tag?: string; readonly value?: unknown }
|
|
158
|
+
clientIds.add(0)
|
|
159
|
+
if (request._tag === 'InitialMessage') {
|
|
160
|
+
runFork(Deferred.succeed(initialMessage, request.value))
|
|
161
|
+
} else {
|
|
162
|
+
runFork(writeRequest(0, request as never))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
port.addEventListener('message', onMessage)
|
|
167
|
+
port.start()
|
|
168
|
+
port.postMessage([0])
|
|
169
|
+
|
|
170
|
+
return yield* Deferred.await(closed).pipe(
|
|
171
|
+
Effect.andThen(Effect.interrupt),
|
|
172
|
+
Effect.ensuring(
|
|
173
|
+
Effect.sync(() => {
|
|
174
|
+
port.removeEventListener('message', onMessage)
|
|
175
|
+
port.close()
|
|
176
|
+
}),
|
|
177
|
+
),
|
|
178
|
+
)
|
|
179
|
+
}),
|
|
180
|
+
})
|
|
181
|
+
}),
|
|
182
|
+
)
|
|
115
183
|
|
|
116
184
|
const makeWorkerRunnerInner = ({ schema, sync: syncOptions, syncPayloadSchema }: WorkerOptions) =>
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
185
|
+
WorkerSchema.LeaderWorkerInnerRpcs.toLayer(
|
|
186
|
+
Effect.gen(function* () {
|
|
187
|
+
const leaderThreadScope = yield* Scope.make()
|
|
188
|
+
yield* Effect.addFinalizer((exit) => Scope.close(leaderThreadScope, exit))
|
|
189
|
+
|
|
190
|
+
const leaderThreadContextOnce = yield* Effect.cached(
|
|
191
|
+
Effect.gen(function* () {
|
|
192
|
+
const { storageOptions, storeId, clientId, devtoolsEnabled, debugInstanceId, syncPayloadEncoded } =
|
|
193
|
+
yield* RpcWorker.initialMessage(WorkerSchema.LeaderWorkerInnerInitialMessage.payloadSchema)
|
|
194
|
+
|
|
195
|
+
const sqlite3 = yield* Effect.promise(() => loadSqlite3Wasm())
|
|
196
|
+
const makeSqliteDb = sqliteDbFactory({ sqlite3 })
|
|
197
|
+
const services = yield* Effect.context()
|
|
198
|
+
|
|
199
|
+
// Check OPFS availability and determine storage mode
|
|
200
|
+
const opfsCheck = yield* checkOpfsAvailability
|
|
201
|
+
const useOpfs = opfsCheck === undefined
|
|
202
|
+
|
|
203
|
+
// Track boot warning to emit later
|
|
204
|
+
let bootWarning: BootStatus | undefined
|
|
205
|
+
if (useOpfs === false) {
|
|
206
|
+
yield* Effect.logWarning(
|
|
207
|
+
'[@livestore/adapter-web:worker] OPFS unavailable, using in-memory storage',
|
|
208
|
+
opfsCheck,
|
|
209
|
+
)
|
|
210
|
+
bootWarning = { stage: 'warning', ...opfsCheck }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const opfsDirectory = useOpfs === true ? yield* sanitizeOpfsDir(storageOptions.directory, storeId) : undefined
|
|
214
|
+
|
|
215
|
+
const makeOpfsDb = (kind: 'state' | 'eventlog') =>
|
|
216
|
+
Effect.acquireRelease(
|
|
217
|
+
makeSqliteDb({
|
|
218
|
+
_tag: 'opfs',
|
|
219
|
+
opfsDirectory: opfsDirectory!,
|
|
220
|
+
fileName: kind === 'state' ? getStateDbFileName(schema) : 'eventlog.db',
|
|
221
|
+
configureDb: (db) =>
|
|
222
|
+
configureConnection(db, {
|
|
223
|
+
// The persisted databases use the AccessHandlePoolVFS which always uses a single database connection.
|
|
224
|
+
// Multiple connections are not supported. This means that we can use the exclusive locking mode to
|
|
225
|
+
// avoid unnecessary system calls and enable the use of the WAL journal mode without the use of shared memory.
|
|
226
|
+
// TODO bring back exclusive locking mode when `WAL` is working properly
|
|
227
|
+
// lockingMode: 'EXCLUSIVE',
|
|
228
|
+
foreignKeys: true,
|
|
229
|
+
}).pipe(Effect.runSyncWith(services)),
|
|
230
|
+
}),
|
|
231
|
+
(db) =>
|
|
232
|
+
Effect.try({
|
|
233
|
+
try: () => db.close(),
|
|
234
|
+
catch: (cause) => new Cause.UnknownError(cause),
|
|
235
|
+
}).pipe(Effect.ignore),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
const makeInMemoryDb = () =>
|
|
239
|
+
Effect.acquireRelease(
|
|
240
|
+
makeSqliteDb({
|
|
241
|
+
_tag: 'in-memory',
|
|
242
|
+
configureDb: (db) => configureConnection(db, { foreignKeys: true }).pipe(Effect.runSyncWith(services)),
|
|
243
|
+
}),
|
|
244
|
+
(db) =>
|
|
245
|
+
Effect.try({
|
|
246
|
+
try: () => db.close(),
|
|
247
|
+
catch: (cause) => new Cause.UnknownError(cause),
|
|
248
|
+
}).pipe(Effect.ignore),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
// Use OPFS if available, otherwise fall back to in-memory
|
|
252
|
+
const [dbState, dbEventlog] =
|
|
253
|
+
useOpfs === true
|
|
254
|
+
? yield* Effect.all([makeOpfsDb('state'), makeOpfsDb('eventlog')], { concurrency: 2 })
|
|
255
|
+
: yield* Effect.all([makeInMemoryDb(), makeInMemoryDb()], { concurrency: 2 })
|
|
256
|
+
|
|
257
|
+
// Clean up old state database files after successful database creation
|
|
258
|
+
// This prevents OPFS file pool capacity exhaustion from accumulated state db files after schema changes/migrations
|
|
259
|
+
if (dbState.metadata._tag === 'opfs') {
|
|
260
|
+
yield* cleanupOldStateDbFiles({
|
|
261
|
+
vfs: dbState.metadata.vfs,
|
|
262
|
+
currentSchema: schema,
|
|
263
|
+
opfsDirectory: dbState.metadata.persistenceInfo.opfsDirectory,
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const devtoolsOptions = yield* makeDevtoolsOptions({ devtoolsEnabled, dbState, dbEventlog })
|
|
268
|
+
const shutdownChannel = yield* makeShutdownChannel(storeId)
|
|
269
|
+
|
|
270
|
+
return yield* Layer.buildWithScope(
|
|
271
|
+
makeLeaderThreadLayer({
|
|
272
|
+
schema,
|
|
273
|
+
storeId,
|
|
274
|
+
clientId,
|
|
275
|
+
makeSqliteDb,
|
|
276
|
+
syncOptions,
|
|
277
|
+
dbState,
|
|
278
|
+
dbEventlog,
|
|
279
|
+
devtoolsOptions,
|
|
280
|
+
shutdownChannel,
|
|
281
|
+
syncPayloadEncoded,
|
|
282
|
+
syncPayloadSchema: syncPayloadSchema as Schema.Decoder<Schema.Json, never> | undefined,
|
|
283
|
+
...(bootWarning !== undefined ? { bootWarning } : {}),
|
|
284
|
+
}).pipe(Layer.provide(StateHead.layer({ dbState }))),
|
|
285
|
+
leaderThreadScope,
|
|
134
286
|
)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
// avoid unnecessary system calls and enable the use of the WAL journal mode without the use of shared memory.
|
|
150
|
-
// TODO bring back exclusive locking mode when `WAL` is working properly
|
|
151
|
-
// lockingMode: 'EXCLUSIVE',
|
|
152
|
-
foreignKeys: true,
|
|
153
|
-
}).pipe(Effect.provide(runtime), Effect.runSync),
|
|
154
|
-
}).pipe(Effect.acquireRelease((db) => Effect.try(() => db.close()).pipe(Effect.ignoreLogged)))
|
|
155
|
-
|
|
156
|
-
const makeInMemoryDb = () =>
|
|
157
|
-
makeSqliteDb({
|
|
158
|
-
_tag: 'in-memory',
|
|
159
|
-
configureDb: (db) =>
|
|
160
|
-
configureConnection(db, { foreignKeys: true }).pipe(Effect.provide(runtime), Effect.runSync),
|
|
161
|
-
}).pipe(Effect.acquireRelease((db) => Effect.try(() => db.close()).pipe(Effect.ignoreLogged)))
|
|
162
|
-
|
|
163
|
-
// Use OPFS if available, otherwise fall back to in-memory
|
|
164
|
-
const [dbState, dbEventlog] = useOpfs === true
|
|
165
|
-
? yield* Effect.all([makeOpfsDb('state'), makeOpfsDb('eventlog')], { concurrency: 2 })
|
|
166
|
-
: yield* Effect.all([makeInMemoryDb(), makeInMemoryDb()], { concurrency: 2 })
|
|
167
|
-
|
|
168
|
-
// Clean up old state database files after successful database creation
|
|
169
|
-
// This prevents OPFS file pool capacity exhaustion from accumulated state db files after schema changes/migrations
|
|
170
|
-
if (dbState.metadata._tag === 'opfs') {
|
|
171
|
-
yield* cleanupOldStateDbFiles({
|
|
172
|
-
vfs: dbState.metadata.vfs,
|
|
173
|
-
currentSchema: schema,
|
|
174
|
-
opfsDirectory: dbState.metadata.persistenceInfo.opfsDirectory,
|
|
175
|
-
})
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const devtoolsOptions = yield* makeDevtoolsOptions({ devtoolsEnabled, dbState, dbEventlog })
|
|
179
|
-
const shutdownChannel = yield* makeShutdownChannel(storeId)
|
|
180
|
-
|
|
181
|
-
return makeLeaderThreadLayer({
|
|
182
|
-
schema,
|
|
183
|
-
storeId,
|
|
184
|
-
clientId,
|
|
185
|
-
makeSqliteDb,
|
|
186
|
-
syncOptions,
|
|
187
|
-
dbState,
|
|
188
|
-
dbEventlog,
|
|
189
|
-
devtoolsOptions,
|
|
190
|
-
shutdownChannel,
|
|
191
|
-
syncPayloadEncoded,
|
|
192
|
-
syncPayloadSchema,
|
|
193
|
-
...(bootWarning !== undefined ? { bootWarning } : {}),
|
|
287
|
+
}).pipe(
|
|
288
|
+
Scope.provide(leaderThreadScope),
|
|
289
|
+
Effect.tapCauseLogPretty,
|
|
290
|
+
UnknownError.mapToUnknownError,
|
|
291
|
+
Effect.withPerformanceMeasure('@livestore/adapter-web:worker:InitialMessage'),
|
|
292
|
+
Effect.withSpan('@livestore/adapter-web:worker:InitialMessage'),
|
|
293
|
+
Effect.orDie,
|
|
294
|
+
),
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
const provideLeaderThread = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
298
|
+
Effect.gen(function* () {
|
|
299
|
+
const leaderThreadContext = yield* leaderThreadContextOnce
|
|
300
|
+
return yield* effect.pipe(Effect.provide(leaderThreadContext))
|
|
194
301
|
})
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
),
|
|
203
|
-
GetRecreateSnapshot: Effect.fn('@livestore/adapter-web:worker:GetRecreateSnapshot')(function* () {
|
|
204
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
302
|
+
const provideLeaderThreadStream = <A, E, R>(stream: Stream.Stream<A, E, R>) =>
|
|
303
|
+
Stream.unwrap(
|
|
304
|
+
Effect.gen(function* () {
|
|
305
|
+
const leaderThreadContext = yield* leaderThreadContextOnce
|
|
306
|
+
return stream.pipe(Stream.provide(leaderThreadContext))
|
|
307
|
+
}),
|
|
308
|
+
)
|
|
205
309
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
310
|
+
return WorkerSchema.LeaderWorkerInnerRpcs.of({
|
|
311
|
+
GetRecreateSnapshot: () =>
|
|
312
|
+
Effect.gen(function* () {
|
|
313
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
209
314
|
|
|
210
|
-
|
|
315
|
+
// NOTE we can only return the cached snapshot once as it's transferred (i.e. disposed), so we need to set it to undefined
|
|
316
|
+
// const cachedSnapshot =
|
|
317
|
+
// result._tag === 'Recreate' ? yield* Ref.getAndSet(result.snapshotRef, undefined) : undefined
|
|
211
318
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
319
|
+
// return cachedSnapshot ?? workerCtx.db.export()
|
|
320
|
+
|
|
321
|
+
const snapshot = workerCtx.dbState.export()
|
|
322
|
+
return { snapshot, migrationsReport: workerCtx.initialState.migrationsReport }
|
|
323
|
+
}).pipe(provideLeaderThread),
|
|
324
|
+
PullStream: ({ cursor }) =>
|
|
325
|
+
Effect.gen(function* () {
|
|
326
|
+
const { syncProcessor } = yield* LeaderThreadCtx // <- syncState comes from here
|
|
327
|
+
return syncProcessor.pull({ cursor })
|
|
328
|
+
}).pipe(
|
|
329
|
+
provideLeaderThread,
|
|
330
|
+
Stream.unwrap,
|
|
331
|
+
// For debugging purposes
|
|
332
|
+
// Stream.tapLogWithLabel('@livestore/adapter-web:worker:PullStream'),
|
|
333
|
+
),
|
|
334
|
+
PushToLeader: ({ batch }) =>
|
|
335
|
+
Effect.andThen(LeaderThreadCtx, ({ syncProcessor }) =>
|
|
336
|
+
syncProcessor.push(batch.map((event) => new LiveStoreEvent.Client.EncodedWithMeta(event))),
|
|
337
|
+
).pipe(
|
|
338
|
+
provideLeaderThread,
|
|
339
|
+
Effect.uninterruptible,
|
|
340
|
+
Effect.withSpan('@livestore/adapter-web:worker:PushToLeader'),
|
|
341
|
+
),
|
|
342
|
+
StreamEvents: (options) =>
|
|
343
|
+
LeaderThreadCtx.pipe(
|
|
344
|
+
Effect.map(({ dbEventlog, syncProcessor }) =>
|
|
345
|
+
streamEventsWithSyncState({
|
|
346
|
+
dbEventlog,
|
|
347
|
+
syncState: syncProcessor.syncState,
|
|
348
|
+
options: options as StreamEventsOptions,
|
|
349
|
+
}),
|
|
350
|
+
),
|
|
351
|
+
provideLeaderThread,
|
|
352
|
+
Stream.unwrap,
|
|
353
|
+
Stream.withSpan('@livestore/adapter-web:worker:StreamEvents'),
|
|
354
|
+
),
|
|
355
|
+
Export: () =>
|
|
356
|
+
LeaderThreadCtx.pipe(
|
|
357
|
+
Effect.flatMap((_) => Effect.sync(() => _.dbState.export())),
|
|
358
|
+
provideLeaderThread,
|
|
359
|
+
Effect.withSpan('@livestore/adapter-web:worker:Export'),
|
|
360
|
+
),
|
|
361
|
+
ExportEventlog: () =>
|
|
362
|
+
LeaderThreadCtx.pipe(
|
|
363
|
+
Effect.flatMap((_) => Effect.sync(() => _.dbEventlog.export())),
|
|
364
|
+
provideLeaderThread,
|
|
365
|
+
Effect.withSpan('@livestore/adapter-web:worker:ExportEventlog'),
|
|
366
|
+
),
|
|
367
|
+
BootStatusStream: () =>
|
|
368
|
+
LeaderThreadCtx.pipe(
|
|
369
|
+
Effect.map((_) => Stream.fromQueue(_.bootStatusQueue)),
|
|
370
|
+
provideLeaderThread,
|
|
371
|
+
Stream.unwrap,
|
|
372
|
+
),
|
|
373
|
+
GetLeaderHead: () =>
|
|
374
|
+
Effect.gen(function* () {
|
|
375
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
376
|
+
return Eventlog.getClientHeadFromDb(workerCtx.dbEventlog)
|
|
377
|
+
}).pipe(provideLeaderThread),
|
|
378
|
+
GetLeaderSyncState: () =>
|
|
379
|
+
Effect.gen(function* () {
|
|
380
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
381
|
+
return yield* workerCtx.syncProcessor.syncState
|
|
382
|
+
}).pipe(provideLeaderThread),
|
|
383
|
+
SyncStateStream: () =>
|
|
384
|
+
Effect.gen(function* () {
|
|
385
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
386
|
+
return workerCtx.syncProcessor.syncState.changes
|
|
387
|
+
}).pipe(provideLeaderThread, Stream.unwrap),
|
|
388
|
+
GetNetworkStatus: () =>
|
|
389
|
+
Effect.gen(function* () {
|
|
390
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
391
|
+
return yield* workerCtx.networkStatus
|
|
392
|
+
}).pipe(provideLeaderThread),
|
|
393
|
+
NetworkStatusStream: () =>
|
|
394
|
+
Effect.gen(function* () {
|
|
395
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
396
|
+
return workerCtx.networkStatus.changes
|
|
397
|
+
}).pipe(provideLeaderThread, Stream.unwrap),
|
|
398
|
+
Shutdown: Effect.fn('@livestore/adapter-web:worker:Shutdown')(function* () {
|
|
399
|
+
yield* Effect.logDebug('[@livestore/adapter-web:worker] Shutdown')
|
|
400
|
+
|
|
401
|
+
// Buy some time for Otel to flush
|
|
402
|
+
// TODO find a cleaner way to do this
|
|
403
|
+
yield* Effect.sleep(300)
|
|
242
404
|
}),
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Effect.withSpan('@livestore/adapter-web:worker:ExportEventlog'),
|
|
253
|
-
),
|
|
254
|
-
BootStatusStream: () =>
|
|
255
|
-
Effect.andThen(LeaderThreadCtx, (_) => Stream.fromQueue(_.bootStatusQueue)).pipe(Stream.unwrap),
|
|
256
|
-
GetLeaderHead: Effect.fn('@livestore/adapter-web:worker:GetLeaderHead')(function* () {
|
|
257
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
258
|
-
return Eventlog.getClientHeadFromDb(workerCtx.dbEventlog)
|
|
259
|
-
}),
|
|
260
|
-
GetLeaderSyncState: Effect.fn('@livestore/adapter-web:worker:GetLeaderSyncState')(function* () {
|
|
261
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
262
|
-
return yield* workerCtx.syncProcessor.syncState
|
|
263
|
-
}),
|
|
264
|
-
SyncStateStream: () =>
|
|
265
|
-
Effect.gen(function* () {
|
|
266
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
267
|
-
return workerCtx.syncProcessor.syncState.changes
|
|
268
|
-
}).pipe(Stream.unwrapScoped),
|
|
269
|
-
GetNetworkStatus: Effect.fn('@livestore/adapter-web:worker:GetNetworkStatus')(function* () {
|
|
270
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
271
|
-
return yield* workerCtx.networkStatus
|
|
272
|
-
}),
|
|
273
|
-
NetworkStatusStream: () =>
|
|
274
|
-
Effect.gen(function* () {
|
|
275
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
276
|
-
return workerCtx.networkStatus.changes
|
|
277
|
-
}).pipe(Stream.unwrapScoped),
|
|
278
|
-
Shutdown: Effect.fn('@livestore/adapter-web:worker:Shutdown')(function* () {
|
|
279
|
-
yield* Effect.logDebug('[@livestore/adapter-web:worker] Shutdown')
|
|
280
|
-
|
|
281
|
-
// Buy some time for Otel to flush
|
|
282
|
-
// TODO find a cleaner way to do this
|
|
283
|
-
yield* Effect.sleep(300)
|
|
405
|
+
ExtraDevtoolsMessage: ({ message }) =>
|
|
406
|
+
Effect.andThen(LeaderThreadCtx, (_) => Queue.offer(_.extraIncomingMessagesQueue, message)).pipe(
|
|
407
|
+
provideLeaderThread,
|
|
408
|
+
Effect.asVoid,
|
|
409
|
+
Effect.withSpan('@livestore/adapter-web:worker:ExtraDevtoolsMessage'),
|
|
410
|
+
),
|
|
411
|
+
'WebmeshWorker.CreateConnection': (payload) =>
|
|
412
|
+
WebmeshWorker.CreateConnection(payload).pipe(provideLeaderThreadStream),
|
|
413
|
+
})
|
|
284
414
|
}),
|
|
285
|
-
|
|
286
|
-
Effect.andThen(LeaderThreadCtx, (_) => _.extraIncomingMessagesQueue.offer(message)).pipe(
|
|
287
|
-
Effect.withSpan('@livestore/adapter-web:worker:ExtraDevtoolsMessage'),
|
|
288
|
-
),
|
|
289
|
-
'DevtoolsWebCommon.CreateConnection': WebmeshWorker.CreateConnection,
|
|
290
|
-
})
|
|
415
|
+
)
|
|
291
416
|
|
|
292
417
|
const makeDevtoolsOptions = ({
|
|
293
418
|
devtoolsEnabled,
|
|
@@ -330,7 +455,7 @@ const checkOpfsAvailability = Effect.gen(function* () {
|
|
|
330
455
|
const opfs = yield* Opfs.Opfs
|
|
331
456
|
return yield* opfs.getRootDirectoryHandle.pipe(
|
|
332
457
|
Effect.as(undefined),
|
|
333
|
-
Effect.
|
|
458
|
+
Effect.catch((error) => {
|
|
334
459
|
const reason: BootWarningReason =
|
|
335
460
|
Schema.is(WebError.SecurityError)(error) === true || Schema.is(WebError.NotAllowedError)(error) === true
|
|
336
461
|
? 'private-browsing'
|