@livestore/common 0.2.0 → 0.3.0-dev.1

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.
Files changed (243) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/__tests__/fixture.d.ts +163 -1
  3. package/dist/__tests__/fixture.d.ts.map +1 -1
  4. package/dist/__tests__/fixture.js +3 -1
  5. package/dist/__tests__/fixture.js.map +1 -1
  6. package/dist/adapter-types.d.ts +53 -38
  7. package/dist/adapter-types.d.ts.map +1 -1
  8. package/dist/adapter-types.js +5 -7
  9. package/dist/adapter-types.js.map +1 -1
  10. package/dist/bounded-collections.d.ts +2 -2
  11. package/dist/bounded-collections.d.ts.map +1 -1
  12. package/dist/debug-info.d.ts +13 -13
  13. package/dist/derived-mutations.d.ts +1 -1
  14. package/dist/derived-mutations.d.ts.map +1 -1
  15. package/dist/devtools/devtools-bridge.d.ts +2 -2
  16. package/dist/devtools/devtools-bridge.d.ts.map +1 -1
  17. package/dist/devtools/devtools-messages.d.ts +84 -196
  18. package/dist/devtools/devtools-messages.d.ts.map +1 -1
  19. package/dist/devtools/devtools-messages.js +55 -61
  20. package/dist/devtools/devtools-messages.js.map +1 -1
  21. package/dist/devtools/index.d.ts.map +1 -1
  22. package/dist/devtools/index.js +1 -2
  23. package/dist/devtools/index.js.map +1 -1
  24. package/dist/index.d.ts +2 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +2 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/init-singleton-tables.d.ts +1 -1
  29. package/dist/init-singleton-tables.d.ts.map +1 -1
  30. package/dist/leader-thread/apply-mutation.d.ts +8 -0
  31. package/dist/leader-thread/apply-mutation.d.ts.map +1 -0
  32. package/dist/leader-thread/apply-mutation.js +95 -0
  33. package/dist/leader-thread/apply-mutation.js.map +1 -0
  34. package/dist/leader-thread/connection.d.ts +11 -0
  35. package/dist/leader-thread/connection.d.ts.map +1 -0
  36. package/dist/leader-thread/connection.js +44 -0
  37. package/dist/leader-thread/connection.js.map +1 -0
  38. package/dist/leader-thread/leader-sync-processor.d.ts +47 -0
  39. package/dist/leader-thread/leader-sync-processor.d.ts.map +1 -0
  40. package/dist/leader-thread/leader-sync-processor.js +422 -0
  41. package/dist/leader-thread/leader-sync-processor.js.map +1 -0
  42. package/dist/leader-thread/leader-worker-devtools.d.ts +6 -0
  43. package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -0
  44. package/dist/leader-thread/leader-worker-devtools.js +216 -0
  45. package/dist/leader-thread/leader-worker-devtools.js.map +1 -0
  46. package/dist/leader-thread/make-leader-thread-layer.d.ts +20 -0
  47. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -0
  48. package/dist/leader-thread/make-leader-thread-layer.js +106 -0
  49. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -0
  50. package/dist/leader-thread/mod.d.ts +7 -0
  51. package/dist/leader-thread/mod.d.ts.map +1 -0
  52. package/dist/leader-thread/mod.js +7 -0
  53. package/dist/leader-thread/mod.js.map +1 -0
  54. package/dist/leader-thread/mutationlog.d.ts +23 -0
  55. package/dist/leader-thread/mutationlog.d.ts.map +1 -0
  56. package/dist/leader-thread/mutationlog.js +27 -0
  57. package/dist/leader-thread/mutationlog.js.map +1 -0
  58. package/dist/leader-thread/pull-queue-set.d.ts +7 -0
  59. package/dist/leader-thread/pull-queue-set.d.ts.map +1 -0
  60. package/dist/leader-thread/pull-queue-set.js +39 -0
  61. package/dist/leader-thread/pull-queue-set.js.map +1 -0
  62. package/dist/leader-thread/recreate-db.d.ts +7 -0
  63. package/dist/leader-thread/recreate-db.d.ts.map +1 -0
  64. package/dist/leader-thread/recreate-db.js +69 -0
  65. package/dist/leader-thread/recreate-db.js.map +1 -0
  66. package/dist/leader-thread/shutdown-channel.d.ts +15 -0
  67. package/dist/leader-thread/shutdown-channel.d.ts.map +1 -0
  68. package/dist/leader-thread/shutdown-channel.js +7 -0
  69. package/dist/leader-thread/shutdown-channel.js.map +1 -0
  70. package/dist/leader-thread/types.d.ts +87 -0
  71. package/dist/leader-thread/types.d.ts.map +1 -0
  72. package/dist/leader-thread/types.js +11 -0
  73. package/dist/leader-thread/types.js.map +1 -0
  74. package/dist/mutation.d.ts +3 -4
  75. package/dist/mutation.d.ts.map +1 -1
  76. package/dist/mutation.js +0 -14
  77. package/dist/mutation.js.map +1 -1
  78. package/dist/otel.d.ts +7 -0
  79. package/dist/otel.d.ts.map +1 -0
  80. package/dist/otel.js +11 -0
  81. package/dist/otel.js.map +1 -0
  82. package/dist/query-builder/api.d.ts +2 -2
  83. package/dist/query-builder/api.d.ts.map +1 -1
  84. package/dist/query-builder/api.js.map +1 -1
  85. package/dist/query-builder/impl.d.ts +1 -1
  86. package/dist/query-builder/impl.d.ts.map +1 -1
  87. package/dist/query-builder/impl.js +21 -4
  88. package/dist/query-builder/impl.js.map +1 -1
  89. package/dist/query-builder/impl.test.js +24 -1
  90. package/dist/query-builder/impl.test.js.map +1 -1
  91. package/dist/query-info.d.ts +1 -1
  92. package/dist/query-info.d.ts.map +1 -1
  93. package/dist/rehydrate-from-mutationlog.d.ts +1 -1
  94. package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
  95. package/dist/rehydrate-from-mutationlog.js +6 -6
  96. package/dist/rehydrate-from-mutationlog.js.map +1 -1
  97. package/dist/schema/EventId.d.ts +37 -0
  98. package/dist/schema/EventId.d.ts.map +1 -0
  99. package/dist/schema/EventId.js +30 -0
  100. package/dist/schema/EventId.js.map +1 -0
  101. package/dist/schema/MutationEvent.d.ts +191 -0
  102. package/dist/schema/MutationEvent.d.ts.map +1 -0
  103. package/dist/schema/MutationEvent.js +56 -0
  104. package/dist/schema/MutationEvent.js.map +1 -0
  105. package/dist/schema/mod.d.ts +8 -0
  106. package/dist/schema/mod.d.ts.map +1 -0
  107. package/dist/schema/mod.js +8 -0
  108. package/dist/schema/mod.js.map +1 -0
  109. package/dist/schema/mutations.d.ts +3 -123
  110. package/dist/schema/mutations.d.ts.map +1 -1
  111. package/dist/schema/mutations.js +0 -26
  112. package/dist/schema/mutations.js.map +1 -1
  113. package/dist/schema/{index.d.ts → schema.d.ts} +1 -5
  114. package/dist/schema/schema.d.ts.map +1 -0
  115. package/dist/schema/{index.js → schema.js} +1 -5
  116. package/dist/schema/schema.js.map +1 -0
  117. package/dist/schema/system-tables.d.ts +55 -29
  118. package/dist/schema/system-tables.d.ts.map +1 -1
  119. package/dist/schema/system-tables.js +10 -5
  120. package/dist/schema/system-tables.js.map +1 -1
  121. package/dist/schema-management/migrations.d.ts +1 -1
  122. package/dist/schema-management/migrations.d.ts.map +1 -1
  123. package/dist/schema-management/migrations.js +6 -1
  124. package/dist/schema-management/migrations.js.map +1 -1
  125. package/dist/schema-management/validate-mutation-defs.d.ts +1 -1
  126. package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
  127. package/dist/sync/client-session-sync-processor.d.ts +45 -0
  128. package/dist/sync/client-session-sync-processor.d.ts.map +1 -0
  129. package/dist/sync/client-session-sync-processor.js +131 -0
  130. package/dist/sync/client-session-sync-processor.js.map +1 -0
  131. package/dist/sync/index.d.ts +2 -0
  132. package/dist/sync/index.d.ts.map +1 -1
  133. package/dist/sync/index.js +2 -0
  134. package/dist/sync/index.js.map +1 -1
  135. package/dist/sync/next/compact-events.d.ts +1 -1
  136. package/dist/sync/next/compact-events.d.ts.map +1 -1
  137. package/dist/sync/next/compact-events.js +2 -1
  138. package/dist/sync/next/compact-events.js.map +1 -1
  139. package/dist/sync/next/facts.d.ts +5 -5
  140. package/dist/sync/next/facts.d.ts.map +1 -1
  141. package/dist/sync/next/facts.js +1 -1
  142. package/dist/sync/next/facts.js.map +1 -1
  143. package/dist/sync/next/history-dag-common.d.ts +30 -0
  144. package/dist/sync/next/history-dag-common.d.ts.map +1 -0
  145. package/dist/sync/next/history-dag-common.js +20 -0
  146. package/dist/sync/next/history-dag-common.js.map +1 -0
  147. package/dist/sync/next/history-dag.d.ts +4 -27
  148. package/dist/sync/next/history-dag.d.ts.map +1 -1
  149. package/dist/sync/next/history-dag.js +1 -19
  150. package/dist/sync/next/history-dag.js.map +1 -1
  151. package/dist/sync/next/mod.d.ts +1 -0
  152. package/dist/sync/next/mod.d.ts.map +1 -1
  153. package/dist/sync/next/mod.js +1 -0
  154. package/dist/sync/next/mod.js.map +1 -1
  155. package/dist/sync/next/rebase-events.d.ts +3 -2
  156. package/dist/sync/next/rebase-events.d.ts.map +1 -1
  157. package/dist/sync/next/rebase-events.js.map +1 -1
  158. package/dist/sync/next/test/compact-events.test.d.ts.map +1 -1
  159. package/dist/sync/next/test/compact-events.test.js +2 -1
  160. package/dist/sync/next/test/compact-events.test.js.map +1 -1
  161. package/dist/sync/next/test/mutation-fixtures.d.ts +1 -1
  162. package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
  163. package/dist/sync/next/test/mutation-fixtures.js +4 -3
  164. package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
  165. package/dist/sync/sync.d.ts +33 -12
  166. package/dist/sync/sync.d.ts.map +1 -1
  167. package/dist/sync/sync.js +10 -1
  168. package/dist/sync/sync.js.map +1 -1
  169. package/dist/sync/syncstate.d.ts +123 -0
  170. package/dist/sync/syncstate.d.ts.map +1 -0
  171. package/dist/sync/syncstate.js +248 -0
  172. package/dist/sync/syncstate.js.map +1 -0
  173. package/dist/sync/syncstate.test.d.ts +2 -0
  174. package/dist/sync/syncstate.test.d.ts.map +1 -0
  175. package/dist/sync/syncstate.test.js +399 -0
  176. package/dist/sync/syncstate.test.js.map +1 -0
  177. package/dist/sync/validate-push-payload.d.ts +5 -0
  178. package/dist/sync/validate-push-payload.d.ts.map +1 -0
  179. package/dist/sync/validate-push-payload.js +15 -0
  180. package/dist/sync/validate-push-payload.js.map +1 -0
  181. package/dist/util.d.ts +2 -2
  182. package/dist/util.d.ts.map +1 -1
  183. package/dist/version.d.ts +2 -2
  184. package/dist/version.d.ts.map +1 -1
  185. package/dist/version.js +2 -2
  186. package/package.json +13 -6
  187. package/src/__tests__/fixture.ts +5 -1
  188. package/src/adapter-types.ts +60 -34
  189. package/src/derived-mutations.test.ts +1 -1
  190. package/src/derived-mutations.ts +1 -1
  191. package/src/devtools/devtools-bridge.ts +2 -2
  192. package/src/devtools/devtools-messages.ts +70 -74
  193. package/src/devtools/index.ts +1 -2
  194. package/src/index.ts +2 -1
  195. package/src/init-singleton-tables.ts +1 -1
  196. package/src/leader-thread/apply-mutation.ts +143 -0
  197. package/src/leader-thread/connection.ts +67 -0
  198. package/src/leader-thread/leader-sync-processor.ts +666 -0
  199. package/src/leader-thread/leader-worker-devtools.ts +358 -0
  200. package/src/leader-thread/make-leader-thread-layer.ts +192 -0
  201. package/src/leader-thread/mod.ts +6 -0
  202. package/src/leader-thread/mutationlog.ts +42 -0
  203. package/src/leader-thread/pull-queue-set.ts +58 -0
  204. package/src/leader-thread/recreate-db.ts +109 -0
  205. package/src/leader-thread/shutdown-channel.ts +13 -0
  206. package/src/leader-thread/types.ts +129 -0
  207. package/src/mutation.ts +3 -21
  208. package/src/otel.ts +20 -0
  209. package/src/query-builder/api.ts +3 -2
  210. package/src/query-builder/impl.test.ts +28 -1
  211. package/src/query-builder/impl.ts +21 -5
  212. package/src/query-info.ts +1 -1
  213. package/src/rehydrate-from-mutationlog.ts +7 -11
  214. package/src/schema/EventId.ts +46 -0
  215. package/src/schema/MutationEvent.ts +161 -0
  216. package/src/schema/mod.ts +7 -0
  217. package/src/schema/mutations.ts +5 -126
  218. package/src/schema/{index.ts → schema.ts} +0 -5
  219. package/src/schema/system-tables.ts +18 -5
  220. package/src/schema-management/migrations.ts +9 -2
  221. package/src/schema-management/validate-mutation-defs.ts +1 -1
  222. package/src/sync/client-session-sync-processor.ts +207 -0
  223. package/src/sync/index.ts +2 -0
  224. package/src/sync/next/compact-events.ts +3 -2
  225. package/src/sync/next/facts.ts +11 -5
  226. package/src/sync/next/history-dag-common.ts +44 -0
  227. package/src/sync/next/history-dag.ts +3 -45
  228. package/src/sync/next/mod.ts +1 -0
  229. package/src/sync/next/rebase-events.ts +6 -5
  230. package/src/sync/next/test/compact-events.test.ts +3 -2
  231. package/src/sync/next/test/mutation-fixtures.ts +7 -6
  232. package/src/sync/sync.ts +32 -12
  233. package/src/sync/syncstate.test.ts +464 -0
  234. package/src/sync/syncstate.ts +385 -0
  235. package/src/sync/validate-push-payload.ts +18 -0
  236. package/src/version.ts +2 -2
  237. package/dist/schema/index.d.ts.map +0 -1
  238. package/dist/schema/index.js.map +0 -1
  239. package/dist/sync/next-mutation-event-id-pair.d.ts +0 -14
  240. package/dist/sync/next-mutation-event-id-pair.d.ts.map +0 -1
  241. package/dist/sync/next-mutation-event-id-pair.js +0 -13
  242. package/dist/sync/next-mutation-event-id-pair.js.map +0 -1
  243. package/src/sync/next-mutation-event-id-pair.ts +0 -20
@@ -2,7 +2,10 @@ import type { Cause, Queue, Scope, SubscriptionRef, WebChannel } from '@livestor
2
2
  import { Effect, Schema, Stream } from '@livestore/utils/effect'
3
3
 
4
4
  import type * as Devtools from './devtools/index.js'
5
- import type { LiveStoreSchema, MutationEvent } from './schema/index.js'
5
+ import type { EventId } from './schema/EventId.js'
6
+ import type { LiveStoreSchema, MutationEvent } from './schema/mod.js'
7
+ import type { InvalidPushError } from './sync/sync.js'
8
+ import type { PayloadUpstream, SyncState } from './sync/syncstate.js'
6
9
  import type { PreparedBindValues } from './util.js'
7
10
 
8
11
  export interface PreparedStatement {
@@ -12,6 +15,17 @@ export interface PreparedStatement {
12
15
  sql: string
13
16
  }
14
17
 
18
+ export type SynchronousDatabaseSession = {
19
+ changeset: () => Uint8Array | undefined
20
+ finish: () => void
21
+ }
22
+
23
+ export type SynchronousDatabaseChangeset = {
24
+ // TODO combining changesets (requires changes in the SQLite WASM binding)
25
+ invert: () => SynchronousDatabaseChangeset
26
+ apply: () => void
27
+ }
28
+
15
29
  export type ClientSession = {
16
30
  /** SQLite database with synchronous API running in the same thread (usually in-memory) */
17
31
  syncDb: SynchronousDatabase
@@ -19,8 +33,9 @@ export type ClientSession = {
19
33
  coordinator: Coordinator
20
34
  }
21
35
 
22
- export type SynchronousDatabase = {
36
+ export type SynchronousDatabase<TReq = any, TMetadata extends TReq = TReq> = {
23
37
  _tag: 'SynchronousDatabase'
38
+ metadata: TMetadata
24
39
  prepare(queryStr: string): PreparedStatement
25
40
  execute(
26
41
  queryStr: string,
@@ -29,9 +44,38 @@ export type SynchronousDatabase = {
29
44
  ): void
30
45
  select<T>(queryStr: string, bindValues?: PreparedBindValues | undefined): ReadonlyArray<T>
31
46
  export(): Uint8Array
47
+ import: (data: Uint8Array | SynchronousDatabase<TReq>) => void
32
48
  close(): void
49
+ destroy(): void
50
+ session(): SynchronousDatabaseSession
51
+ makeChangeset: (data: Uint8Array) => SynchronousDatabaseChangeset
33
52
  }
34
53
 
54
+ export type MakeSynchronousDatabase<
55
+ TReq = { dbPointer: number; persistenceInfo: PersistenceInfo },
56
+ TInput_ extends { _tag: string } = { _tag: string },
57
+ TMetadata_ extends TReq = TReq,
58
+ R = never,
59
+ > = <
60
+ TInput extends TInput_,
61
+ TMetadata extends TMetadata_ & { _tag: TInput['_tag'] } = TMetadata_ & { _tag: TInput['_tag'] },
62
+ >(
63
+ input: TInput,
64
+ ) => Effect.Effect<
65
+ SynchronousDatabase<TReq, Extract<TMetadata, { _tag: TInput['_tag'] }>>,
66
+ SqliteError | UnexpectedError,
67
+ R
68
+ >
69
+
70
+ export const PersistenceInfo = Schema.Struct(
71
+ {
72
+ fileName: Schema.String,
73
+ },
74
+ { key: Schema.String, value: Schema.Any },
75
+ ).annotations({ title: 'LiveStore.PersistenceInfo' })
76
+
77
+ export type PersistenceInfo<With extends {} = {}> = typeof PersistenceInfo.Type & With
78
+
35
79
  export type ResetMode = 'all-data' | 'only-app-db'
36
80
 
37
81
  export const NetworkStatus = Schema.Struct({
@@ -55,10 +99,11 @@ export const BootStatus = Schema.Union(
55
99
  Schema.Struct({ stage: Schema.Literal('rehydrating'), progress: BootStateProgress }),
56
100
  Schema.Struct({ stage: Schema.Literal('syncing'), progress: BootStateProgress }),
57
101
  Schema.Struct({ stage: Schema.Literal('done') }),
58
- )
102
+ ).annotations({ title: 'BootStatus' })
59
103
 
60
104
  export type BootStatus = typeof BootStatus.Type
61
105
 
106
+ // TODO refactor `Coordinator` to embrace more of the "leader semantics"
62
107
  export type Coordinator = {
63
108
  devtools: {
64
109
  enabled: boolean
@@ -68,19 +113,16 @@ export type Coordinator = {
68
113
  sessionId: string
69
114
  // TODO is exposing the lock status really needed (or only relevant for web adapter?)
70
115
  lockStatus: SubscriptionRef.SubscriptionRef<LockStatus>
71
- syncMutations: Stream.Stream<MutationEvent.Any, UnexpectedError>
72
- execute(queryStr: string, bindValues: PreparedBindValues | undefined): Effect.Effect<void, UnexpectedError>
73
- mutate(
74
- mutationEventEncoded: MutationEvent.AnyEncoded,
75
- options: { persisted: boolean },
76
- ): Effect.Effect<void, UnexpectedError>
77
- /** Can be called synchronously */
78
- nextMutationEventIdPair: (opts: { localOnly: boolean }) => Effect.Effect<EventIdPair, UnexpectedError>
79
- /** Used to initially get the current mutation event id to use as `parentId` for the next mutation event */
80
- getCurrentMutationEventId: Effect.Effect<EventId, UnexpectedError>
81
- export: Effect.Effect<Uint8Array | undefined, UnexpectedError>
116
+ mutations: {
117
+ pull: Stream.Stream<{ payload: PayloadUpstream; remaining: number }, UnexpectedError>
118
+ push(batch: ReadonlyArray<MutationEvent.AnyEncoded>): Effect.Effect<void, UnexpectedError | InvalidPushError>
119
+ initialMutationEventId: EventId
120
+ }
121
+ export: Effect.Effect<Uint8Array, UnexpectedError>
82
122
  getMutationLogData: Effect.Effect<Uint8Array, UnexpectedError>
123
+ getLeaderSyncState: Effect.Effect<SyncState, UnexpectedError>
83
124
  networkStatus: SubscriptionRef.SubscriptionRef<NetworkStatus>
125
+ shutdown: (cause: Cause.Cause<UnexpectedError | IntentionalShutdownCause>) => Effect.Effect<void>
84
126
  }
85
127
 
86
128
  /**
@@ -97,23 +139,6 @@ export type SessionIdSymbol = typeof SessionIdSymbol
97
139
 
98
140
  export type LockStatus = 'has-lock' | 'no-lock'
99
141
 
100
- /**
101
- * LiveStore event id value consisting of a globally unique event sequence number
102
- * and a local sequence number.
103
- *
104
- * The local sequence number is only used for localOnly mutations and starts from 0 for each global sequence number.
105
- */
106
- export type EventId = { global: number; local: number }
107
-
108
- export const EventId = Schema.Struct({
109
- global: Schema.Number,
110
- local: Schema.Number,
111
- }).annotations({ title: 'LiveStore.EventId' })
112
-
113
- export type EventIdPair = { id: EventId; parentId: EventId }
114
-
115
- export const ROOT_ID = { global: -1, local: 0 } satisfies EventId
116
-
117
142
  export class UnexpectedError extends Schema.TaggedError<UnexpectedError>()('LiveStore.UnexpectedError', {
118
143
  cause: Schema.Defect,
119
144
  note: Schema.optional(Schema.String),
@@ -134,7 +159,7 @@ export class UnexpectedError extends Schema.TaggedError<UnexpectedError>()('Live
134
159
  export class IntentionalShutdownCause extends Schema.TaggedError<IntentionalShutdownCause>()(
135
160
  'LiveStore.IntentionalShutdownCause',
136
161
  {
137
- reason: Schema.Literal('devtools-reset', 'devtools-import'),
162
+ reason: Schema.Literal('devtools-reset', 'devtools-import', 'manual'),
138
163
  },
139
164
  ) {}
140
165
 
@@ -190,8 +215,8 @@ export type MigrationOptionsFromMutationLog<TSchema extends LiveStoreSchema = Li
190
215
  }
191
216
 
192
217
  export type StoreDevtoolsChannel = WebChannel.WebChannel<
193
- Devtools.MessageToAppHostStore,
194
- Devtools.MessageFromAppHostStore
218
+ Devtools.MessageToAppClientSession,
219
+ Devtools.MessageFromAppClientSession
195
220
  >
196
221
 
197
222
  export type ConnectDevtoolsToStore = (
@@ -202,6 +227,7 @@ export type Adapter = (opts: {
202
227
  schema: LiveStoreSchema
203
228
  storeId: string
204
229
  devtoolsEnabled: boolean
230
+ debugInstanceId: string
205
231
  bootStatusQueue: Queue.Queue<BootStatus>
206
232
  shutdown: (cause: Cause.Cause<any>) => Effect.Effect<void>
207
233
  connectDevtoolsToStore: ConnectDevtoolsToStore
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, test } from 'vitest'
2
2
 
3
3
  import { appConfig, todos } from './__tests__/fixture.js'
4
- import type { MutationEvent } from './schema/mutations.js'
4
+ import type * as MutationEvent from './schema/MutationEvent.js'
5
5
 
6
6
  describe('derived mutations', () => {
7
7
  test('todos', () => {
@@ -2,7 +2,7 @@ import type { SqliteDsl } from '@livestore/db-schema'
2
2
  import type { GetValForKey } from '@livestore/utils'
3
3
  import { ReadonlyRecord, Schema } from '@livestore/utils/effect'
4
4
 
5
- import type { MutationEvent } from './schema/mutations.js'
5
+ import type * as MutationEvent from './schema/MutationEvent.js'
6
6
  import { defineMutation } from './schema/mutations.js'
7
7
  import { getDefaultValuesDecoded } from './schema/schema-helpers.js'
8
8
  import type * as DbSchema from './schema/table-def.js'
@@ -4,8 +4,8 @@ import type * as Devtools from './devtools-messages.js'
4
4
 
5
5
  export type PrepareDevtoolsBridge = {
6
6
  /** Messages coming from the app host (usually responses to requests) */
7
- responsePubSub: PubSub.PubSub<Devtools.MessageFromAppHostCoordinator | Devtools.MessageFromAppHostStore>
8
- sendToAppHost: (msg: Devtools.MessageToAppHostCoordinator | Devtools.MessageToAppHostStore) => Effect.Effect<void>
7
+ responsePubSub: PubSub.PubSub<Devtools.MessageFromAppLeader | Devtools.MessageFromAppClientSession>
8
+ sendToAppHost: (msg: Devtools.MessageToAppLeader | Devtools.MessageToAppClientSession) => Effect.Effect<void>
9
9
  appHostId: string
10
10
  copyToClipboard: (text: string) => Effect.Effect<void>
11
11
  sendEscapeKey?: Effect.Effect<void>
@@ -2,7 +2,7 @@ import { Schema, Transferable } from '@livestore/utils/effect'
2
2
 
3
3
  import { NetworkStatus } from '../adapter-types.js'
4
4
  import { DebugInfo } from '../debug-info.js'
5
- import { mutationEventSchemaEncodedAny } from '../schema/mutations.js'
5
+ import * as MutationEvent from '../schema/MutationEvent.js'
6
6
  import { PreparedBindValues } from '../util.js'
7
7
  import { liveStoreVersion as pkgVersion } from '../version.js'
8
8
 
@@ -17,8 +17,20 @@ const LSDMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag
17
17
  }).annotations({ identifier: tag })
18
18
 
19
19
  const LSDChannelMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
20
+ LSDMessage(tag, {
21
+ ...fields,
22
+ })
23
+
24
+ const LSDStoreChannelMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
25
+ LSDMessage(tag, {
26
+ appHostId,
27
+ ...fields,
28
+ })
29
+
30
+ const LSDStoreReqResMessage = <Tag extends string, Fields extends Schema.Struct.Fields>(tag: Tag, fields: Fields) =>
20
31
  LSDMessage(tag, {
21
32
  appHostId,
33
+ requestId,
22
34
  ...fields,
23
35
  })
24
36
 
@@ -28,77 +40,77 @@ const LSDReqResMessage = <Tag extends string, Fields extends Schema.Struct.Field
28
40
  ...fields,
29
41
  })
30
42
 
31
- export class SnapshotReq extends LSDReqResMessage('LSD.SnapshotReq', {}) {}
43
+ export class SnapshotReq extends LSDReqResMessage('LSD.Leader.SnapshotReq', {}) {}
32
44
 
33
- export class SnapshotRes extends LSDReqResMessage('LSD.SnapshotRes', {
45
+ export class SnapshotRes extends LSDReqResMessage('LSD.Leader.SnapshotRes', {
34
46
  snapshot: Transferable.Uint8Array,
35
47
  }) {}
36
48
 
37
- export class LoadDatabaseFileReq extends LSDReqResMessage('LSD.LoadDatabaseFileReq', {
49
+ export class LoadDatabaseFileReq extends LSDReqResMessage('LSD.Leader.LoadDatabaseFileReq', {
38
50
  data: Transferable.Uint8Array,
39
51
  }) {}
40
52
 
41
- export class LoadDatabaseFileRes extends LSDReqResMessage('LSD.LoadDatabaseFileRes', {
53
+ export class LoadDatabaseFileRes extends LSDReqResMessage('LSD.Leader.LoadDatabaseFileRes', {
42
54
  status: Schema.Literal('ok', 'unsupported-file', 'unsupported-database'),
43
55
  }) {}
44
56
 
45
- export class DebugInfoReq extends LSDReqResMessage('LSD.DebugInfoReq', {}) {}
57
+ export class DebugInfoReq extends LSDStoreReqResMessage('LSD.DebugInfoReq', {}) {}
46
58
 
47
- export class DebugInfoRes extends LSDReqResMessage('LSD.DebugInfoRes', {
59
+ export class DebugInfoRes extends LSDStoreReqResMessage('LSD.DebugInfoRes', {
48
60
  debugInfo: DebugInfo,
49
61
  }) {}
50
62
 
51
- export class DebugInfoHistorySubscribe extends LSDReqResMessage('LSD.DebugInfoHistorySubscribe', {}) {}
63
+ export class DebugInfoHistorySubscribe extends LSDStoreReqResMessage('LSD.DebugInfoHistorySubscribe', {}) {}
52
64
 
53
- export class DebugInfoHistoryRes extends LSDReqResMessage('LSD.DebugInfoHistoryRes', {
65
+ export class DebugInfoHistoryRes extends LSDStoreReqResMessage('LSD.DebugInfoHistoryRes', {
54
66
  debugInfoHistory: Schema.Array(DebugInfo),
55
67
  }) {}
56
68
 
57
- export class DebugInfoHistoryUnsubscribe extends LSDReqResMessage('LSD.DebugInfoHistoryUnsubscribe', {}) {}
69
+ export class DebugInfoHistoryUnsubscribe extends LSDStoreReqResMessage('LSD.DebugInfoHistoryUnsubscribe', {}) {}
58
70
 
59
- export class DebugInfoResetReq extends LSDReqResMessage('LSD.DebugInfoResetReq', {}) {}
71
+ export class DebugInfoResetReq extends LSDStoreReqResMessage('LSD.DebugInfoResetReq', {}) {}
60
72
 
61
- export class DebugInfoResetRes extends LSDReqResMessage('LSD.DebugInfoResetRes', {}) {}
73
+ export class DebugInfoResetRes extends LSDStoreReqResMessage('LSD.DebugInfoResetRes', {}) {}
62
74
 
63
- export class DebugInfoRerunQueryReq extends LSDReqResMessage('LSD.DebugInfoRerunQueryReq', {
75
+ export class DebugInfoRerunQueryReq extends LSDStoreReqResMessage('LSD.DebugInfoRerunQueryReq', {
64
76
  queryStr: Schema.String,
65
77
  bindValues: Schema.UndefinedOr(PreparedBindValues),
66
78
  queriedTables: Schema.ReadonlySet(Schema.String),
67
79
  }) {}
68
80
 
69
- export class DebugInfoRerunQueryRes extends LSDReqResMessage('LSD.DebugInfoRerunQueryRes', {}) {}
81
+ export class DebugInfoRerunQueryRes extends LSDStoreReqResMessage('LSD.DebugInfoRerunQueryRes', {}) {}
70
82
 
71
- export class MutationBroadcast extends LSDMessage('LSD.MutationBroadcast', {
72
- mutationEventEncoded: mutationEventSchemaEncodedAny,
73
- persisted: Schema.Boolean,
83
+ // TODO refactor this to use push/pull semantics
84
+ export class MutationBroadcast extends LSDMessage('LSD.Leader.MutationBroadcast', {
85
+ mutationEventEncoded: MutationEvent.EncodedAny,
74
86
  }) {}
75
87
 
76
- export class RunMutationReq extends LSDReqResMessage('LSD.RunMutationReq', {
77
- mutationEventEncoded: mutationEventSchemaEncodedAny.pipe(Schema.omit('id', 'parentId')),
78
- persisted: Schema.Boolean,
88
+ // TODO refactor this to use push/pull semantics
89
+ export class RunMutationReq extends LSDReqResMessage('LSD.Leader.RunMutationReq', {
90
+ mutationEventEncoded: MutationEvent.EncodedAny.pipe(Schema.omit('id', 'parentId')),
79
91
  }) {}
80
92
 
81
- export class RunMutationRes extends LSDReqResMessage('LSD.RunMutationRes', {}) {}
93
+ export class RunMutationRes extends LSDReqResMessage('LSD.Leader.RunMutationRes', {}) {}
82
94
 
83
- export class MutationLogReq extends LSDReqResMessage('LSD.MutationLogReq', {}) {}
95
+ export class MutationLogReq extends LSDReqResMessage('LSD.Leader.MutationLogReq', {}) {}
84
96
 
85
- export class MutationLogRes extends LSDReqResMessage('LSD.MutationLogRes', {
97
+ export class MutationLogRes extends LSDReqResMessage('LSD.Leader.MutationLogRes', {
86
98
  mutationLog: Transferable.Uint8Array,
87
99
  }) {}
88
100
 
89
- export class ReactivityGraphSubscribe extends LSDReqResMessage('LSD.ReactivityGraphSubscribe', {
101
+ export class ReactivityGraphSubscribe extends LSDStoreReqResMessage('LSD.ReactivityGraphSubscribe', {
90
102
  includeResults: Schema.Boolean,
91
103
  }) {}
92
104
 
93
- export class ReactivityGraphUnsubscribe extends LSDReqResMessage('LSD.ReactivityGraphUnsubscribe', {}) {}
105
+ export class ReactivityGraphUnsubscribe extends LSDStoreReqResMessage('LSD.ReactivityGraphUnsubscribe', {}) {}
94
106
 
95
- export class ReactivityGraphRes extends LSDReqResMessage('LSD.ReactivityGraphRes', {
107
+ export class ReactivityGraphRes extends LSDStoreReqResMessage('LSD.ReactivityGraphRes', {
96
108
  reactivityGraph: Schema.Any,
97
109
  }) {}
98
110
 
99
- export class LiveQueriesSubscribe extends LSDReqResMessage('LSD.LiveQueriesSubscribe', {}) {}
111
+ export class LiveQueriesSubscribe extends LSDStoreReqResMessage('LSD.LiveQueriesSubscribe', {}) {}
100
112
 
101
- export class LiveQueriesUnsubscribe extends LSDReqResMessage('LSD.LiveQueriesUnsubscribe', {}) {}
113
+ export class LiveQueriesUnsubscribe extends LSDStoreReqResMessage('LSD.LiveQueriesUnsubscribe', {}) {}
102
114
 
103
115
  export class SerializedLiveQuery extends Schema.Struct({
104
116
  _tag: Schema.Literal('computed', 'db', 'graphql'),
@@ -112,95 +124,78 @@ export class SerializedLiveQuery extends Schema.Struct({
112
124
  ),
113
125
  }) {}
114
126
 
115
- export class LiveQueriesRes extends LSDReqResMessage('LSD.LiveQueriesRes', {
127
+ export class LiveQueriesRes extends LSDStoreReqResMessage('LSD.LiveQueriesRes', {
116
128
  liveQueries: Schema.Array(SerializedLiveQuery),
117
129
  }) {}
118
130
 
119
- export class ResetAllDataReq extends LSDReqResMessage('LSD.ResetAllDataReq', {
131
+ export class ResetAllDataReq extends LSDReqResMessage('LSD.Leader.ResetAllDataReq', {
120
132
  mode: Schema.Literal('all-data', 'only-app-db'),
121
133
  }) {}
122
134
 
123
- export class ResetAllDataRes extends LSDReqResMessage('LSD.ResetAllDataRes', {}) {}
135
+ export class ResetAllDataRes extends LSDReqResMessage('LSD.Leader.ResetAllDataRes', {}) {}
124
136
 
125
- export class DatabaseFileInfoReq extends LSDReqResMessage('LSD.DatabaseFileInfoReq', {}) {}
137
+ export class DatabaseFileInfoReq extends LSDReqResMessage('LSD.Leader.DatabaseFileInfoReq', {}) {}
126
138
 
127
139
  export class DatabaseFileInfo extends Schema.Struct({
128
140
  fileSize: Schema.Number,
129
141
  persistenceInfo: Schema.Struct({ fileName: Schema.String }, { key: Schema.String, value: Schema.Any }),
130
142
  }) {}
131
143
 
132
- export class DatabaseFileInfoRes extends LSDReqResMessage('LSD.DatabaseFileInfoRes', {
144
+ export class DatabaseFileInfoRes extends LSDReqResMessage('LSD.Leader.DatabaseFileInfoRes', {
133
145
  db: DatabaseFileInfo,
134
146
  mutationLog: DatabaseFileInfo,
135
147
  }) {}
136
148
 
137
- export class MessagePortForStoreReq extends LSDReqResMessage('LSD.MessagePortForStoreReq', {}) {}
149
+ export class NetworkStatusSubscribe extends LSDReqResMessage('LSD.Leader.NetworkStatusSubscribe', {}) {}
150
+ export class NetworkStatusUnsubscribe extends LSDReqResMessage('LSD.Leader.NetworkStatusUnsubscribe', {}) {}
138
151
 
139
- export class MessagePortForStoreRes extends LSDReqResMessage('LSD.MessagePortForStoreRes', {
140
- port: Transferable.MessagePort,
141
- }) {}
142
-
143
- export class NetworkStatusSubscribe extends LSDReqResMessage('LSD.NetworkStatusSubscribe', {}) {}
144
- export class NetworkStatusUnsubscribe extends LSDReqResMessage('LSD.NetworkStatusUnsubscribe', {}) {}
145
-
146
- export class NetworkStatusRes extends LSDReqResMessage('LSD.NetworkStatusRes', {
152
+ export class NetworkStatusRes extends LSDReqResMessage('LSD.Leader.NetworkStatusRes', {
147
153
  networkStatus: NetworkStatus,
148
154
  }) {}
149
155
 
150
- export class SyncingInfoReq extends LSDReqResMessage('LSD.SyncingInfoReq', {}) {}
156
+ export class SyncingInfoReq extends LSDReqResMessage('LSD.Leader.SyncingInfoReq', {}) {}
151
157
 
152
158
  export class SyncingInfo extends Schema.Struct({
153
159
  enabled: Schema.Boolean,
154
160
  metadata: Schema.Record({ key: Schema.String, value: Schema.Any }),
155
161
  }) {}
156
162
 
157
- export class SyncingInfoRes extends LSDReqResMessage('LSD.SyncingInfoRes', {
163
+ export class SyncingInfoRes extends LSDReqResMessage('LSD.Leader.SyncingInfoRes', {
158
164
  syncingInfo: SyncingInfo,
159
165
  }) {}
160
166
 
161
- export class SyncHistorySubscribe extends LSDReqResMessage('LSD.SyncHistorySubscribe', {}) {}
162
- export class SyncHistoryUnsubscribe extends LSDReqResMessage('LSD.SyncHistoryUnsubscribe', {}) {}
163
- export class SyncHistoryRes extends LSDReqResMessage('LSD.SyncHistoryRes', {
164
- mutationEventEncoded: mutationEventSchemaEncodedAny,
167
+ export class SyncHistorySubscribe extends LSDReqResMessage('LSD.Leader.SyncHistorySubscribe', {}) {}
168
+ export class SyncHistoryUnsubscribe extends LSDReqResMessage('LSD.Leader.SyncHistoryUnsubscribe', {}) {}
169
+ export class SyncHistoryRes extends LSDReqResMessage('LSD.Leader.SyncHistoryRes', {
170
+ mutationEventEncoded: MutationEvent.EncodedAny,
165
171
  metadata: Schema.Option(Schema.JsonValue),
166
172
  }) {}
167
173
 
168
- export class DevtoolsReady extends LSDMessage('LSD.DevtoolsReady', {}) {}
169
-
170
- export class DevtoolsConnected extends LSDChannelMessage('LSD.DevtoolsConnected', {}) {}
171
-
172
- export class AppHostReady extends LSDChannelMessage('LSD.AppHostReady', {
173
- isLeader: Schema.Boolean,
174
- }) {}
175
-
176
- export class Disconnect extends LSDChannelMessage('LSD.Disconnect', {}) {}
174
+ export class Disconnect extends LSDStoreChannelMessage('LSD.Disconnect', {}) {}
177
175
 
178
176
  export class Ping extends LSDReqResMessage('LSD.Ping', {}) {}
179
177
 
180
178
  export class Pong extends LSDReqResMessage('LSD.Pong', {}) {}
181
179
 
182
- export const MessageToAppHostCoordinator = Schema.Union(
180
+ export const MessageToAppLeader = Schema.Union(
183
181
  SnapshotReq,
184
182
  LoadDatabaseFileReq,
185
183
  MutationLogReq,
186
184
  ResetAllDataReq,
187
- MessagePortForStoreRes,
188
185
  NetworkStatusSubscribe,
189
186
  NetworkStatusUnsubscribe,
190
- DevtoolsReady,
191
187
  Disconnect,
192
- DevtoolsConnected,
193
188
  RunMutationReq,
194
189
  Ping,
195
190
  DatabaseFileInfoReq,
196
191
  SyncHistorySubscribe,
197
192
  SyncHistoryUnsubscribe,
198
193
  SyncingInfoReq,
199
- ).annotations({ identifier: 'LSD.MessageToAppHostCoordinator' })
194
+ ).annotations({ identifier: 'LSD.MessageToAppLeader' })
200
195
 
201
- export type MessageToAppHostCoordinator = typeof MessageToAppHostCoordinator.Type
196
+ export type MessageToAppLeader = typeof MessageToAppLeader.Type
202
197
 
203
- export const MessageToAppHostStore = Schema.Union(
198
+ export const MessageToAppClientSession = Schema.Union(
204
199
  DebugInfoReq,
205
200
  DebugInfoHistorySubscribe,
206
201
  DebugInfoHistoryUnsubscribe,
@@ -210,38 +205,39 @@ export const MessageToAppHostStore = Schema.Union(
210
205
  ReactivityGraphUnsubscribe,
211
206
  LiveQueriesSubscribe,
212
207
  LiveQueriesUnsubscribe,
208
+ Disconnect,
209
+ // TODO also introduce a ping/pong protocol for the client session <> devtools connection
213
210
  // Ping,
214
- ).annotations({ identifier: 'LSD.MessageToAppHostStore' })
211
+ ).annotations({ identifier: 'LSD.MessageToAppClientSession' })
215
212
 
216
- export type MessageToAppHostStore = typeof MessageToAppHostStore.Type
213
+ export type MessageToAppClientSession = typeof MessageToAppClientSession.Type
217
214
 
218
- export const MessageFromAppHostCoordinator = Schema.Union(
215
+ export const MessageFromAppLeader = Schema.Union(
219
216
  SnapshotRes,
220
217
  LoadDatabaseFileRes,
221
218
  MutationLogRes,
222
219
  ResetAllDataRes,
223
- MessagePortForStoreReq,
224
220
  Disconnect,
225
221
  MutationBroadcast,
226
- AppHostReady,
227
222
  NetworkStatusRes,
228
223
  RunMutationRes,
229
224
  Pong,
230
225
  DatabaseFileInfoRes,
231
226
  SyncHistoryRes,
232
227
  SyncingInfoRes,
233
- ).annotations({ identifier: 'LSD.MessageFromAppHostCoordinator' })
228
+ ).annotations({ identifier: 'LSD.MessageFromAppLeader' })
234
229
 
235
- export type MessageFromAppHostCoordinator = typeof MessageFromAppHostCoordinator.Type
230
+ export type MessageFromAppLeader = typeof MessageFromAppLeader.Type
236
231
 
237
- export const MessageFromAppHostStore = Schema.Union(
232
+ export const MessageFromAppClientSession = Schema.Union(
238
233
  DebugInfoRes,
239
234
  DebugInfoHistoryRes,
240
235
  DebugInfoResetRes,
241
236
  DebugInfoRerunQueryRes,
242
237
  ReactivityGraphRes,
243
238
  LiveQueriesRes,
239
+ Disconnect,
244
240
  // Pong,
245
- ).annotations({ identifier: 'LSD.MessageFromAppHostStore' })
241
+ ).annotations({ identifier: 'LSD.MessageFromAppClientSession' })
246
242
 
247
- export type MessageFromAppHostStore = typeof MessageFromAppHostStore.Type
243
+ export type MessageFromAppClientSession = typeof MessageFromAppClientSession.Type
@@ -43,7 +43,6 @@ export namespace WebBridge {
43
43
  ): Effect.Effect<WebChannel.WebChannel<typeof All.Type, typeof All.Type>, never, Scope.Scope> =>
44
44
  WebChannel.broadcastChannel({
45
45
  channelName: `livestore-web-bridge-devtools${key ? `-${key}` : ''}`,
46
- listenSchema: All,
47
- sendSchema: All,
46
+ schema: All,
48
47
  })
49
48
  }
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export * from './schema/system-tables.js'
2
2
  export * from './util.js'
3
3
  export * from './adapter-types.js'
4
- export * from './sync/next-mutation-event-id-pair.js'
5
4
  export * from './schema-management/migrations.js'
6
5
  export * from './mutation.js'
7
6
  export * from './init-singleton-tables.js'
@@ -14,6 +13,8 @@ export * from './debug-info.js'
14
13
  export * from './bounded-collections.js'
15
14
  export * from './version.js'
16
15
  export * from './query-builder/mod.js'
16
+ export * from './sync/syncstate.js'
17
+ export * from './otel.js'
17
18
 
18
19
  declare global {
19
20
  interface LiveStoreGlobal {
@@ -1,5 +1,5 @@
1
1
  import type { SynchronousDatabase } from './adapter-types.js'
2
- import type { LiveStoreSchema } from './schema/index.js'
2
+ import type { LiveStoreSchema } from './schema/mod.js'
3
3
  import { getDefaultValuesEncoded } from './schema/schema-helpers.js'
4
4
  import { prepareBindValues, sql } from './util.js'
5
5