@livestore/common 0.0.55-dev.2 → 0.0.55

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.
@@ -135,7 +135,10 @@ export class MessagePortForStoreRes extends LSDReqResMessage('LSD.MessagePortFor
135
135
  port: Transferable.MessagePort,
136
136
  }) {}
137
137
 
138
- export class NetworkStatusChanged extends LSDChannelMessage('LSD.NetworkStatusChanged', {
138
+ export class NetworkStatusSubscribe extends LSDReqResMessage('LSD.NetworkStatusSubscribe', {}) {}
139
+ export class NetworkStatusUnsubscribe extends LSDReqResMessage('LSD.NetworkStatusUnsubscribe', {}) {}
140
+
141
+ export class NetworkStatusRes extends LSDReqResMessage('LSD.NetworkStatusRes', {
139
142
  networkStatus: NetworkStatus,
140
143
  }) {}
141
144
 
@@ -158,7 +161,7 @@ export class AppHostReady extends LSDChannelMessage('LSD.AppHostReady', {
158
161
  isLeaderTab: Schema.Boolean,
159
162
  }) {}
160
163
 
161
- export class Disconnect extends LSDReqResMessage('LSD.Disconnect', {}) {}
164
+ export class Disconnect extends LSDChannelMessage('LSD.Disconnect', {}) {}
162
165
 
163
166
  export class Ping extends LSDReqResMessage('LSD.Ping', {}) {}
164
167
 
@@ -170,6 +173,8 @@ export const MessageToAppHostCoordinator = Schema.Union(
170
173
  MutationLogReq,
171
174
  ResetAllDataReq,
172
175
  MessagePortForStoreRes,
176
+ NetworkStatusSubscribe,
177
+ NetworkStatusUnsubscribe,
173
178
  DevtoolsReady,
174
179
  Disconnect,
175
180
  DevtoolsConnected,
@@ -205,7 +210,7 @@ export const MessageFromAppHostCoordinator = Schema.Union(
205
210
  Disconnect,
206
211
  MutationBroadcast,
207
212
  AppHostReady,
208
- NetworkStatusChanged,
213
+ NetworkStatusRes,
209
214
  RunMutationRes,
210
215
  Pong,
211
216
  DatabaseFileInfoRes,
@@ -4,7 +4,7 @@ const channelId = Schema.String
4
4
 
5
5
  export namespace DevtoolsWindowMessage {
6
6
  /** Message is being created in contentscript-iframe, sent to contentscript and then sent to Store */
7
- export class MessagePortReady extends Schema.TaggedStruct('LSD.WindowMessage.MessagePortForStore', {
7
+ export class MessagePortReady extends Schema.TaggedStruct('LSD.WindowMessage.MessagePortReady', {
8
8
  port: Transferable.MessagePort,
9
9
  channelId,
10
10
  }) {}
@@ -1,13 +1,46 @@
1
+ import type { Effect, Scope } from '@livestore/utils/effect'
1
2
  import { BrowserChannel, Schema } from '@livestore/utils/effect'
2
3
 
3
4
  export * from './devtools-messages.js'
4
5
  export * from './devtools-window-message.js'
5
6
 
6
7
  export namespace WebBridge {
7
- export const makeBroadcastChannel = (key?: string) =>
8
+ export class AppHostReady extends Schema.TaggedStruct('LSD.WebBridge.AppHostReady', {
9
+ channelId: Schema.String,
10
+ isLeader: Schema.Boolean,
11
+ }) {}
12
+
13
+ export class DevtoolsReady extends Schema.TaggedStruct('LSD.WebBridge.DevtoolsReady', {
14
+ devtoolsId: Schema.String,
15
+ }) {}
16
+
17
+ export class ConnectToDevtools extends Schema.TaggedStruct('LSD.WebBridge.ConnectToDevtools', {
18
+ devtoolsId: Schema.String,
19
+ channelId: Schema.String,
20
+ /**
21
+ * Given the m:n relationship between devtools and app hosts and the fact that channelIds are usually
22
+ * sticky, we generate a new unique id for the lifetime of the web bridge.
23
+ */
24
+ webBridgeId: Schema.String,
25
+ isLeader: Schema.Boolean,
26
+ }) {}
27
+
28
+ export class AppHostWillDisconnect extends Schema.TaggedStruct('LSD.WebBridge.AppHostWillDisconnect', {
29
+ channelId: Schema.String,
30
+ }) {}
31
+
32
+ // export class DevtoolsWillDisconnect extends Schema.TaggedStruct('LSD.WebBridge.DevtoolsWillDisconnect', {
33
+ // channelId: Schema.String,
34
+ // }) {}
35
+
36
+ export class All extends Schema.Union(AppHostReady, DevtoolsReady, ConnectToDevtools, AppHostWillDisconnect) {}
37
+
38
+ export const makeBroadcastChannel = (
39
+ key?: string,
40
+ ): Effect.Effect<BrowserChannel.BrowserChannel<typeof All.Type, typeof All.Type>, never, Scope.Scope> =>
8
41
  BrowserChannel.broadcastChannel({
9
42
  channelName: `livestore-web-bridge-devtools${key ? `-${key}` : ''}`,
10
- listenSchema: Schema.Never,
11
- sendSchema: Schema.Never,
43
+ listenSchema: All,
44
+ sendSchema: All,
12
45
  })
13
46
  }
@@ -1,5 +1,4 @@
1
1
  import { isReadonlyArray, shouldNeverHappen } from '@livestore/utils'
2
- import type { ReadonlyArray } from '@livestore/utils/effect'
3
2
  import type { SqliteDsl } from 'effect-db-schema'
4
3
  import { SqliteAst } from 'effect-db-schema'
5
4
 
@@ -17,7 +16,6 @@ import { type TableDef, tableHasDerivedMutations } from './table-def.js'
17
16
 
18
17
  export * from './system-tables.js'
19
18
  export * as DbSchema from './table-def.js'
20
- export * as ParseUtils from './parse-utils.js'
21
19
  export * from './mutations.js'
22
20
  export * from './schema-helpers.js'
23
21
 
@@ -40,11 +38,20 @@ export type LiveStoreSchema<
40
38
  readonly hash: number
41
39
 
42
40
  migrationOptions: MigrationOptions
41
+
42
+ key: string
43
43
  }
44
44
 
45
45
  export type InputSchema = {
46
46
  readonly tables: Record<string, TableDef> | ReadonlyArray<TableDef>
47
47
  readonly mutations?: ReadonlyArray<MutationDef.Any> | Record<string, MutationDef.Any>
48
+ /**
49
+ * Can be used to isolate multiple LiveStore apps running in the same origin
50
+ *
51
+ * Make sure you also use this key in the `storage` options (e.g. directory, prefix etc) to make sure
52
+ * different instances of LiveStore aren't overlapping on the storage level.
53
+ */
54
+ readonly key?: string
48
55
  }
49
56
 
50
57
  export const makeSchema = <TInputSchema extends InputSchema>(
@@ -111,6 +118,7 @@ export const makeSchema = <TInputSchema extends InputSchema>(
111
118
  mutations,
112
119
  migrationOptions: inputSchema.migrations ?? { strategy: 'hard-reset' },
113
120
  hash,
121
+ key: inputSchema.key ?? '',
114
122
  } satisfies LiveStoreSchema
115
123
  }
116
124
 
@@ -1,43 +0,0 @@
1
- import { shouldNeverHappen } from '@livestore/utils'
2
- import type { ReadonlyArray } from '@livestore/utils/effect'
3
- import { pipe, ReadonlyRecord, Schema, TreeFormatter } from '@livestore/utils/effect'
4
- import { SqliteDsl as __SqliteDsl } from 'effect-db-schema'
5
-
6
- import { getDefaultValuesDecoded } from './schema-helpers.js'
7
- import { type FromColumns, type FromTable, type TableDef } from './table-def.js'
8
-
9
- export const many = <TTableDef extends TableDef>(
10
- table: TTableDef,
11
- ): ((rawRows: ReadonlyArray<any>) => ReadonlyArray<FromTable.RowDecoded<TTableDef>>) => {
12
- return Schema.decodeSync(Schema.Array(table.schema)) as TODO
13
- }
14
-
15
- export const first =
16
- <TTableDef extends TableDef>(
17
- table: TTableDef,
18
- fallback?: FromColumns.InsertRowDecoded<TTableDef['sqliteDef']['columns']>,
19
- ) =>
20
- (rawRows: ReadonlyArray<any>) => {
21
- const rows = Schema.decodeSync(Schema.Array(table.schema))(rawRows)
22
-
23
- if (rows.length === 0) {
24
- const schemaDefaultValues = getDefaultValuesDecoded(table)
25
-
26
- const defaultValuesResult = pipe(
27
- table.sqliteDef.columns,
28
- ReadonlyRecord.map((_column, columnName) => (fallback as any)?.[columnName] ?? schemaDefaultValues[columnName]),
29
- Schema.validateEither(table.schema),
30
- )
31
-
32
- if (defaultValuesResult._tag === 'Right') {
33
- return defaultValuesResult.right
34
- } else {
35
- console.error('decode error', TreeFormatter.formatErrorSync(defaultValuesResult.left))
36
- return shouldNeverHappen(
37
- `Expected query (for table ${table.sqliteDef.name}) to return at least one result but found none. Also can't fallback to default values as some were not provided.`,
38
- )
39
- }
40
- }
41
-
42
- return rows[0]!
43
- }