@livestore/common 0.2.0 → 0.3.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.
Files changed (244) 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/dist/version.js.map +1 -1
  187. package/package.json +13 -6
  188. package/src/__tests__/fixture.ts +5 -1
  189. package/src/adapter-types.ts +60 -34
  190. package/src/derived-mutations.test.ts +1 -1
  191. package/src/derived-mutations.ts +1 -1
  192. package/src/devtools/devtools-bridge.ts +2 -2
  193. package/src/devtools/devtools-messages.ts +70 -74
  194. package/src/devtools/index.ts +1 -2
  195. package/src/index.ts +2 -1
  196. package/src/init-singleton-tables.ts +1 -1
  197. package/src/leader-thread/apply-mutation.ts +143 -0
  198. package/src/leader-thread/connection.ts +67 -0
  199. package/src/leader-thread/leader-sync-processor.ts +666 -0
  200. package/src/leader-thread/leader-worker-devtools.ts +358 -0
  201. package/src/leader-thread/make-leader-thread-layer.ts +192 -0
  202. package/src/leader-thread/mod.ts +6 -0
  203. package/src/leader-thread/mutationlog.ts +42 -0
  204. package/src/leader-thread/pull-queue-set.ts +58 -0
  205. package/src/leader-thread/recreate-db.ts +109 -0
  206. package/src/leader-thread/shutdown-channel.ts +13 -0
  207. package/src/leader-thread/types.ts +129 -0
  208. package/src/mutation.ts +3 -21
  209. package/src/otel.ts +20 -0
  210. package/src/query-builder/api.ts +3 -2
  211. package/src/query-builder/impl.test.ts +28 -1
  212. package/src/query-builder/impl.ts +21 -5
  213. package/src/query-info.ts +1 -1
  214. package/src/rehydrate-from-mutationlog.ts +7 -11
  215. package/src/schema/EventId.ts +46 -0
  216. package/src/schema/MutationEvent.ts +161 -0
  217. package/src/schema/mod.ts +7 -0
  218. package/src/schema/mutations.ts +5 -126
  219. package/src/schema/{index.ts → schema.ts} +0 -5
  220. package/src/schema/system-tables.ts +18 -5
  221. package/src/schema-management/migrations.ts +9 -2
  222. package/src/schema-management/validate-mutation-defs.ts +1 -1
  223. package/src/sync/client-session-sync-processor.ts +207 -0
  224. package/src/sync/index.ts +2 -0
  225. package/src/sync/next/compact-events.ts +3 -2
  226. package/src/sync/next/facts.ts +11 -5
  227. package/src/sync/next/history-dag-common.ts +44 -0
  228. package/src/sync/next/history-dag.ts +3 -45
  229. package/src/sync/next/mod.ts +1 -0
  230. package/src/sync/next/rebase-events.ts +6 -5
  231. package/src/sync/next/test/compact-events.test.ts +3 -2
  232. package/src/sync/next/test/mutation-fixtures.ts +7 -6
  233. package/src/sync/sync.ts +32 -12
  234. package/src/sync/syncstate.test.ts +464 -0
  235. package/src/sync/syncstate.ts +385 -0
  236. package/src/sync/validate-push-payload.ts +18 -0
  237. package/src/version.ts +2 -2
  238. package/dist/schema/index.d.ts.map +0 -1
  239. package/dist/schema/index.js.map +0 -1
  240. package/dist/sync/next-mutation-event-id-pair.d.ts +0 -14
  241. package/dist/sync/next-mutation-event-id-pair.d.ts.map +0 -1
  242. package/dist/sync/next-mutation-event-id-pair.js +0 -13
  243. package/dist/sync/next-mutation-event-id-pair.js.map +0 -1
  244. package/src/sync/next-mutation-event-id-pair.ts +0 -20
@@ -0,0 +1,44 @@
1
+ import * as EventId from '../../schema/EventId.js'
2
+ import type { MutationEventFactsGroup } from '../../schema/mutations.js'
3
+ import { graphology } from './graphology_.js'
4
+
5
+ export const connectionTypeOptions = ['parent', 'facts'] as const
6
+ export type ConnectionType = (typeof connectionTypeOptions)[number]
7
+
8
+ /**
9
+ * Eventlog represented as a multi-DAG including edges for
10
+ * - total-order (parent) relationships
11
+ * - dependency (requires/reads facts) relationships
12
+ */
13
+ export type HistoryDag = graphology.IGraph<HistoryDagNode, { type: ConnectionType }>
14
+
15
+ export const emptyHistoryDag = (): HistoryDag =>
16
+ new graphology.Graph({
17
+ allowSelfLoops: false,
18
+ multi: true,
19
+ type: 'directed',
20
+ })
21
+
22
+ // TODO consider making `ROOT_ID` parent to itself
23
+ export const rootParentId = { global: EventId.ROOT.global - 1, local: 0 } satisfies EventId.EventId
24
+
25
+ export type HistoryDagNode = {
26
+ id: EventId.EventId
27
+ parentId: EventId.EventId
28
+ mutation: string
29
+ args: any
30
+ /** Facts are being used for conflict detection and history compaction */
31
+ factsGroup: MutationEventFactsGroup
32
+ meta?: any
33
+ }
34
+
35
+ export const rootEventNode: HistoryDagNode = {
36
+ id: EventId.ROOT,
37
+ parentId: rootParentId,
38
+ // unused below
39
+ mutation: '__Root__',
40
+ args: {},
41
+ factsGroup: { modifySet: new Map(), modifyUnset: new Map(), depRequire: new Map(), depRead: new Map() },
42
+ }
43
+
44
+ export const EMPTY_FACT_VALUE = Symbol('EMPTY_FACT_VALUE')
@@ -1,50 +1,8 @@
1
- import { type EventId, ROOT_ID } from '../../adapter-types.js'
2
- import type { MutationEventFactsGroup } from '../../schema/mutations.js'
1
+ import type * as EventId from '../../schema/EventId.js'
3
2
  import { factsToString, validateFacts } from './facts.js'
4
- import { graphology } from './graphology_.js'
3
+ import { emptyHistoryDag, type HistoryDagNode, rootParentId } from './history-dag-common.js'
5
4
 
6
- export const connectionTypeOptions = ['parent', 'facts'] as const
7
- export type ConnectionType = (typeof connectionTypeOptions)[number]
8
-
9
- /**
10
- * Eventlog represented as a multi-DAG including edges for
11
- * - total-order (parent) relationships
12
- * - dependency (requires/reads facts) relationships
13
- */
14
- export type HistoryDag = graphology.IGraph<HistoryDagNode, { type: ConnectionType }>
15
-
16
- export const emptyHistoryDag = (): HistoryDag =>
17
- new graphology.Graph({
18
- allowSelfLoops: false,
19
- multi: true,
20
- type: 'directed',
21
- })
22
-
23
- // TODO consider making `ROOT_ID` parent to itself
24
- const rootParentId = { global: ROOT_ID.global - 1, local: 0 } satisfies EventId
25
-
26
- export type HistoryDagNode = {
27
- id: EventId
28
- parentId: EventId
29
- mutation: string
30
- args: any
31
- /** Facts are being used for conflict detection and history compaction */
32
- factsGroup: MutationEventFactsGroup
33
- meta?: any
34
- }
35
-
36
- export const rootEventNode: HistoryDagNode = {
37
- id: ROOT_ID,
38
- parentId: rootParentId,
39
- // unused below
40
- mutation: '__Root__',
41
- args: {},
42
- factsGroup: { modifySet: new Map(), modifyUnset: new Map(), depRequire: new Map(), depRead: new Map() },
43
- }
44
-
45
- export const EMPTY_FACT_VALUE = Symbol('EMPTY_FACT_VALUE')
46
-
47
- export const eventIdToString = (eventId: EventId) =>
5
+ export const eventIdToString = (eventId: EventId.EventId) =>
48
6
  eventId.local === 0 ? eventId.global.toString() : `${eventId.global}.${eventId.local}`
49
7
 
50
8
  export const historyDagFromNodes = (dagNodes: HistoryDagNode[], options?: { skipFactsCheck: boolean }) => {
@@ -1,3 +1,4 @@
1
+ export * from './history-dag-common.js'
1
2
  export * from './history-dag.js'
2
3
  export * from './facts.js'
3
4
  export * from './compact-events.js'
@@ -1,5 +1,6 @@
1
- import type { EventId } from '../../adapter-types.js'
2
- import type { MutationDef, MutationEvent, MutationEventFactsSnapshot } from '../../schema/mutations.js'
1
+ import type * as EventId from '../../schema/EventId.js'
2
+ import type * as MutationEvent from '../../schema/MutationEvent.js'
3
+ import type { MutationDef, MutationEventFactsSnapshot } from '../../schema/mutations.js'
3
4
  import {
4
5
  applyFactGroups,
5
6
  factsIntersect,
@@ -7,7 +8,7 @@ import {
7
8
  getFactsGroupForMutationArgs,
8
9
  validateFacts,
9
10
  } from './facts.js'
10
- import type { HistoryDagNode } from './history-dag.js'
11
+ import type { HistoryDagNode } from './history-dag-common.js'
11
12
 
12
13
  export type RebaseEventWithConflict = HistoryDagNode & {
13
14
  conflictType: 'overlap' | 'missing-requirement'
@@ -88,8 +89,8 @@ export const rebaseEvents = ({
88
89
  return rebasedLocalEvents.map(
89
90
  (event, index) =>
90
91
  ({
91
- id: { global: headGlobalId + index + 1, local: 0 } satisfies EventId,
92
- parentId: { global: headGlobalId + index, local: 0 } satisfies EventId,
92
+ id: { global: headGlobalId + index + 1, local: 0 } satisfies EventId.EventId,
93
+ parentId: { global: headGlobalId + index, local: 0 } satisfies EventId.EventId,
93
94
  mutation: event.mutation,
94
95
  args: event.args,
95
96
  }) satisfies MutationEvent.Any,
@@ -2,8 +2,9 @@ import type { MutationEventFacts } from '@livestore/common/schema'
2
2
  import { describe, expect, it } from 'vitest'
3
3
 
4
4
  import { compactEvents } from '../compact-events.js'
5
- import type { HistoryDagNode } from '../history-dag.js'
6
- import { EMPTY_FACT_VALUE, historyDagFromNodes } from '../history-dag.js'
5
+ import { historyDagFromNodes } from '../history-dag.js'
6
+ import type { HistoryDagNode } from '../history-dag-common.js'
7
+ import { EMPTY_FACT_VALUE } from '../history-dag-common.js'
7
8
  import { mutations, toEventNodes } from './mutation-fixtures.js'
8
9
 
9
10
  const customStringify = (value: any): string => {
@@ -1,11 +1,12 @@
1
1
  import { Schema } from '@livestore/utils/effect'
2
2
 
3
- import { type EventId, ROOT_ID } from '../../../adapter-types.js'
3
+ import * as EventId from '../../../schema/EventId.js'
4
4
  import type { MutationDef } from '../../../schema/mutations.js'
5
5
  import { defineFacts, defineMutation } from '../../../schema/mutations.js'
6
6
  import { factsSnapshotForDag, getFactsGroupForMutationArgs } from '../facts.js'
7
- import type { HistoryDagNode } from '../history-dag.js'
8
- import { historyDagFromNodes, rootEventNode } from '../history-dag.js'
7
+ import { historyDagFromNodes } from '../history-dag.js'
8
+ import type { HistoryDagNode } from '../history-dag-common.js'
9
+ import { rootEventNode } from '../history-dag-common.js'
9
10
 
10
11
  /** Used for conflict detection and event history compaction */
11
12
  export const facts = defineFacts({
@@ -137,9 +138,9 @@ export const toEventNodes = (
137
138
  ): HistoryDagNode[] => {
138
139
  const nodesAcc: HistoryDagNode[] = [rootEventNode]
139
140
 
140
- let currentEventId: EventId = ROOT_ID
141
+ let currentEventId: EventId.EventId = EventId.ROOT
141
142
 
142
- const getNextEventId = (mutationDef: MutationDef.Any): EventId => {
143
+ const getNextEventId = (mutationDef: MutationDef.Any): EventId.EventId => {
143
144
  if (mutationDef.options.localOnly) {
144
145
  return { global: currentEventId.global, local: currentEventId.local + 1 }
145
146
  }
@@ -218,7 +219,7 @@ export const toEventNodes = (
218
219
  return eventNodes
219
220
  }
220
221
 
221
- const getParentId = (eventId: EventId): EventId => {
222
+ const getParentId = (eventId: EventId.EventId): EventId.EventId => {
222
223
  const globalParentId = eventId.global
223
224
  const localParentId = eventId.local - 1
224
225
 
package/src/sync/sync.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { Effect, HttpClient, Option, Stream, SubscriptionRef } from '@livestore/utils/effect'
2
2
  import { Schema } from '@livestore/utils/effect'
3
3
 
4
- import type { EventId } from '../adapter-types.js'
5
- import type { MutationEvent } from '../schema/mutations.js'
4
+ import * as EventId from '../schema/EventId.js'
5
+ import type * as MutationEvent from '../schema/MutationEvent.js'
6
6
 
7
7
  export interface SyncBackendOptionsBase {
8
8
  type: string
@@ -12,25 +12,33 @@ export interface SyncBackendOptionsBase {
12
12
  export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
13
13
  pull: (
14
14
  args: Option.Option<{
15
- cursor: EventId
15
+ cursor: EventId.EventId
16
16
  metadata: Option.Option<TSyncMetadata>
17
17
  }>,
18
- options: { listenForNew: boolean },
19
18
  ) => Stream.Stream<
20
19
  {
21
- mutationEventEncoded: MutationEvent.AnyEncoded
22
- metadata: Option.Option<TSyncMetadata>
23
- persisted: boolean
20
+ batch: ReadonlyArray<{
21
+ mutationEventEncoded: MutationEvent.AnyEncoded
22
+ metadata: Option.Option<TSyncMetadata>
23
+ }>
24
+ remaining: number
24
25
  },
25
26
  IsOfflineError | InvalidPullError,
26
27
  HttpClient.HttpClient
27
28
  >
28
- // TODO support batching
29
+ // TODO support transactions (i.e. group of mutation events which need to be applied together)
29
30
  push: (
30
- mutationEventEncoded: MutationEvent.AnyEncoded,
31
- persisted: boolean,
31
+ /**
32
+ * Constraints for batch:
33
+ * - Number of events: 1-100
34
+ * - event ids must be in ascending order
35
+ * */
36
+ batch: ReadonlyArray<MutationEvent.AnyEncoded>,
32
37
  ) => Effect.Effect<
33
- { metadata: Option.Option<TSyncMetadata> },
38
+ {
39
+ /** Indexes are relative to `batch` */
40
+ metadata: ReadonlyArray<Option.Option<TSyncMetadata>>
41
+ },
34
42
  IsOfflineError | InvalidPushError,
35
43
  HttpClient.HttpClient
36
44
  >
@@ -39,7 +47,19 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
39
47
 
40
48
  export class IsOfflineError extends Schema.TaggedError<IsOfflineError>()('IsOfflineError', {}) {}
41
49
  export class InvalidPushError extends Schema.TaggedError<InvalidPushError>()('InvalidPushError', {
42
- message: Schema.String,
50
+ reason: Schema.Union(
51
+ Schema.TaggedStruct('Unexpected', {
52
+ message: Schema.String,
53
+ }),
54
+ Schema.TaggedStruct('ServerAhead', {
55
+ minimumExpectedId: Schema.Number,
56
+ providedId: Schema.Number,
57
+ }),
58
+ Schema.TaggedStruct('LeaderAhead', {
59
+ minimumExpectedId: EventId.EventId,
60
+ providedId: EventId.EventId,
61
+ }),
62
+ ),
43
63
  }) {}
44
64
  export class InvalidPullError extends Schema.TaggedError<InvalidPullError>()('InvalidPullError', {
45
65
  message: Schema.String,