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

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.
@@ -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
  }
@@ -40,11 +40,20 @@ export type LiveStoreSchema<
40
40
  readonly hash: number
41
41
 
42
42
  migrationOptions: MigrationOptions
43
+
44
+ key: string
43
45
  }
44
46
 
45
47
  export type InputSchema = {
46
48
  readonly tables: Record<string, TableDef> | ReadonlyArray<TableDef>
47
49
  readonly mutations?: ReadonlyArray<MutationDef.Any> | Record<string, MutationDef.Any>
50
+ /**
51
+ * Can be used to isolate multiple LiveStore apps running in the same origin
52
+ *
53
+ * Make sure you also use this key in the `storage` options (e.g. directory, prefix etc) to make sure
54
+ * different instances of LiveStore aren't overlapping on the storage level.
55
+ */
56
+ readonly key?: string
48
57
  }
49
58
 
50
59
  export const makeSchema = <TInputSchema extends InputSchema>(
@@ -111,6 +120,7 @@ export const makeSchema = <TInputSchema extends InputSchema>(
111
120
  mutations,
112
121
  migrationOptions: inputSchema.migrations ?? { strategy: 'hard-reset' },
113
122
  hash,
123
+ key: inputSchema.key ?? '',
114
124
  } satisfies LiveStoreSchema
115
125
  }
116
126