@livestore/common 0.0.55-dev.0 → 0.0.55-dev.2

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.
@@ -10,149 +10,95 @@ const requestId = Schema.String
10
10
  const channelId = Schema.String
11
11
  const liveStoreVersion = Schema.Literal(pkgVersion)
12
12
 
13
- export class SnapshotReq extends Schema.TaggedStruct('LSD.SnapshotReq', {
14
- liveStoreVersion,
15
- requestId,
16
- channelId,
17
- }).annotations({ identifier: 'LSD.SnapshotReq' }) {}
18
-
19
- export class SnapshotRes extends Schema.TaggedStruct('LSD.SnapshotRes', {
20
- liveStoreVersion,
21
- requestId,
13
+ const LSDMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
14
+ Schema.TaggedStruct(tag, {
15
+ liveStoreVersion,
16
+ ...fields,
17
+ }).annotations({ identifier: tag })
18
+
19
+ const LSDChannelMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
20
+ LSDMessage(tag, {
21
+ channelId,
22
+ ...fields,
23
+ })
24
+
25
+ const LSDReqResMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
26
+ LSDChannelMessage(tag, {
27
+ requestId,
28
+ ...fields,
29
+ })
30
+
31
+ export class SnapshotReq extends LSDReqResMessage('LSD.SnapshotReq', {}) {}
32
+
33
+ export class SnapshotRes extends LSDReqResMessage('LSD.SnapshotRes', {
22
34
  snapshot: Transferable.Uint8Array,
23
- }).annotations({ identifier: 'LSD.SnapshotRes' }) {}
35
+ }) {}
24
36
 
25
- export class LoadDatabaseFileReq extends Schema.TaggedStruct('LSD.LoadDatabaseFileReq', {
26
- liveStoreVersion,
27
- requestId,
28
- channelId,
37
+ export class LoadDatabaseFileReq extends LSDReqResMessage('LSD.LoadDatabaseFileReq', {
29
38
  data: Transferable.Uint8Array,
30
- }).annotations({ identifier: 'LSD.LoadDatabaseFileReq' }) {}
39
+ }) {}
31
40
 
32
- export class LoadDatabaseFileRes extends Schema.TaggedStruct('LSD.LoadDatabaseFileRes', {
33
- liveStoreVersion,
34
- requestId,
41
+ export class LoadDatabaseFileRes extends LSDReqResMessage('LSD.LoadDatabaseFileRes', {
35
42
  status: Schema.Literal('ok', 'unsupported-file', 'unsupported-database'),
36
- }).annotations({ identifier: 'LSD.LoadDatabaseFileRes' }) {}
43
+ }) {}
37
44
 
38
- export class DebugInfoReq extends Schema.TaggedStruct('LSD.DebugInfoReq', {
39
- liveStoreVersion,
40
- requestId,
41
- channelId,
42
- }).annotations({ identifier: 'LSD.DebugInfoReq' }) {}
45
+ export class DebugInfoReq extends LSDReqResMessage('LSD.DebugInfoReq', {}) {}
43
46
 
44
- export class DebugInfoRes extends Schema.TaggedStruct('LSD.DebugInfoRes', {
45
- liveStoreVersion,
46
- requestId,
47
+ export class DebugInfoRes extends LSDReqResMessage('LSD.DebugInfoRes', {
47
48
  debugInfo: DebugInfo,
48
- }).annotations({ identifier: 'LSD.DebugInfoRes' }) {}
49
+ }) {}
49
50
 
50
- export class DebugInfoHistorySubscribe extends Schema.TaggedStruct('LSD.DebugInfoHistorySubscribe', {
51
- liveStoreVersion,
52
- requestId,
53
- channelId,
54
- }).annotations({ identifier: 'LSD.DebugInfoHistorySubscribe' }) {}
51
+ export class DebugInfoHistorySubscribe extends LSDReqResMessage('LSD.DebugInfoHistorySubscribe', {}) {}
55
52
 
56
- export class DebugInfoHistoryRes extends Schema.TaggedStruct('LSD.DebugInfoHistoryRes', {
57
- liveStoreVersion,
58
- requestId,
53
+ export class DebugInfoHistoryRes extends LSDReqResMessage('LSD.DebugInfoHistoryRes', {
59
54
  debugInfoHistory: Schema.Array(DebugInfo),
60
- }).annotations({ identifier: 'LSD.DebugInfoHistoryRes' }) {}
61
-
62
- export class DebugInfoHistoryUnsubscribe extends Schema.TaggedStruct('LSD.DebugInfoHistoryUnsubscribe', {
63
- liveStoreVersion,
64
- requestId,
65
- channelId,
66
- }).annotations({ identifier: 'LSD.DebugInfoHistoryUnsubscribe' }) {}
67
-
68
- export class DebugInfoResetReq extends Schema.TaggedStruct('LSD.DebugInfoResetReq', {
69
- liveStoreVersion,
70
- requestId,
71
- channelId,
72
- }).annotations({ identifier: 'LSD.DebugInfoResetReq' }) {}
73
-
74
- export class DebugInfoResetRes extends Schema.TaggedStruct('LSD.DebugInfoResetRes', {
75
- liveStoreVersion,
76
- requestId,
77
- }).annotations({ identifier: 'LSD.DebugInfoResetRes' }) {}
78
-
79
- export class DebugInfoRerunQueryReq extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryReq', {
80
- liveStoreVersion,
81
- requestId,
82
- channelId,
55
+ }) {}
56
+
57
+ export class DebugInfoHistoryUnsubscribe extends LSDReqResMessage('LSD.DebugInfoHistoryUnsubscribe', {}) {}
58
+
59
+ export class DebugInfoResetReq extends LSDReqResMessage('LSD.DebugInfoResetReq', {}) {}
60
+
61
+ export class DebugInfoResetRes extends LSDReqResMessage('LSD.DebugInfoResetRes', {}) {}
62
+
63
+ export class DebugInfoRerunQueryReq extends LSDReqResMessage('LSD.DebugInfoRerunQueryReq', {
83
64
  queryStr: Schema.String,
84
65
  bindValues: Schema.UndefinedOr(PreparedBindValues),
85
66
  queriedTables: Schema.ReadonlySet(Schema.String),
86
- }).annotations({ identifier: 'LSD.DebugInfoRerunQueryReq' }) {}
67
+ }) {}
87
68
 
88
- export class DebugInfoRerunQueryRes extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryRes', {
89
- liveStoreVersion,
90
- requestId,
91
- }).annotations({ identifier: 'LSD.DebugInfoRerunQueryRes' }) {}
69
+ export class DebugInfoRerunQueryRes extends LSDReqResMessage('LSD.DebugInfoRerunQueryRes', {}) {}
92
70
 
93
- export class MutationBroadcast extends Schema.TaggedStruct('LSD.MutationBroadcast', {
94
- liveStoreVersion,
71
+ export class MutationBroadcast extends LSDMessage('LSD.MutationBroadcast', {
95
72
  mutationEventEncoded: mutationEventSchemaEncodedAny,
96
73
  persisted: Schema.Boolean,
97
- }).annotations({ identifier: 'LSD.MutationBroadcast' }) {}
74
+ }) {}
98
75
 
99
- export class RunMutationReq extends Schema.TaggedStruct('LSD.RunMutationReq', {
100
- liveStoreVersion,
101
- requestId,
102
- channelId,
76
+ export class RunMutationReq extends LSDReqResMessage('LSD.RunMutationReq', {
103
77
  mutationEventEncoded: mutationEventSchemaEncodedAny,
104
78
  persisted: Schema.Boolean,
105
- }).annotations({ identifier: 'LSD.RunMutationReq' }) {}
106
-
107
- export class RunMutationRes extends Schema.TaggedStruct('LSD.RunMutationRes', {
108
- liveStoreVersion,
109
- requestId,
110
- channelId,
111
- }).annotations({ identifier: 'LSD.RunMutationRes' }) {}
112
-
113
- export class MutationLogReq extends Schema.TaggedStruct('LSD.MutationLogReq', {
114
- liveStoreVersion,
115
- requestId,
116
- channelId,
117
- }).annotations({ identifier: 'LSD.MutationLogReq' }) {}
118
-
119
- export class MutationLogRes extends Schema.TaggedStruct('LSD.MutationLogRes', {
120
- liveStoreVersion,
121
- requestId,
122
- channelId,
79
+ }) {}
80
+
81
+ export class RunMutationRes extends LSDReqResMessage('LSD.RunMutationRes', {}) {}
82
+
83
+ export class MutationLogReq extends LSDReqResMessage('LSD.MutationLogReq', {}) {}
84
+
85
+ export class MutationLogRes extends LSDReqResMessage('LSD.MutationLogRes', {
123
86
  mutationLog: Transferable.Uint8Array,
124
- }).annotations({ identifier: 'LSD.MutationLogRes' }) {}
87
+ }) {}
125
88
 
126
- export class ReactivityGraphSubscribe extends Schema.TaggedStruct('LSD.ReactivityGraphSubscribe', {
127
- liveStoreVersion,
128
- requestId,
129
- channelId,
89
+ export class ReactivityGraphSubscribe extends LSDReqResMessage('LSD.ReactivityGraphSubscribe', {
130
90
  includeResults: Schema.Boolean,
131
- }).annotations({ identifier: 'LSD.ReactivityGraphSubscribe' }) {}
91
+ }) {}
132
92
 
133
- export class ReactivityGraphUnsubscribe extends Schema.TaggedStruct('LSD.ReactivityGraphUnsubscribe', {
134
- liveStoreVersion,
135
- requestId,
136
- channelId,
137
- }).annotations({ identifier: 'LSD.ReactivityGraphUnsubscribe' }) {}
93
+ export class ReactivityGraphUnsubscribe extends LSDReqResMessage('LSD.ReactivityGraphUnsubscribe', {}) {}
138
94
 
139
- export class ReactivityGraphRes extends Schema.TaggedStruct('LSD.ReactivityGraphRes', {
140
- liveStoreVersion,
141
- requestId,
95
+ export class ReactivityGraphRes extends LSDReqResMessage('LSD.ReactivityGraphRes', {
142
96
  reactivityGraph: Schema.Any,
143
- }).annotations({ identifier: 'LSD.ReactivityGraphRes' }) {}
97
+ }) {}
144
98
 
145
- export class LiveQueriesSubscribe extends Schema.TaggedStruct('LSD.LiveQueriesSubscribe', {
146
- liveStoreVersion,
147
- requestId,
148
- channelId,
149
- }).annotations({ identifier: 'LSD.LiveQueriesSubscribe' }) {}
99
+ export class LiveQueriesSubscribe extends LSDReqResMessage('LSD.LiveQueriesSubscribe', {}) {}
150
100
 
151
- export class LiveQueriesUnsubscribe extends Schema.TaggedStruct('LSD.LiveQueriesUnsubscribe', {
152
- liveStoreVersion,
153
- requestId,
154
- channelId,
155
- }).annotations({ identifier: 'LSD.LiveQueriesUnsubscribe' }) {}
101
+ export class LiveQueriesUnsubscribe extends LSDReqResMessage('LSD.LiveQueriesUnsubscribe', {}) {}
156
102
 
157
103
  export class SerializedLiveQuery extends Schema.Struct({
158
104
  _tag: Schema.Literal('js', 'sql', 'graphql'),
@@ -164,76 +110,59 @@ export class SerializedLiveQuery extends Schema.Struct({
164
110
  activeSubscriptions: Schema.Array(
165
111
  Schema.Struct({ frames: Schema.Array(Schema.Struct({ name: Schema.String, filePath: Schema.String })) }),
166
112
  ),
167
- }).annotations({ identifier: 'SerializedLiveQuery' }) {}
113
+ }) {}
168
114
 
169
- export class LiveQueriesRes extends Schema.TaggedStruct('LSD.LiveQueriesRes', {
170
- liveStoreVersion,
171
- requestId,
115
+ export class LiveQueriesRes extends LSDReqResMessage('LSD.LiveQueriesRes', {
172
116
  liveQueries: Schema.Array(SerializedLiveQuery),
173
- }).annotations({ identifier: 'LSD.LiveQueriesRes' }) {}
117
+ }) {}
174
118
 
175
- export class ResetAllDataReq extends Schema.TaggedStruct('LSD.ResetAllDataReq', {
176
- liveStoreVersion,
177
- requestId,
178
- channelId,
119
+ export class ResetAllDataReq extends LSDReqResMessage('LSD.ResetAllDataReq', {
179
120
  mode: Schema.Literal('all-data', 'only-app-db'),
180
- }).annotations({ identifier: 'LSD.ResetAllDataReq' }) {}
181
-
182
- export class ResetAllDataRes extends Schema.TaggedStruct('LSD.ResetAllDataRes', {
183
- liveStoreVersion,
184
- requestId,
185
- }).annotations({ identifier: 'LSD.ResetAllDataRes' }) {}
186
-
187
- export class MessagePortForStoreReq extends Schema.TaggedStruct('LSD.MessagePortForStoreReq', {
188
- liveStoreVersion,
189
- requestId,
190
- channelId,
191
- }).annotations({ identifier: 'LSD.MessagePortForStoreReq' }) {}
192
-
193
- export class MessagePortForStoreRes extends Schema.TaggedStruct('LSD.MessagePortForStoreRes', {
194
- liveStoreVersion,
195
- requestId,
196
- channelId,
121
+ }) {}
122
+
123
+ export class ResetAllDataRes extends LSDReqResMessage('LSD.ResetAllDataRes', {}) {}
124
+
125
+ export class DatabaseFileInfoReq extends LSDReqResMessage('LSD.DatabaseFileInfoReq', {}) {}
126
+
127
+ export class DatabaseFileInfoRes extends LSDReqResMessage('LSD.DatabaseFileInfoRes', {
128
+ dbFileSize: Schema.Number,
129
+ mutationLogFileSize: Schema.Number,
130
+ }) {}
131
+
132
+ export class MessagePortForStoreReq extends LSDReqResMessage('LSD.MessagePortForStoreReq', {}) {}
133
+
134
+ export class MessagePortForStoreRes extends LSDReqResMessage('LSD.MessagePortForStoreRes', {
197
135
  port: Transferable.MessagePort,
198
- }).annotations({ identifier: 'LSD.MessagePortForStoreRes' }) {}
136
+ }) {}
199
137
 
200
- export class NetworkStatusChanged extends Schema.TaggedStruct('LSD.NetworkStatusChanged', {
201
- liveStoreVersion,
202
- channelId,
138
+ export class NetworkStatusChanged extends LSDChannelMessage('LSD.NetworkStatusChanged', {
203
139
  networkStatus: NetworkStatus,
204
- }).annotations({ identifier: 'LSD.NetworkStatusChanged' }) {}
140
+ }) {}
141
+
142
+ export class SyncingInfoReq extends LSDReqResMessage('LSD.SyncingInfoReq', {}) {}
143
+
144
+ export class SyncingInfo extends Schema.Struct({
145
+ enabled: Schema.Boolean,
146
+ metadata: Schema.Record({ key: Schema.String, value: Schema.Any }),
147
+ }) {}
148
+
149
+ export class SyncingInfoRes extends LSDReqResMessage('LSD.SyncingInfoRes', {
150
+ syncingInfo: SyncingInfo,
151
+ }) {}
205
152
 
206
- export class DevtoolsReady extends Schema.TaggedStruct('LSD.DevtoolsReady', {
207
- liveStoreVersion,
208
- }).annotations({ identifier: 'LSD.DevtoolsReady' }) {}
153
+ export class DevtoolsReady extends LSDMessage('LSD.DevtoolsReady', {}) {}
209
154
 
210
- export class DevtoolsConnected extends Schema.TaggedStruct('LSD.DevtoolsConnected', {
211
- liveStoreVersion,
212
- channelId,
213
- }).annotations({ identifier: 'LSD.DevtoolsConnected' }) {}
155
+ export class DevtoolsConnected extends LSDChannelMessage('LSD.DevtoolsConnected', {}) {}
214
156
 
215
- export class AppHostReady extends Schema.TaggedStruct('LSD.AppHostReady', {
216
- liveStoreVersion,
217
- channelId,
157
+ export class AppHostReady extends LSDChannelMessage('LSD.AppHostReady', {
218
158
  isLeaderTab: Schema.Boolean,
219
- }).annotations({ identifier: 'LSD.AppHostReady' }) {}
159
+ }) {}
220
160
 
221
- export class Disconnect extends Schema.TaggedStruct('LSD.Disconnect', {
222
- liveStoreVersion,
223
- requestId,
224
- channelId,
225
- }).annotations({ identifier: 'LSD.Disconnect' }) {}
161
+ export class Disconnect extends LSDReqResMessage('LSD.Disconnect', {}) {}
226
162
 
227
- export class Ping extends Schema.TaggedStruct('LSD.Ping', {
228
- liveStoreVersion,
229
- requestId,
230
- channelId,
231
- }).annotations({ identifier: 'LSD.Ping' }) {}
163
+ export class Ping extends LSDReqResMessage('LSD.Ping', {}) {}
232
164
 
233
- export class Pong extends Schema.TaggedStruct('LSD.Pong', {
234
- liveStoreVersion,
235
- requestId,
236
- }).annotations({ identifier: 'LSD.Pong' }) {}
165
+ export class Pong extends LSDReqResMessage('LSD.Pong', {}) {}
237
166
 
238
167
  export const MessageToAppHostCoordinator = Schema.Union(
239
168
  SnapshotReq,
@@ -246,6 +175,8 @@ export const MessageToAppHostCoordinator = Schema.Union(
246
175
  DevtoolsConnected,
247
176
  RunMutationReq,
248
177
  Ping,
178
+ DatabaseFileInfoReq,
179
+ SyncingInfoReq,
249
180
  ).annotations({ identifier: 'LSD.MessageToAppHostCoordinator' })
250
181
 
251
182
  export type MessageToAppHostCoordinator = typeof MessageToAppHostCoordinator.Type
@@ -277,6 +208,8 @@ export const MessageFromAppHostCoordinator = Schema.Union(
277
208
  NetworkStatusChanged,
278
209
  RunMutationRes,
279
210
  Pong,
211
+ DatabaseFileInfoRes,
212
+ SyncingInfoRes,
280
213
  ).annotations({ identifier: 'LSD.MessageFromAppHostCoordinator' })
281
214
 
282
215
  export type MessageFromAppHostCoordinator = typeof MessageFromAppHostCoordinator.Type
@@ -1,2 +1,13 @@
1
+ import { BrowserChannel, Schema } from '@livestore/utils/effect'
2
+
1
3
  export * from './devtools-messages.js'
2
4
  export * from './devtools-window-message.js'
5
+
6
+ export namespace WebBridge {
7
+ export const makeBroadcastChannel = (key?: string) =>
8
+ BrowserChannel.broadcastChannel({
9
+ channelName: `livestore-web-bridge-devtools${key ? `-${key}` : ''}`,
10
+ listenSchema: Schema.Never,
11
+ sendSchema: Schema.Never,
12
+ })
13
+ }
@@ -1,7 +1,12 @@
1
1
  import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
2
2
  import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
3
3
 
4
- import { type InMemoryDatabase, type MigrationOptionsFromMutationLog, SqliteError } from './adapter-types.js'
4
+ import {
5
+ type InMemoryDatabase,
6
+ type MigrationOptionsFromMutationLog,
7
+ SqliteError,
8
+ UnexpectedError,
9
+ } from './adapter-types.js'
5
10
  import { getExecArgsFromMutation } from './mutation.js'
6
11
  import type { LiveStoreSchema, MutationDef, MutationLogMetaRow } from './schema/index.js'
7
12
  import { MUTATION_LOG_META_TABLE } from './schema/index.js'
@@ -35,23 +40,28 @@ export const rehydrateFromMutationLog = ({
35
40
  if (migrationOptions.excludeMutations?.has(row.mutation) === true) return
36
41
 
37
42
  if (hashMutation(mutationDef) !== row.schemaHash) {
38
- console.warn(`Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`)
43
+ yield* Effect.logWarning(
44
+ `Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`,
45
+ )
39
46
  }
40
47
 
41
- const argsDecodedEither = Schema.decodeUnknownEither(Schema.parseJson(mutationDef.schema))(row.argsJson)
42
- if (argsDecodedEither._tag === 'Left') {
43
- return shouldNeverHappen(`\
44
- There was an error decoding the persisted mutation event args for mutation "${row.mutation}".
48
+ const argsDecoded = yield* Schema.decodeUnknown(Schema.parseJson(mutationDef.schema))(row.argsJson).pipe(
49
+ Effect.mapError((cause) =>
50
+ UnexpectedError.make({
51
+ cause,
52
+ note: `\
53
+ There was an error during rehydrating from the mutation log while decoding
54
+ the persisted mutation event args for mutation "${row.mutation}".
45
55
  This likely means the schema has changed in an incompatible way.
46
-
47
- Error: ${argsDecodedEither.left}
48
- `)
49
- }
56
+ `,
57
+ }),
58
+ ),
59
+ )
50
60
 
51
61
  const mutationEventDecoded = {
52
62
  id: row.id,
53
63
  mutation: row.mutation,
54
- args: argsDecodedEither.right,
64
+ args: argsDecoded,
55
65
  }
56
66
  // const argsEncoded = JSON.parse(row.args_json)
57
67
  // const mutationSqlRes =