@livestore/adapter-web 0.4.0-dev.9 → 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/README.md +5 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/in-memory/in-memory-adapter.d.ts +50 -8
- package/dist/in-memory/in-memory-adapter.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.js +100 -39
- package/dist/in-memory/in-memory-adapter.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/single-tab/mod.d.ts +15 -0
- package/dist/single-tab/mod.d.ts.map +1 -0
- package/dist/single-tab/mod.js +15 -0
- package/dist/single-tab/mod.js.map +1 -0
- package/dist/single-tab/single-tab-adapter.d.ts +108 -0
- package/dist/single-tab/single-tab-adapter.d.ts.map +1 -0
- package/dist/single-tab/single-tab-adapter.js +252 -0
- package/dist/single-tab/single-tab-adapter.js.map +1 -0
- package/dist/web-worker/client-session/client-session-devtools.d.ts +4 -5
- package/dist/web-worker/client-session/client-session-devtools.d.ts.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.js +27 -14
- 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 +18 -0
- package/dist/web-worker/client-session/persisted-adapter.d.ts.map +1 -1
- package/dist/web-worker/client-session/persisted-adapter.js +170 -103
- package/dist/web-worker/client-session/persisted-adapter.js.map +1 -1
- package/dist/web-worker/client-session/sqlite-loader.d.ts +2 -0
- package/dist/web-worker/client-session/sqlite-loader.d.ts.map +1 -0
- package/dist/web-worker/client-session/sqlite-loader.js +16 -0
- package/dist/web-worker/client-session/sqlite-loader.js.map +1 -0
- package/dist/web-worker/common/persisted-sqlite.d.ts +15 -35
- package/dist/web-worker/common/persisted-sqlite.d.ts.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.js +91 -201
- 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/shutdown-channel.d.ts +3 -2
- package/dist/web-worker/common/shutdown-channel.d.ts.map +1 -1
- package/dist/web-worker/common/shutdown-channel.js +2 -2
- package/dist/web-worker/common/shutdown-channel.js.map +1 -1
- package/dist/web-worker/common/webmesh-node-names.d.ts +4 -0
- package/dist/web-worker/common/webmesh-node-names.d.ts.map +1 -0
- package/dist/web-worker/common/webmesh-node-names.js +2 -0
- package/dist/web-worker/common/webmesh-node-names.js.map +1 -0
- package/dist/web-worker/common/worker-disconnect-channel.d.ts +3 -10
- package/dist/web-worker/common/worker-disconnect-channel.d.ts.map +1 -1
- package/dist/web-worker/common/worker-disconnect-channel.js +4 -4
- package/dist/web-worker/common/worker-disconnect-channel.js.map +1 -1
- package/dist/web-worker/common/worker-schema.d.ts +295 -146
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.js +89 -54
- package/dist/web-worker/common/worker-schema.js.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts +6 -6
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.js +191 -80
- 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 -1
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.js +91 -94
- 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 +47 -19
- package/src/in-memory/in-memory-adapter.ts +147 -73
- package/src/index.ts +15 -1
- package/src/single-tab/mod.ts +15 -0
- package/src/single-tab/single-tab-adapter.ts +499 -0
- package/src/web-worker/ambient.d.ts +7 -24
- package/src/web-worker/client-session/client-session-devtools.ts +47 -25
- package/src/web-worker/client-session/devtools-web-channel.ts +57 -0
- package/src/web-worker/client-session/persisted-adapter.ts +259 -163
- package/src/web-worker/client-session/sqlite-loader.ts +19 -0
- package/src/web-worker/common/persisted-sqlite.ts +204 -309
- package/src/web-worker/common/rpc-worker.ts +39 -0
- package/src/web-worker/common/shutdown-channel.ts +10 -3
- package/src/web-worker/common/webmesh-node-names.ts +1 -0
- package/src/web-worker/common/worker-disconnect-channel.ts +11 -4
- package/src/web-worker/common/worker-schema.ts +138 -131
- package/src/web-worker/leader-worker/make-leader-worker.ts +396 -194
- package/src/web-worker/shared-worker/make-shared-worker.ts +143 -146
- package/src/web-worker/vite-dev-polyfill.ts +1 -2
- package/dist/opfs-utils.d.ts +0 -7
- package/dist/opfs-utils.d.ts.map +0 -1
- package/dist/opfs-utils.js +0 -43
- package/dist/opfs-utils.js.map +0 -1
- package/src/opfs-utils.ts +0 -61
|
@@ -1,31 +1,39 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import type * as otel from '@opentelemetry/api'
|
|
2
|
+
|
|
3
|
+
import type { BootStatus, BootWarningReason, LogConfig, SqliteDb, SyncOptions } from '@livestore/common'
|
|
4
|
+
import { Devtools, StateHead, UnknownError } from '@livestore/common'
|
|
5
|
+
import type { DevtoolsOptions, StreamEventsOptions } from '@livestore/common/leader-thread'
|
|
6
|
+
import {
|
|
7
|
+
configureConnection,
|
|
8
|
+
Eventlog,
|
|
9
|
+
LeaderThreadCtx,
|
|
10
|
+
makeLeaderThreadLayer,
|
|
11
|
+
streamEventsWithSyncState,
|
|
12
|
+
} from '@livestore/common/leader-thread'
|
|
13
|
+
import { LiveStoreEvent, type LiveStoreSchema } from '@livestore/common/schema'
|
|
8
14
|
import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser'
|
|
9
15
|
import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
|
|
10
16
|
import { isDevEnv, LS_DEV } from '@livestore/utils'
|
|
11
|
-
import type { HttpClient, Scope, WorkerError } from '@livestore/utils/effect'
|
|
12
17
|
import {
|
|
13
|
-
|
|
18
|
+
Cause,
|
|
19
|
+
Deferred,
|
|
14
20
|
Effect,
|
|
15
21
|
FetchHttpClient,
|
|
16
22
|
identity,
|
|
17
23
|
Layer,
|
|
18
|
-
Logger,
|
|
19
|
-
LogLevel,
|
|
20
24
|
OtelTracer,
|
|
21
|
-
|
|
25
|
+
Queue,
|
|
26
|
+
References,
|
|
27
|
+
RpcServer,
|
|
28
|
+
RpcWorker,
|
|
29
|
+
Schema,
|
|
30
|
+
Scope,
|
|
22
31
|
Stream,
|
|
23
32
|
TaskTracing,
|
|
24
|
-
WorkerRunner,
|
|
25
33
|
} from '@livestore/utils/effect'
|
|
26
|
-
import
|
|
34
|
+
import { BrowserWorkerRunner, Opfs, WebError } from '@livestore/utils/effect/browser'
|
|
35
|
+
import * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
27
36
|
|
|
28
|
-
import * as OpfsUtils from '../../opfs-utils.ts'
|
|
29
37
|
import { cleanupOldStateDbFiles, getStateDbFileName, sanitizeOpfsDir } from '../common/persisted-sqlite.ts'
|
|
30
38
|
import { makeShutdownChannel } from '../common/shutdown-channel.ts'
|
|
31
39
|
import * as WorkerSchema from '../common/worker-schema.ts'
|
|
@@ -33,210 +41,378 @@ import * as WorkerSchema from '../common/worker-schema.ts'
|
|
|
33
41
|
export type WorkerOptions = {
|
|
34
42
|
schema: LiveStoreSchema
|
|
35
43
|
sync?: SyncOptions
|
|
44
|
+
syncPayloadSchema?: Schema.Top
|
|
36
45
|
otelOptions?: {
|
|
37
46
|
tracer?: otel.Tracer
|
|
38
47
|
}
|
|
39
|
-
}
|
|
48
|
+
} & LogConfig.LoggerOptions
|
|
40
49
|
|
|
41
|
-
if (isDevEnv()) {
|
|
50
|
+
if (isDevEnv() === true) {
|
|
42
51
|
globalThis.__debugLiveStoreUtils = {
|
|
43
|
-
opfs:
|
|
52
|
+
opfs: Opfs.debugUtils,
|
|
44
53
|
blobUrl: (buffer: Uint8Array<ArrayBuffer>) =>
|
|
45
54
|
URL.createObjectURL(new Blob([buffer], { type: 'application/octet-stream' })),
|
|
46
|
-
runSync: (effect: Effect.Effect<
|
|
47
|
-
runFork: (effect: Effect.Effect<
|
|
55
|
+
runSync: <A, E>(effect: Effect.Effect<A, E>) => Effect.runSync(effect),
|
|
56
|
+
runFork: <A, E>(effect: Effect.Effect<A, E>) => Effect.runFork(effect),
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
export const makeWorker = (options: WorkerOptions) => {
|
|
52
|
-
makeWorkerEffect(options)
|
|
61
|
+
Effect.runFork(makeWorkerEffect(options))
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
export const makeWorkerEffect = (options: WorkerOptions) => {
|
|
56
|
-
const TracingLive =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
|
61
71
|
|
|
62
|
-
const
|
|
72
|
+
const runtimeLayer = Layer.mergeAll(
|
|
73
|
+
FetchHttpClient.layer,
|
|
74
|
+
TracingLive,
|
|
75
|
+
Layer.succeed(References.MaxOpsBeforeYield, 4096),
|
|
76
|
+
)
|
|
63
77
|
|
|
64
78
|
return makeWorkerRunnerOuter(options).pipe(
|
|
65
|
-
|
|
66
|
-
WorkerRunner.launch,
|
|
79
|
+
Effect.provide(Layer.provideMerge(RpcServer.layerProtocolWorkerRunner, BrowserWorkerRunner.layer)),
|
|
67
80
|
Effect.scoped,
|
|
68
81
|
Effect.tapCauseLogPretty,
|
|
69
82
|
Effect.annotateLogs({ thread: self.name }),
|
|
70
|
-
Effect.provide(
|
|
71
|
-
LS_DEV ? TaskTracing.withAsyncTaggingTracing((name) => (console as any).createTask(name)) : identity,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
Logger.withMinimumLogLevel(LogLevel.Debug),
|
|
83
|
+
Effect.provide(runtimeLayer),
|
|
84
|
+
LS_DEV === true ? TaskTracing.withAsyncTaggingTracing((name) => (console as any).createTask(name)) : identity,
|
|
85
|
+
Effect.provide(
|
|
86
|
+
Layer.mergeAll(
|
|
87
|
+
options.logger ?? Layer.empty,
|
|
88
|
+
Layer.succeed(References.MinimumLogLevel, options.logLevel ?? (isDevEnv() === true ? 'Debug' : 'Info')),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
79
91
|
)
|
|
80
92
|
}
|
|
81
93
|
|
|
82
|
-
const makeWorkerRunnerOuter = (
|
|
94
|
+
const makeWorkerRunnerOuter = Effect.fn('@livestore/adapter-web:worker:wrapper:InitialMessage')(function* (
|
|
83
95
|
workerOptions: WorkerOptions,
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
WorkerRunner.launch,
|
|
92
|
-
Effect.scoped,
|
|
93
|
-
Effect.withSpan('@livestore/adapter-web:worker:wrapper:InitialMessage:innerFiber'),
|
|
94
|
-
Effect.tapCauseLogPretty,
|
|
95
|
-
Effect.provide(
|
|
96
|
-
WebmeshWorker.CacheService.layer({ nodeName: Devtools.makeNodeName.client.leader({ storeId, clientId }) }),
|
|
97
|
-
),
|
|
98
|
-
Effect.forkScoped,
|
|
99
|
-
)
|
|
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)
|
|
100
103
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
+
})
|
|
104
120
|
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const makeDb = (kind: 'state' | 'eventlog') =>
|
|
114
|
-
makeSqliteDb({
|
|
115
|
-
_tag: 'opfs',
|
|
116
|
-
opfsDirectory: sanitizeOpfsDir(storageOptions.directory, storeId),
|
|
117
|
-
fileName: kind === 'state' ? getStateDbFileName(schema) : 'eventlog.db',
|
|
118
|
-
configureDb: (db) =>
|
|
119
|
-
configureConnection(db, {
|
|
120
|
-
// The persisted databases use the AccessHandlePoolVFS which always uses a single database connection.
|
|
121
|
-
// Multiple connections are not supported. This means that we can use the exclusive locking mode to
|
|
122
|
-
// avoid unnecessary system calls and enable the use of the WAL journal mode without the use of shared memory.
|
|
123
|
-
// TODO bring back exclusive locking mode when `WAL` is working properly
|
|
124
|
-
// lockingMode: 'EXCLUSIVE',
|
|
125
|
-
foreignKeys: true,
|
|
126
|
-
}).pipe(Effect.provide(runtime), Effect.runSync),
|
|
127
|
-
}).pipe(Effect.acquireRelease((db) => Effect.try(() => db.close()).pipe(Effect.ignoreLogged)))
|
|
128
|
-
|
|
129
|
-
// Might involve some async work, so we're running them concurrently
|
|
130
|
-
const [dbState, dbEventlog] = yield* Effect.all([makeDb('state'), makeDb('eventlog')], {
|
|
131
|
-
concurrency: 2,
|
|
132
|
-
})
|
|
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>()
|
|
133
129
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
Effect.tapCauseLogPretty,
|
|
161
|
-
UnexpectedError.mapToUnexpectedError,
|
|
162
|
-
Effect.withPerformanceMeasure('@livestore/adapter-web:worker:InitialMessage'),
|
|
163
|
-
Effect.withSpan('@livestore/adapter-web:worker:InitialMessage'),
|
|
164
|
-
Effect.annotateSpans({ debugInstanceId }),
|
|
165
|
-
Layer.unwrapScoped,
|
|
166
|
-
),
|
|
167
|
-
GetRecreateSnapshot: () =>
|
|
168
|
-
Effect.gen(function* () {
|
|
169
|
-
const workerCtx = yield* LeaderThreadCtx
|
|
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
|
+
}
|
|
170
156
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
+
}
|
|
174
165
|
|
|
175
|
-
|
|
166
|
+
port.addEventListener('message', onMessage)
|
|
167
|
+
port.start()
|
|
168
|
+
port.postMessage([0])
|
|
176
169
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
)
|
|
183
|
+
|
|
184
|
+
const makeWorkerRunnerInner = ({ schema, sync: syncOptions, syncPayloadSchema }: WorkerOptions) =>
|
|
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,
|
|
286
|
+
)
|
|
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,
|
|
198
294
|
),
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
Effect.
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
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))
|
|
301
|
+
})
|
|
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
|
+
)
|
|
309
|
+
|
|
310
|
+
return WorkerSchema.LeaderWorkerInnerRpcs.of({
|
|
311
|
+
GetRecreateSnapshot: () =>
|
|
312
|
+
Effect.gen(function* () {
|
|
313
|
+
const workerCtx = yield* LeaderThreadCtx
|
|
314
|
+
|
|
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
|
|
318
|
+
|
|
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)
|
|
404
|
+
}),
|
|
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
|
+
})
|
|
414
|
+
}),
|
|
415
|
+
)
|
|
240
416
|
|
|
241
417
|
const makeDevtoolsOptions = ({
|
|
242
418
|
devtoolsEnabled,
|
|
@@ -246,7 +422,7 @@ const makeDevtoolsOptions = ({
|
|
|
246
422
|
devtoolsEnabled: boolean
|
|
247
423
|
dbState: SqliteDb
|
|
248
424
|
dbEventlog: SqliteDb
|
|
249
|
-
}): Effect.Effect<DevtoolsOptions,
|
|
425
|
+
}): Effect.Effect<DevtoolsOptions, UnknownError, Scope.Scope | WebmeshWorker.CacheService> =>
|
|
250
426
|
Effect.gen(function* () {
|
|
251
427
|
if (devtoolsEnabled === false) {
|
|
252
428
|
return { enabled: false }
|
|
@@ -256,13 +432,39 @@ const makeDevtoolsOptions = ({
|
|
|
256
432
|
|
|
257
433
|
return {
|
|
258
434
|
enabled: true,
|
|
259
|
-
boot: Effect.
|
|
260
|
-
|
|
435
|
+
boot: Effect.succeed({
|
|
436
|
+
node,
|
|
437
|
+
persistenceInfo: {
|
|
261
438
|
state: dbState.metadata.persistenceInfo,
|
|
262
439
|
eventlog: dbEventlog.metadata.persistenceInfo,
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return { node, persistenceInfo, mode: 'direct' }
|
|
440
|
+
},
|
|
441
|
+
mode: 'direct' as const,
|
|
266
442
|
}),
|
|
267
443
|
}
|
|
268
444
|
})
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Attempts to access OPFS and returns a warning if unavailable.
|
|
448
|
+
*
|
|
449
|
+
* Common failure scenarios:
|
|
450
|
+
* - Safari/Firefox private browsing: SecurityError or NotAllowedError
|
|
451
|
+
* - Permission denied: NotAllowedError
|
|
452
|
+
* - Quota exceeded: QuotaExceededError
|
|
453
|
+
*/
|
|
454
|
+
const checkOpfsAvailability = Effect.gen(function* () {
|
|
455
|
+
const opfs = yield* Opfs.Opfs
|
|
456
|
+
return yield* opfs.getRootDirectoryHandle.pipe(
|
|
457
|
+
Effect.as(undefined),
|
|
458
|
+
Effect.catch((error) => {
|
|
459
|
+
const reason: BootWarningReason =
|
|
460
|
+
Schema.is(WebError.SecurityError)(error) === true || Schema.is(WebError.NotAllowedError)(error) === true
|
|
461
|
+
? 'private-browsing'
|
|
462
|
+
: 'storage-unavailable'
|
|
463
|
+
const message =
|
|
464
|
+
reason === 'private-browsing'
|
|
465
|
+
? 'Storage unavailable in private browsing mode. LiveStore will continue without persistence.'
|
|
466
|
+
: 'Storage access denied. LiveStore will continue without persistence.'
|
|
467
|
+
return Effect.succeed({ reason, message } as const)
|
|
468
|
+
}),
|
|
469
|
+
)
|
|
470
|
+
})
|