@livestore/common 0.3.0-dev.28 → 0.3.0-dev.29

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 (277) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/__tests__/fixture.d.ts +83 -221
  3. package/dist/__tests__/fixture.d.ts.map +1 -1
  4. package/dist/__tests__/fixture.js +33 -11
  5. package/dist/__tests__/fixture.js.map +1 -1
  6. package/dist/adapter-types.d.ts +22 -15
  7. package/dist/adapter-types.d.ts.map +1 -1
  8. package/dist/adapter-types.js +15 -2
  9. package/dist/adapter-types.js.map +1 -1
  10. package/dist/bounded-collections.d.ts +1 -1
  11. package/dist/bounded-collections.d.ts.map +1 -1
  12. package/dist/debug-info.d.ts.map +1 -1
  13. package/dist/debug-info.js +1 -0
  14. package/dist/debug-info.js.map +1 -1
  15. package/dist/devtools/devtools-messages-client-session.d.ts +21 -21
  16. package/dist/devtools/devtools-messages-common.d.ts +6 -6
  17. package/dist/devtools/devtools-messages-leader.d.ts +45 -45
  18. package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
  19. package/dist/devtools/devtools-messages-leader.js +11 -11
  20. package/dist/devtools/devtools-messages-leader.js.map +1 -1
  21. package/dist/index.d.ts +2 -5
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/leader-thread/LeaderSyncProcessor.d.ts +10 -10
  26. package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
  27. package/dist/leader-thread/LeaderSyncProcessor.js +63 -65
  28. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
  29. package/dist/leader-thread/{apply-mutation.d.ts → apply-event.d.ts} +7 -7
  30. package/dist/leader-thread/apply-event.d.ts.map +1 -0
  31. package/dist/leader-thread/apply-event.js +103 -0
  32. package/dist/leader-thread/apply-event.js.map +1 -0
  33. package/dist/leader-thread/eventlog.d.ts +27 -0
  34. package/dist/leader-thread/eventlog.d.ts.map +1 -0
  35. package/dist/leader-thread/eventlog.js +123 -0
  36. package/dist/leader-thread/eventlog.js.map +1 -0
  37. package/dist/leader-thread/leader-worker-devtools.js +18 -18
  38. package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
  39. package/dist/leader-thread/make-leader-thread-layer.d.ts +2 -2
  40. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
  41. package/dist/leader-thread/make-leader-thread-layer.js +16 -16
  42. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
  43. package/dist/leader-thread/mod.d.ts +1 -1
  44. package/dist/leader-thread/mod.d.ts.map +1 -1
  45. package/dist/leader-thread/mod.js +1 -1
  46. package/dist/leader-thread/mod.js.map +1 -1
  47. package/dist/leader-thread/recreate-db.d.ts.map +1 -1
  48. package/dist/leader-thread/recreate-db.js +6 -8
  49. package/dist/leader-thread/recreate-db.js.map +1 -1
  50. package/dist/leader-thread/types.d.ts +11 -11
  51. package/dist/leader-thread/types.d.ts.map +1 -1
  52. package/dist/materializer-helper.d.ts +23 -0
  53. package/dist/materializer-helper.d.ts.map +1 -0
  54. package/dist/materializer-helper.js +70 -0
  55. package/dist/materializer-helper.js.map +1 -0
  56. package/dist/query-builder/api.d.ts +58 -53
  57. package/dist/query-builder/api.d.ts.map +1 -1
  58. package/dist/query-builder/api.js +3 -5
  59. package/dist/query-builder/api.js.map +1 -1
  60. package/dist/query-builder/astToSql.d.ts.map +1 -1
  61. package/dist/query-builder/astToSql.js +59 -37
  62. package/dist/query-builder/astToSql.js.map +1 -1
  63. package/dist/query-builder/impl.d.ts +2 -3
  64. package/dist/query-builder/impl.d.ts.map +1 -1
  65. package/dist/query-builder/impl.js +48 -46
  66. package/dist/query-builder/impl.js.map +1 -1
  67. package/dist/query-builder/impl.test.d.ts +86 -1
  68. package/dist/query-builder/impl.test.d.ts.map +1 -1
  69. package/dist/query-builder/impl.test.js +244 -36
  70. package/dist/query-builder/impl.test.js.map +1 -1
  71. package/dist/rehydrate-from-eventlog.d.ts +14 -0
  72. package/dist/rehydrate-from-eventlog.d.ts.map +1 -0
  73. package/dist/{rehydrate-from-mutationlog.js → rehydrate-from-eventlog.js} +25 -26
  74. package/dist/rehydrate-from-eventlog.js.map +1 -0
  75. package/dist/schema/EventDef.d.ts +136 -0
  76. package/dist/schema/EventDef.d.ts.map +1 -0
  77. package/dist/schema/EventDef.js +58 -0
  78. package/dist/schema/EventDef.js.map +1 -0
  79. package/dist/schema/EventId.d.ts +2 -2
  80. package/dist/schema/EventId.d.ts.map +1 -1
  81. package/dist/schema/EventId.js +3 -2
  82. package/dist/schema/EventId.js.map +1 -1
  83. package/dist/schema/{MutationEvent.d.ts → LiveStoreEvent.d.ts} +56 -56
  84. package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
  85. package/dist/schema/{MutationEvent.js → LiveStoreEvent.js} +24 -24
  86. package/dist/schema/LiveStoreEvent.js.map +1 -0
  87. package/dist/schema/client-document-def.d.ts +223 -0
  88. package/dist/schema/client-document-def.d.ts.map +1 -0
  89. package/dist/schema/client-document-def.js +170 -0
  90. package/dist/schema/client-document-def.js.map +1 -0
  91. package/dist/schema/client-document-def.test.d.ts +2 -0
  92. package/dist/schema/client-document-def.test.d.ts.map +1 -0
  93. package/dist/schema/client-document-def.test.js +201 -0
  94. package/dist/schema/client-document-def.test.js.map +1 -0
  95. package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -1
  96. package/dist/schema/events.d.ts +2 -0
  97. package/dist/schema/events.d.ts.map +1 -0
  98. package/dist/schema/events.js +2 -0
  99. package/dist/schema/events.js.map +1 -0
  100. package/dist/schema/mod.d.ts +4 -3
  101. package/dist/schema/mod.d.ts.map +1 -1
  102. package/dist/schema/mod.js +4 -3
  103. package/dist/schema/mod.js.map +1 -1
  104. package/dist/schema/schema.d.ts +27 -23
  105. package/dist/schema/schema.d.ts.map +1 -1
  106. package/dist/schema/schema.js +45 -43
  107. package/dist/schema/schema.js.map +1 -1
  108. package/dist/schema/sqlite-state.d.ts +12 -0
  109. package/dist/schema/sqlite-state.d.ts.map +1 -0
  110. package/dist/schema/sqlite-state.js +36 -0
  111. package/dist/schema/sqlite-state.js.map +1 -0
  112. package/dist/schema/system-tables.d.ts +67 -98
  113. package/dist/schema/system-tables.d.ts.map +1 -1
  114. package/dist/schema/system-tables.js +62 -48
  115. package/dist/schema/system-tables.js.map +1 -1
  116. package/dist/schema/table-def.d.ts +26 -96
  117. package/dist/schema/table-def.d.ts.map +1 -1
  118. package/dist/schema/table-def.js +16 -64
  119. package/dist/schema/table-def.js.map +1 -1
  120. package/dist/schema/view.d.ts +3 -0
  121. package/dist/schema/view.d.ts.map +1 -0
  122. package/dist/schema/view.js +3 -0
  123. package/dist/schema/view.js.map +1 -0
  124. package/dist/schema-management/common.d.ts +4 -4
  125. package/dist/schema-management/common.d.ts.map +1 -1
  126. package/dist/schema-management/migrations.d.ts.map +1 -1
  127. package/dist/schema-management/migrations.js +6 -6
  128. package/dist/schema-management/migrations.js.map +1 -1
  129. package/dist/schema-management/validate-mutation-defs.d.ts +3 -3
  130. package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
  131. package/dist/schema-management/validate-mutation-defs.js +17 -17
  132. package/dist/schema-management/validate-mutation-defs.js.map +1 -1
  133. package/dist/sync/ClientSessionSyncProcessor.d.ts +7 -7
  134. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
  135. package/dist/sync/ClientSessionSyncProcessor.js +31 -30
  136. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
  137. package/dist/sync/next/facts.d.ts +19 -19
  138. package/dist/sync/next/facts.d.ts.map +1 -1
  139. package/dist/sync/next/facts.js +2 -2
  140. package/dist/sync/next/facts.js.map +1 -1
  141. package/dist/sync/next/history-dag-common.d.ts +3 -3
  142. package/dist/sync/next/history-dag-common.d.ts.map +1 -1
  143. package/dist/sync/next/history-dag-common.js +1 -1
  144. package/dist/sync/next/history-dag-common.js.map +1 -1
  145. package/dist/sync/next/history-dag.js +1 -1
  146. package/dist/sync/next/history-dag.js.map +1 -1
  147. package/dist/sync/next/rebase-events.d.ts +7 -7
  148. package/dist/sync/next/rebase-events.d.ts.map +1 -1
  149. package/dist/sync/next/rebase-events.js +5 -5
  150. package/dist/sync/next/rebase-events.js.map +1 -1
  151. package/dist/sync/next/test/compact-events.calculator.test.js +38 -33
  152. package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
  153. package/dist/sync/next/test/compact-events.test.js +71 -71
  154. package/dist/sync/next/test/compact-events.test.js.map +1 -1
  155. package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +29 -29
  156. package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
  157. package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +60 -25
  158. package/dist/sync/next/test/event-fixtures.js.map +1 -0
  159. package/dist/sync/next/test/mod.d.ts +1 -1
  160. package/dist/sync/next/test/mod.d.ts.map +1 -1
  161. package/dist/sync/next/test/mod.js +1 -1
  162. package/dist/sync/next/test/mod.js.map +1 -1
  163. package/dist/sync/sync.d.ts +3 -3
  164. package/dist/sync/sync.d.ts.map +1 -1
  165. package/dist/sync/syncstate.d.ts +32 -32
  166. package/dist/sync/syncstate.d.ts.map +1 -1
  167. package/dist/sync/syncstate.js +10 -10
  168. package/dist/sync/syncstate.js.map +1 -1
  169. package/dist/sync/syncstate.test.js +5 -5
  170. package/dist/sync/syncstate.test.js.map +1 -1
  171. package/dist/sync/validate-push-payload.d.ts +2 -2
  172. package/dist/sync/validate-push-payload.d.ts.map +1 -1
  173. package/dist/sync/validate-push-payload.js.map +1 -1
  174. package/dist/version.d.ts +1 -1
  175. package/dist/version.js +1 -1
  176. package/package.json +3 -3
  177. package/src/__tests__/fixture.ts +36 -15
  178. package/src/adapter-types.ts +23 -16
  179. package/src/debug-info.ts +1 -0
  180. package/src/devtools/devtools-messages-leader.ts +13 -13
  181. package/src/index.ts +2 -5
  182. package/src/leader-thread/LeaderSyncProcessor.ts +81 -91
  183. package/src/leader-thread/{apply-mutation.ts → apply-event.ts} +50 -74
  184. package/src/leader-thread/eventlog.ts +199 -0
  185. package/src/leader-thread/leader-worker-devtools.ts +18 -18
  186. package/src/leader-thread/make-leader-thread-layer.ts +18 -18
  187. package/src/leader-thread/mod.ts +1 -1
  188. package/src/leader-thread/recreate-db.ts +6 -9
  189. package/src/leader-thread/types.ts +12 -12
  190. package/src/materializer-helper.ts +110 -0
  191. package/src/query-builder/api.ts +79 -105
  192. package/src/query-builder/astToSql.ts +68 -39
  193. package/src/query-builder/impl.test.ts +264 -42
  194. package/src/query-builder/impl.ts +72 -56
  195. package/src/{rehydrate-from-mutationlog.ts → rehydrate-from-eventlog.ts} +33 -40
  196. package/src/schema/EventDef.ts +216 -0
  197. package/src/schema/EventId.ts +5 -3
  198. package/src/schema/{MutationEvent.ts → LiveStoreEvent.ts} +67 -68
  199. package/src/schema/client-document-def.test.ts +239 -0
  200. package/src/schema/client-document-def.ts +444 -0
  201. package/src/schema/db-schema/dsl/mod.ts +0 -1
  202. package/src/schema/events.ts +1 -0
  203. package/src/schema/mod.ts +4 -3
  204. package/src/schema/schema.ts +79 -69
  205. package/src/schema/sqlite-state.ts +62 -0
  206. package/src/schema/system-tables.ts +42 -53
  207. package/src/schema/table-def.ts +53 -209
  208. package/src/schema/view.ts +2 -0
  209. package/src/schema-management/common.ts +4 -4
  210. package/src/schema-management/migrations.ts +8 -9
  211. package/src/schema-management/validate-mutation-defs.ts +22 -24
  212. package/src/sync/ClientSessionSyncProcessor.ts +37 -36
  213. package/src/sync/next/facts.ts +31 -32
  214. package/src/sync/next/history-dag-common.ts +4 -4
  215. package/src/sync/next/history-dag.ts +1 -1
  216. package/src/sync/next/rebase-events.ts +13 -13
  217. package/src/sync/next/test/compact-events.calculator.test.ts +45 -45
  218. package/src/sync/next/test/compact-events.test.ts +73 -73
  219. package/src/sync/next/test/event-fixtures.ts +219 -0
  220. package/src/sync/next/test/mod.ts +1 -1
  221. package/src/sync/sync.ts +3 -3
  222. package/src/sync/syncstate.test.ts +8 -8
  223. package/src/sync/syncstate.ts +19 -19
  224. package/src/sync/validate-push-payload.ts +2 -2
  225. package/src/version.ts +1 -1
  226. package/tmp/pack.tgz +0 -0
  227. package/tsconfig.json +1 -0
  228. package/dist/derived-mutations.d.ts +0 -109
  229. package/dist/derived-mutations.d.ts.map +0 -1
  230. package/dist/derived-mutations.js +0 -54
  231. package/dist/derived-mutations.js.map +0 -1
  232. package/dist/derived-mutations.test.d.ts +0 -2
  233. package/dist/derived-mutations.test.d.ts.map +0 -1
  234. package/dist/derived-mutations.test.js +0 -93
  235. package/dist/derived-mutations.test.js.map +0 -1
  236. package/dist/init-singleton-tables.d.ts +0 -4
  237. package/dist/init-singleton-tables.d.ts.map +0 -1
  238. package/dist/init-singleton-tables.js +0 -16
  239. package/dist/init-singleton-tables.js.map +0 -1
  240. package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
  241. package/dist/leader-thread/apply-mutation.js +0 -122
  242. package/dist/leader-thread/apply-mutation.js.map +0 -1
  243. package/dist/leader-thread/mutationlog.d.ts +0 -27
  244. package/dist/leader-thread/mutationlog.d.ts.map +0 -1
  245. package/dist/leader-thread/mutationlog.js +0 -124
  246. package/dist/leader-thread/mutationlog.js.map +0 -1
  247. package/dist/leader-thread/pull-queue-set.d.ts +0 -7
  248. package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
  249. package/dist/leader-thread/pull-queue-set.js +0 -38
  250. package/dist/leader-thread/pull-queue-set.js.map +0 -1
  251. package/dist/mutation.d.ts +0 -20
  252. package/dist/mutation.d.ts.map +0 -1
  253. package/dist/mutation.js +0 -68
  254. package/dist/mutation.js.map +0 -1
  255. package/dist/query-info.d.ts +0 -41
  256. package/dist/query-info.d.ts.map +0 -1
  257. package/dist/query-info.js +0 -7
  258. package/dist/query-info.js.map +0 -1
  259. package/dist/rehydrate-from-mutationlog.d.ts +0 -15
  260. package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
  261. package/dist/rehydrate-from-mutationlog.js.map +0 -1
  262. package/dist/schema/MutationEvent.d.ts.map +0 -1
  263. package/dist/schema/MutationEvent.js.map +0 -1
  264. package/dist/schema/mutations.d.ts +0 -115
  265. package/dist/schema/mutations.d.ts.map +0 -1
  266. package/dist/schema/mutations.js +0 -42
  267. package/dist/schema/mutations.js.map +0 -1
  268. package/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
  269. package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
  270. package/src/derived-mutations.test.ts +0 -101
  271. package/src/derived-mutations.ts +0 -170
  272. package/src/init-singleton-tables.ts +0 -24
  273. package/src/leader-thread/mutationlog.ts +0 -202
  274. package/src/mutation.ts +0 -108
  275. package/src/query-info.ts +0 -83
  276. package/src/schema/mutations.ts +0 -193
  277. package/src/sync/next/test/mutation-fixtures.ts +0 -228
@@ -1,11 +1,11 @@
1
- import type { MutationEventFacts } from '@livestore/common/schema'
1
+ import type { EventDefFacts } from '@livestore/common/schema'
2
2
  import { describe, expect, it } from 'vitest'
3
3
 
4
4
  import { compactEvents } from '../compact-events.js'
5
5
  import { historyDagFromNodes } from '../history-dag.js'
6
6
  import type { HistoryDagNode } from '../history-dag-common.js'
7
7
  import { EMPTY_FACT_VALUE } from '../history-dag-common.js'
8
- import { mutations, toEventNodes } from './mutation-fixtures.js'
8
+ import { events as eventDefs, toEventNodes } from './event-fixtures.js'
9
9
 
10
10
  const customStringify = (value: any): string => {
11
11
  if (value === null) {
@@ -58,7 +58,7 @@ const factsToString = (facts: HistoryDagNode['factsGroup']) =>
58
58
  .flat()
59
59
  .join(' ')
60
60
 
61
- const factsSetToString = (facts: MutationEventFacts, prefix: string) =>
61
+ const factsSetToString = (facts: EventDefFacts, prefix: string) =>
62
62
  Array.from(facts.entries()).map(([key, value]) => prefix + key + (value === EMPTY_FACT_VALUE ? '' : `=${value}`))
63
63
 
64
64
  export const customSerializer = {
@@ -71,7 +71,7 @@ export const customSerializer = {
71
71
  expect.addSnapshotSerializer(customSerializer)
72
72
 
73
73
  const compact = (events: any[]) => {
74
- const dag = historyDagFromNodes(toEventNodes(events, mutations, 'client-id', 'session-id'))
74
+ const dag = historyDagFromNodes(toEventNodes(events, eventDefs, 'client-id', 'session-id'))
75
75
  const compacted = compactEvents(dag)
76
76
 
77
77
  return Array.from(compacted.dag.nodeEntries())
@@ -83,90 +83,90 @@ const compact = (events: any[]) => {
83
83
  describe('compactEvents todo app', () => {
84
84
  it('todoCompleted', () => {
85
85
  const expected = compact([
86
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
87
- mutations.todoCompleted({ id: 'A' }), // 1
88
- mutations.todoCompleted({ id: 'A' }), // 2
86
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
87
+ eventDefs.todoCompleted({ id: 'A' }), // 1
88
+ eventDefs.todoCompleted({ id: 'A' }), // 2
89
89
  ])
90
90
 
91
91
  expect(expected).toMatchInlineSnapshot(`
92
92
  [
93
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
94
- { id: 2, parentId: 0, mutation: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
93
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
94
+ { id: 3, parentId: 1, name: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
95
95
  ]
96
96
  `)
97
97
  })
98
98
 
99
99
  it('toggleTodo', () => {
100
100
  const expected = compact([
101
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
102
- mutations.toggleTodo({ id: 'A' }), // 1
103
- mutations.toggleTodo({ id: 'A' }), // 2
104
- mutations.toggleTodo({ id: 'A' }), // 3
101
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
102
+ eventDefs.toggleTodo({ id: 'A' }), // 1
103
+ eventDefs.toggleTodo({ id: 'A' }), // 2
104
+ eventDefs.toggleTodo({ id: 'A' }), // 3
105
105
  ])
106
106
 
107
107
  expect(expected).toMatchInlineSnapshot(`
108
108
  [
109
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
110
- { id: 1, parentId: 0, mutation: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=true" }
111
- { id: 2, parentId: 1, mutation: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=false" }
112
- { id: 3, parentId: 2, mutation: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=true" }
109
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
110
+ { id: 2, parentId: 1, name: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=true" }
111
+ { id: 3, parentId: 2, name: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=false" }
112
+ { id: 4, parentId: 3, name: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=true" }
113
113
  ]
114
114
  `)
115
115
  })
116
116
 
117
117
  it('todoCompleted / toggleTodo', () => {
118
118
  const expected = compact([
119
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
120
- mutations.toggleTodo({ id: 'A' }), // 1
121
- mutations.toggleTodo({ id: 'A' }), // 2
122
- mutations.todoCompleted({ id: 'A' }), // 3
123
- mutations.todoCompleted({ id: 'A' }), // 4
124
- mutations.toggleTodo({ id: 'A' }), // 5
119
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
120
+ eventDefs.toggleTodo({ id: 'A' }), // 1
121
+ eventDefs.toggleTodo({ id: 'A' }), // 2
122
+ eventDefs.todoCompleted({ id: 'A' }), // 3
123
+ eventDefs.todoCompleted({ id: 'A' }), // 4
124
+ eventDefs.toggleTodo({ id: 'A' }), // 5
125
125
  ])
126
126
 
127
127
  expect(expected).toMatchInlineSnapshot(`
128
128
  [
129
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
130
- { id: 4, parentId: 0, mutation: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
131
- { id: 5, parentId: 4, mutation: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=false" }
129
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
130
+ { id: 5, parentId: 1, name: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
131
+ { id: 6, parentId: 5, name: "toggleTodo", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=false" }
132
132
  ]
133
133
  `)
134
134
  })
135
135
 
136
136
  it('readonly setTextTodo', () => {
137
137
  const expected = compact([
138
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
139
- mutations.setReadonlyTodo({ id: 'A', readonly: false }), // 1
140
- mutations.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 2
141
- mutations.setReadonlyTodo({ id: 'A', readonly: true }), // 3
138
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
139
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: false }), // 1
140
+ eventDefs.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 2
141
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: true }), // 3
142
142
  ])
143
143
 
144
144
  expect(expected).toMatchInlineSnapshot(`
145
145
  [
146
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
147
- { id: 1, parentId: 0, mutation: "setReadonlyTodo", args: { id: "A", readonly: false }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=true" }
148
- { id: 2, parentId: 1, mutation: "setTextTodo", args: { id: "A", text: "buy soy milk" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-text-updated-A" }
149
- { id: 3, parentId: 2, mutation: "setReadonlyTodo", args: { id: "A", readonly: true }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=false" }
146
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
147
+ { id: 2, parentId: 1, name: "setReadonlyTodo", args: { id: "A", readonly: false }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=true" }
148
+ { id: 3, parentId: 2, name: "setTextTodo", args: { id: "A", text: "buy soy milk" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-text-updated-A" }
149
+ { id: 4, parentId: 3, name: "setReadonlyTodo", args: { id: "A", readonly: true }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=false" }
150
150
  ]
151
151
  `)
152
152
  })
153
153
 
154
154
  it('readonly setTextTodo 2', () => {
155
155
  const expected = compact([
156
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
157
- mutations.setReadonlyTodo({ id: 'A', readonly: false }), // 1
158
- mutations.todoCompleted({ id: 'A' }), // 2
159
- mutations.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 3
160
- mutations.setReadonlyTodo({ id: 'A', readonly: true }), // 4
156
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
157
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: false }), // 1
158
+ eventDefs.todoCompleted({ id: 'A' }), // 2
159
+ eventDefs.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 3
160
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: true }), // 4
161
161
  ])
162
162
 
163
163
  expect(expected).toMatchInlineSnapshot(`
164
164
  [
165
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
166
- { id: 1, parentId: 0, mutation: "setReadonlyTodo", args: { id: "A", readonly: false }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=true" }
167
- { id: 2, parentId: 1, mutation: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
168
- { id: 3, parentId: 2, mutation: "setTextTodo", args: { id: "A", text: "buy soy milk" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-text-updated-A" }
169
- { id: 4, parentId: 3, mutation: "setReadonlyTodo", args: { id: "A", readonly: true }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=false" }
165
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
166
+ { id: 2, parentId: 1, name: "setReadonlyTodo", args: { id: "A", readonly: false }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=true" }
167
+ { id: 3, parentId: 2, name: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
168
+ { id: 4, parentId: 3, name: "setTextTodo", args: { id: "A", text: "buy soy milk" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-text-updated-A" }
169
+ { id: 5, parentId: 4, name: "setReadonlyTodo", args: { id: "A", readonly: true }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A +todo-is-writeable-A=false" }
170
170
  ]
171
171
  `)
172
172
  })
@@ -174,15 +174,15 @@ describe('compactEvents todo app', () => {
174
174
  it('readonly setTextTodo - should fail', () => {
175
175
  const expected = () =>
176
176
  compact([
177
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
178
- mutations.setReadonlyTodo({ id: 'A', readonly: false }), // 1
179
- mutations.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 2
180
- mutations.setReadonlyTodo({ id: 'A', readonly: true }), // 3
181
- mutations.setTextTodo({ id: 'A', text: 'buy oat milk' }), // 4
177
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
178
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: false }), // 1
179
+ eventDefs.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 2
180
+ eventDefs.setReadonlyTodo({ id: 'A', readonly: true }), // 3
181
+ eventDefs.setTextTodo({ id: 'A', text: 'buy oat milk' }), // 4
182
182
  ])
183
183
 
184
184
  expect(expected).toThrowErrorMatchingInlineSnapshot(`
185
- [Error: Mutation setTextTodo requires facts that have not been set yet.
185
+ [Error: Event setTextTodo requires facts that have not been set yet.
186
186
  Requires: todo-exists-A, todo-is-writeable-A=true
187
187
  Facts Snapshot: todo-exists-A, todo-is-writeable-A=false, todo-completed-A=false, todo-text-updated-A]
188
188
  `)
@@ -190,43 +190,43 @@ describe('compactEvents todo app', () => {
190
190
 
191
191
  it('todoCompleteds', () => {
192
192
  const expected = compact([
193
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
194
- mutations.createTodo({ id: 'B', text: 'buy bread' }), // 1
195
- mutations.createTodo({ id: 'C', text: 'buy cheese' }), // 2
196
- mutations.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 3
197
- mutations.toggleTodo({ id: 'A' }), // 4
198
- mutations.todoCompleted({ id: 'A' }), // 5
193
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
194
+ eventDefs.createTodo({ id: 'B', text: 'buy bread' }), // 1
195
+ eventDefs.createTodo({ id: 'C', text: 'buy cheese' }), // 2
196
+ eventDefs.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 3
197
+ eventDefs.toggleTodo({ id: 'A' }), // 4
198
+ eventDefs.todoCompleted({ id: 'A' }), // 5
199
199
  ])
200
200
 
201
201
  expect(expected).toMatchInlineSnapshot(`
202
202
  [
203
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
204
- { id: 1, parentId: 0, mutation: "createTodo", args: { id: "B", text: "buy bread" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-B +todo-is-writeable-B=true +todo-completed-B=false" }
205
- { id: 2, parentId: 1, mutation: "createTodo", args: { id: "C", text: "buy cheese" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
206
- { id: 3, parentId: 2, mutation: "todoCompleteds", args: { ids: ["A", "B", "C"] }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ↖todo-exists-B ↖todo-is-writeable-B=true ↖todo-exists-C ↖todo-is-writeable-C=true +todo-completed-A=true +todo-completed-B=true +todo-completed-C=true" }
207
- { id: 5, parentId: 3, mutation: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
203
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
204
+ { id: 2, parentId: 1, name: "createTodo", args: { id: "B", text: "buy bread" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-B +todo-is-writeable-B=true +todo-completed-B=false" }
205
+ { id: 3, parentId: 2, name: "createTodo", args: { id: "C", text: "buy cheese" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
206
+ { id: 4, parentId: 3, name: "todoCompleteds", args: { ids: ["A", "B", "C"] }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ↖todo-exists-B ↖todo-is-writeable-B=true ↖todo-exists-C ↖todo-is-writeable-C=true +todo-completed-A=true +todo-completed-B=true +todo-completed-C=true" }
207
+ { id: 6, parentId: 4, name: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
208
208
  ]
209
209
  `)
210
210
  })
211
211
 
212
212
  it('todoCompleteds 2', () => {
213
213
  const expected = compact([
214
- mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
215
- mutations.createTodo({ id: 'B', text: 'buy bread' }), // 1
216
- mutations.createTodo({ id: 'C', text: 'buy cheese' }), // 2
217
- mutations.toggleTodo({ id: 'A' }), // 3
218
- mutations.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 4
219
- mutations.toggleTodo({ id: 'A' }), // 5
220
- mutations.todoCompleted({ id: 'A' }), // 6
214
+ eventDefs.createTodo({ id: 'A', text: 'buy milk' }), // 0
215
+ eventDefs.createTodo({ id: 'B', text: 'buy bread' }), // 1
216
+ eventDefs.createTodo({ id: 'C', text: 'buy cheese' }), // 2
217
+ eventDefs.toggleTodo({ id: 'A' }), // 3
218
+ eventDefs.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 4
219
+ eventDefs.toggleTodo({ id: 'A' }), // 5
220
+ eventDefs.todoCompleted({ id: 'A' }), // 6
221
221
  ])
222
222
 
223
223
  expect(expected).toMatchInlineSnapshot(`
224
224
  [
225
- { id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
226
- { id: 1, parentId: 0, mutation: "createTodo", args: { id: "B", text: "buy bread" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-B +todo-is-writeable-B=true +todo-completed-B=false" }
227
- { id: 2, parentId: 1, mutation: "createTodo", args: { id: "C", text: "buy cheese" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
228
- { id: 4, parentId: 2, mutation: "todoCompleteds", args: { ids: ["A", "B", "C"] }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ↖todo-exists-B ↖todo-is-writeable-B=true ↖todo-exists-C ↖todo-is-writeable-C=true +todo-completed-A=true +todo-completed-B=true +todo-completed-C=true" }
229
- { id: 6, parentId: 4, mutation: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
225
+ { id: 1, parentId: 0, name: "createTodo", args: { id: "A", text: "buy milk" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
226
+ { id: 2, parentId: 1, name: "createTodo", args: { id: "B", text: "buy bread" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-B +todo-is-writeable-B=true +todo-completed-B=false" }
227
+ { id: 3, parentId: 2, name: "createTodo", args: { id: "C", text: "buy cheese" }, clientId: "client-id", sessionId: "session-id", facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
228
+ { id: 5, parentId: 3, name: "todoCompleteds", args: { ids: ["A", "B", "C"] }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true ↖todo-exists-B ↖todo-is-writeable-B=true ↖todo-exists-C ↖todo-is-writeable-C=true +todo-completed-A=true +todo-completed-B=true +todo-completed-C=true" }
229
+ { id: 7, parentId: 5, name: "todoCompleted", args: { id: "A" }, clientId: "client-id", sessionId: "session-id", facts: "↖todo-exists-A ↖todo-is-writeable-A=true +todo-completed-A=true" }
230
230
  ]
231
231
  `)
232
232
  })
@@ -0,0 +1,219 @@
1
+ import { Schema } from '@livestore/utils/effect'
2
+
3
+ import type { EventDef } from '../../../schema/EventDef.js'
4
+ import { defineEvent, defineFacts } from '../../../schema/EventDef.js'
5
+ import * as EventId from '../../../schema/EventId.js'
6
+ import { factsSnapshotForDag, getFactsGroupForEventArgs } from '../facts.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'
10
+
11
+ /** Used for conflict detection and event history compaction */
12
+ export const facts = defineFacts({
13
+ todoExists: (id: string) => `todo-exists-${id}`,
14
+ todoIsWriteable: (id: string, writeable: boolean) => [`todo-is-writeable-${id}`, writeable],
15
+ todoCompleted: (id: string, completed: boolean) => [`todo-completed-${id}`, completed],
16
+ todoTextUpdated: (id: string) => `todo-text-updated-${id}`,
17
+ inputValue: (id: string) => `input-value-${id}`,
18
+ })
19
+
20
+ export const events = {
21
+ createTodo: defineEvent({
22
+ name: 'createTodo',
23
+ schema: Schema.Struct({ id: Schema.String, text: Schema.String }),
24
+ // 'INSERT INTO todos (id, text) VALUES ($id, $text)',
25
+ // {
26
+ facts: ({ id }) => ({
27
+ modify: {
28
+ set: [facts.todoExists(id), facts.todoIsWriteable(id, true), facts.todoCompleted(id, false)],
29
+ },
30
+ }),
31
+ }),
32
+ upsertTodo: defineEvent({
33
+ name: 'upsertTodo',
34
+ schema: Schema.Struct({ id: Schema.String, text: Schema.optional(Schema.String) }),
35
+ // 'INSERT INTO todos (id, text) VALUES ($id, $text) ON CONFLICT (id) DO UPDATE SET text = $text',
36
+ // {
37
+ facts: ({ id }, currentFacts) =>
38
+ // TODO enable an API along the lines of `map.has(key, value)`
39
+ currentFacts.has(facts.todoExists(id)) && currentFacts.get(facts.todoIsWriteable(id, true)[0]) === false
40
+ ? { require: [facts.todoExists(id), facts.todoIsWriteable(id, true)] }
41
+ : { modify: { set: [facts.todoExists(id), facts.todoIsWriteable(id, true), facts.todoTextUpdated(id)] } },
42
+ }),
43
+ todoCompleted: defineEvent({
44
+ name: 'todoCompleted',
45
+ schema: Schema.Struct({ id: Schema.String }),
46
+ // consider `RETURNING` to validate before applying facts
47
+ // 'UPDATE todos SET completed = true WHERE id = $id',
48
+ // {
49
+ // prewrite assertions from DB
50
+ // enables more concurrency
51
+ // turning database inside out
52
+ // similar to upsert semantics
53
+ facts: ({ id }) => ({
54
+ require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
55
+ modify: { set: [facts.todoCompleted(id, true)] },
56
+ }),
57
+ }),
58
+ todoUncompleted: defineEvent({
59
+ name: 'todoUncompleted',
60
+ schema: Schema.Struct({ id: Schema.String }),
61
+ // 'UPDATE todos SET completed = false WHERE id = $id',
62
+ // {
63
+ facts: ({ id }) => ({
64
+ require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
65
+ modify: { set: [facts.todoCompleted(id, false)] },
66
+ }),
67
+ }),
68
+ todoCompleteds: defineEvent({
69
+ name: 'todoCompleteds',
70
+ schema: Schema.Struct({ ids: Schema.Array(Schema.String) }),
71
+ // 'UPDATE todos SET completed = true WHERE id IN ($ids:csv)',
72
+ // {
73
+ facts: ({ ids }) => ({
74
+ require: ids.flatMap((id) => [facts.todoExists(id), facts.todoIsWriteable(id, true)]),
75
+ modify: { set: ids.map((id) => facts.todoCompleted(id, true)) },
76
+ }),
77
+ }),
78
+ toggleTodo: defineEvent({
79
+ name: 'toggleTodo',
80
+ schema: Schema.Struct({ id: Schema.String }),
81
+ // 'UPDATE todos SET completed = NOT completed WHERE id = $id',
82
+ // {
83
+ facts: ({ id }, currentFacts) => {
84
+ const currentIsCompleted = currentFacts.get(facts.todoCompleted(id, true)[0]) === true
85
+ return {
86
+ require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
87
+ modify: {
88
+ // remove: [facts.todoCompleted(id, currentIsCompleted)],
89
+ set: [facts.todoCompleted(id, !currentIsCompleted)],
90
+ },
91
+ }
92
+ },
93
+ }),
94
+ setReadonlyTodo: defineEvent({
95
+ name: 'setReadonlyTodo',
96
+ schema: Schema.Struct({ id: Schema.String, readonly: Schema.Boolean }),
97
+ // 'UPDATE todos SET readonly = $readonly WHERE id = $id',
98
+ // {
99
+ facts: ({ id, readonly }) => ({
100
+ require: [facts.todoExists(id)],
101
+ modify: { set: [facts.todoIsWriteable(id, !readonly)] },
102
+ }),
103
+ }),
104
+ setTextTodo: defineEvent({
105
+ name: 'setTextTodo',
106
+ schema: Schema.Struct({ id: Schema.String, text: Schema.String }),
107
+ // 'UPDATE todos SET text = $text WHERE id = $id',
108
+ // {
109
+ facts: ({ id }) => ({
110
+ require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
111
+ modify: { set: [facts.todoTextUpdated(id)] },
112
+ }),
113
+ }),
114
+ setInputValue: defineEvent({
115
+ name: 'setInputValue',
116
+ schema: Schema.Struct({ id: Schema.String, text: Schema.String }),
117
+ // 'UPDATE todos SET text = $text WHERE id = $id',
118
+ // {
119
+ clientOnly: true,
120
+ facts: ({ id }) => ({ modify: { set: [facts.inputValue(id)] } }),
121
+ }),
122
+ }
123
+
124
+ export type PartialEvent = { name: string; args: any }
125
+
126
+ export const toEventNodes = (
127
+ partialEvents: PartialEvent[],
128
+ eventDefs: Record<string, EventDef.Any>,
129
+ clientId: string,
130
+ sessionId: string | undefined,
131
+ ): HistoryDagNode[] => {
132
+ const nodesAcc: HistoryDagNode[] = [rootEventNode]
133
+
134
+ let currentEventId: EventId.EventId = EventId.ROOT
135
+
136
+ const eventNodes = partialEvents.map((partialEvent) => {
137
+ const eventDef = eventDefs[partialEvent.name]!
138
+ const eventId = EventId.nextPair(currentEventId, eventDef.options.clientOnly).id
139
+ currentEventId = eventId
140
+
141
+ const factsSnapshot = factsSnapshotForDag(historyDagFromNodes(nodesAcc, { skipFactsCheck: true }), undefined)
142
+ // console.log('factsSnapshot', eventId, factsSnapshot)
143
+ // const depRead: EventDefFactsSnapshot = new Map<string, any>()
144
+ // const factsSnapshotProxy = new Proxy(factsSnapshot, {
145
+ // get: (target, prop) => {
146
+ // if (prop === 'has') {
147
+ // return (key: string) => {
148
+ // depRead.set(key, EMPTY_FACT_VALUE)
149
+ // return target.has(key)
150
+ // }
151
+ // } else if (prop === 'get') {
152
+ // return (key: string) => {
153
+ // depRead.set(key, EMPTY_FACT_VALUE)
154
+ // return target.get(key)
155
+ // }
156
+ // }
157
+
158
+ // notYetImplemented(`toEventNodes: ${prop.toString()} is not yet implemented`)
159
+ // },
160
+ // })
161
+
162
+ // const factsRes = eventDef.options.facts?.(partialEvent.args, factsSnapshotProxy)
163
+ // console.log('factsRes', factsRes?.modify, factsRes?.require)
164
+ // const iterableToMap = (iterable: Iterable<EventDefFactInput>) => {
165
+ // const map = new Map()
166
+ // for (const item of iterable) {
167
+ // if (typeof item === 'string') {
168
+ // map.set(item, EMPTY_FACT_VALUE)
169
+ // } else {
170
+ // map.set(item[0], item[1])
171
+ // }
172
+ // }
173
+ // return map
174
+ // }
175
+ // const facts = {
176
+ // modifyAdd: factsRes?.modify.add ? iterableToMap(factsRes.modify.add) : new Map(),
177
+ // modifyRemove: factsRes?.modify.remove ? iterableToMap(factsRes.modify.remove) : new Map(),
178
+ // depRequire: factsRes?.require ? iterableToMap(factsRes.require) : new Map(),
179
+ // depRead,
180
+ // } satisfies EventDefFactsGroup
181
+
182
+ // applyFactGroup(facts, factsSnapshot)
183
+
184
+ const facts = getFactsGroupForEventArgs({
185
+ factsCallback: eventDef.options.facts,
186
+ args: partialEvent.args,
187
+ currentFacts: factsSnapshot,
188
+ })
189
+
190
+ const node = {
191
+ id: eventId,
192
+ parentId: getParentId(eventId),
193
+ name: partialEvent.name,
194
+ args: partialEvent.args,
195
+ factsGroup: facts,
196
+ clientId,
197
+ sessionId,
198
+ } satisfies HistoryDagNode
199
+ nodesAcc.push(node)
200
+ return node
201
+ })
202
+
203
+ eventNodes.unshift(rootEventNode as never)
204
+
205
+ // console.log('eventNodes', eventNodes)
206
+
207
+ return eventNodes
208
+ }
209
+
210
+ const getParentId = (eventId: EventId.EventId): EventId.EventId => {
211
+ const globalParentId = eventId.global
212
+ const clientParentId = eventId.client - 1
213
+
214
+ if (clientParentId < 0) {
215
+ return EventId.make({ global: globalParentId - 1, client: EventId.clientDefault })
216
+ }
217
+
218
+ return EventId.make({ global: globalParentId, client: clientParentId })
219
+ }
@@ -1 +1 @@
1
- export * from './mutation-fixtures.js'
1
+ export * from './event-fixtures.js'
package/src/sync/sync.ts CHANGED
@@ -4,7 +4,7 @@ import { Schema } from '@livestore/utils/effect'
4
4
  import type { UnexpectedError } from '../adapter-types.js'
5
5
  import type { InitialSyncOptions } from '../leader-thread/types.js'
6
6
  import * as EventId from '../schema/EventId.js'
7
- import type * as MutationEvent from '../schema/MutationEvent.js'
7
+ import type * as LiveStoreEvent from '../schema/LiveStoreEvent.js'
8
8
 
9
9
  /**
10
10
  * Those arguments can be used to implement multi-tenancy etc and are passed in from the store.
@@ -48,7 +48,7 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
48
48
  ) => Stream.Stream<
49
49
  {
50
50
  batch: ReadonlyArray<{
51
- mutationEventEncoded: MutationEvent.AnyEncodedGlobal
51
+ eventEncoded: LiveStoreEvent.AnyEncodedGlobal
52
52
  metadata: Option.Option<TSyncMetadata>
53
53
  }>
54
54
  remaining: number
@@ -63,7 +63,7 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
63
63
  * - Number of events: 1-100
64
64
  * - event ids must be in ascending order
65
65
  * */
66
- batch: ReadonlyArray<MutationEvent.AnyEncodedGlobal>,
66
+ batch: ReadonlyArray<LiveStoreEvent.AnyEncodedGlobal>,
67
67
  ) => Effect.Effect<void, IsOfflineError | InvalidPushError, HttpClient.HttpClient>
68
68
  isConnected: SubscriptionRef.SubscriptionRef<boolean>
69
69
  /**
@@ -2,10 +2,10 @@
2
2
  import { describe, expect, it } from 'vitest'
3
3
 
4
4
  import * as EventId from '../schema/EventId.js'
5
- import * as MutationEvent from '../schema/MutationEvent.js'
5
+ import * as LiveStoreEvent from '../schema/LiveStoreEvent.js'
6
6
  import * as SyncState from './syncstate.js'
7
7
 
8
- class TestEvent extends MutationEvent.EncodedWithMeta {
8
+ class TestEvent extends LiveStoreEvent.EncodedWithMeta {
9
9
  constructor(
10
10
  id: EventId.EventId | typeof EventId.EventId.Encoded,
11
11
  parentId: EventId.EventId,
@@ -15,7 +15,7 @@ class TestEvent extends MutationEvent.EncodedWithMeta {
15
15
  super({
16
16
  id: EventId.make(id),
17
17
  parentId: EventId.make(parentId),
18
- mutation: 'a',
18
+ name: 'a',
19
19
  args: payload,
20
20
  clientId: 'static-local-id',
21
21
  sessionId: 'static-session-id',
@@ -39,9 +39,9 @@ const e1_3 = new TestEvent({ global: 1, client: 3 }, e1_2.id, 'a', true)
39
39
  const e2_0 = new TestEvent({ global: 2, client: 0 }, e1_0.id, 'a', false)
40
40
  const e2_1 = new TestEvent({ global: 2, client: 1 }, e2_0.id, 'a', true)
41
41
 
42
- const isEqualEvent = MutationEvent.isEqualEncoded
42
+ const isEqualEvent = LiveStoreEvent.isEqualEncoded
43
43
 
44
- const isClientEvent = (event: MutationEvent.EncodedWithMeta) => (event as TestEvent).isLocal
44
+ const isClientEvent = (event: LiveStoreEvent.EncodedWithMeta) => (event as TestEvent).isLocal
45
45
 
46
46
  describe('syncstate', () => {
47
47
  describe('merge', () => {
@@ -460,14 +460,14 @@ describe('syncstate', () => {
460
460
  })
461
461
 
462
462
  const expectEventArraysEqual = (
463
- actual: ReadonlyArray<MutationEvent.EncodedWithMeta>,
464
- expected: ReadonlyArray<MutationEvent.EncodedWithMeta>,
463
+ actual: ReadonlyArray<LiveStoreEvent.EncodedWithMeta>,
464
+ expected: ReadonlyArray<LiveStoreEvent.EncodedWithMeta>,
465
465
  ) => {
466
466
  expect(actual.length).toBe(expected.length)
467
467
  actual.forEach((event, i) => {
468
468
  expect(event.id).toStrictEqual(expected[i]!.id)
469
469
  expect(event.parentId).toStrictEqual(expected[i]!.parentId)
470
- expect(event.mutation).toStrictEqual(expected[i]!.mutation)
470
+ expect(event.name).toStrictEqual(expected[i]!.name)
471
471
  expect(event.args).toStrictEqual(expected[i]!.args)
472
472
  })
473
473
  }