@livestore/common 0.3.0-dev.27 → 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.
- package/dist/.tsbuildinfo +1 -1
- package/dist/__tests__/fixture.d.ts +83 -221
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +33 -11
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/adapter-types.d.ts +22 -15
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +15 -2
- package/dist/adapter-types.js.map +1 -1
- package/dist/bounded-collections.d.ts +1 -1
- package/dist/bounded-collections.d.ts.map +1 -1
- package/dist/debug-info.d.ts.map +1 -1
- package/dist/debug-info.js +1 -0
- package/dist/debug-info.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +21 -21
- package/dist/devtools/devtools-messages-common.d.ts +6 -6
- package/dist/devtools/devtools-messages-leader.d.ts +45 -45
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
- package/dist/devtools/devtools-messages-leader.js +11 -11
- package/dist/devtools/devtools-messages-leader.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts +25 -12
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.js +125 -89
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
- package/dist/leader-thread/{apply-mutation.d.ts → apply-event.d.ts} +7 -7
- package/dist/leader-thread/apply-event.d.ts.map +1 -0
- package/dist/leader-thread/apply-event.js +103 -0
- package/dist/leader-thread/apply-event.js.map +1 -0
- package/dist/leader-thread/eventlog.d.ts +27 -0
- package/dist/leader-thread/eventlog.d.ts.map +1 -0
- package/dist/leader-thread/eventlog.js +123 -0
- package/dist/leader-thread/eventlog.js.map +1 -0
- package/dist/leader-thread/leader-worker-devtools.js +18 -18
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +16 -4
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +23 -16
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mod.d.ts +1 -1
- package/dist/leader-thread/mod.d.ts.map +1 -1
- package/dist/leader-thread/mod.js +1 -1
- package/dist/leader-thread/mod.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +6 -8
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/types.d.ts +11 -11
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/materializer-helper.d.ts +23 -0
- package/dist/materializer-helper.d.ts.map +1 -0
- package/dist/materializer-helper.js +70 -0
- package/dist/materializer-helper.js.map +1 -0
- package/dist/query-builder/api.d.ts +58 -53
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/query-builder/api.js +3 -5
- package/dist/query-builder/api.js.map +1 -1
- package/dist/query-builder/astToSql.d.ts.map +1 -1
- package/dist/query-builder/astToSql.js +59 -37
- package/dist/query-builder/astToSql.js.map +1 -1
- package/dist/query-builder/impl.d.ts +2 -3
- package/dist/query-builder/impl.d.ts.map +1 -1
- package/dist/query-builder/impl.js +48 -46
- package/dist/query-builder/impl.js.map +1 -1
- package/dist/query-builder/impl.test.d.ts +86 -1
- package/dist/query-builder/impl.test.d.ts.map +1 -1
- package/dist/query-builder/impl.test.js +244 -36
- package/dist/query-builder/impl.test.js.map +1 -1
- package/dist/rehydrate-from-eventlog.d.ts +14 -0
- package/dist/rehydrate-from-eventlog.d.ts.map +1 -0
- package/dist/{rehydrate-from-mutationlog.js → rehydrate-from-eventlog.js} +25 -26
- package/dist/rehydrate-from-eventlog.js.map +1 -0
- package/dist/schema/EventDef.d.ts +136 -0
- package/dist/schema/EventDef.d.ts.map +1 -0
- package/dist/schema/EventDef.js +58 -0
- package/dist/schema/EventDef.js.map +1 -0
- package/dist/schema/EventId.d.ts +2 -2
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +8 -2
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/{MutationEvent.d.ts → LiveStoreEvent.d.ts} +56 -56
- package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
- package/dist/schema/{MutationEvent.js → LiveStoreEvent.js} +25 -25
- package/dist/schema/LiveStoreEvent.js.map +1 -0
- package/dist/schema/client-document-def.d.ts +223 -0
- package/dist/schema/client-document-def.d.ts.map +1 -0
- package/dist/schema/client-document-def.js +170 -0
- package/dist/schema/client-document-def.js.map +1 -0
- package/dist/schema/client-document-def.test.d.ts +2 -0
- package/dist/schema/client-document-def.test.d.ts.map +1 -0
- package/dist/schema/client-document-def.test.js +201 -0
- package/dist/schema/client-document-def.test.js.map +1 -0
- package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -1
- package/dist/schema/events.d.ts +2 -0
- package/dist/schema/events.d.ts.map +1 -0
- package/dist/schema/events.js +2 -0
- package/dist/schema/events.js.map +1 -0
- package/dist/schema/mod.d.ts +4 -3
- package/dist/schema/mod.d.ts.map +1 -1
- package/dist/schema/mod.js +4 -3
- package/dist/schema/mod.js.map +1 -1
- package/dist/schema/schema.d.ts +27 -23
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +45 -43
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/sqlite-state.d.ts +12 -0
- package/dist/schema/sqlite-state.d.ts.map +1 -0
- package/dist/schema/sqlite-state.js +36 -0
- package/dist/schema/sqlite-state.js.map +1 -0
- package/dist/schema/system-tables.d.ts +67 -98
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +62 -48
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema/table-def.d.ts +26 -96
- package/dist/schema/table-def.d.ts.map +1 -1
- package/dist/schema/table-def.js +16 -64
- package/dist/schema/table-def.js.map +1 -1
- package/dist/schema/view.d.ts +3 -0
- package/dist/schema/view.d.ts.map +1 -0
- package/dist/schema/view.js +3 -0
- package/dist/schema/view.js.map +1 -0
- package/dist/schema-management/common.d.ts +4 -4
- package/dist/schema-management/common.d.ts.map +1 -1
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +6 -6
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts +3 -3
- package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
- package/dist/schema-management/validate-mutation-defs.js +17 -17
- package/dist/schema-management/validate-mutation-defs.js.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.d.ts +7 -7
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.js +31 -30
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
- package/dist/sync/next/facts.d.ts +19 -19
- package/dist/sync/next/facts.d.ts.map +1 -1
- package/dist/sync/next/facts.js +2 -2
- package/dist/sync/next/facts.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +3 -3
- package/dist/sync/next/history-dag-common.d.ts.map +1 -1
- package/dist/sync/next/history-dag-common.js +1 -1
- package/dist/sync/next/history-dag-common.js.map +1 -1
- package/dist/sync/next/history-dag.js +1 -1
- package/dist/sync/next/history-dag.js.map +1 -1
- package/dist/sync/next/rebase-events.d.ts +7 -7
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +5 -5
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.calculator.test.js +38 -33
- package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +71 -71
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +29 -29
- package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
- package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +60 -25
- package/dist/sync/next/test/event-fixtures.js.map +1 -0
- package/dist/sync/next/test/mod.d.ts +1 -1
- package/dist/sync/next/test/mod.d.ts.map +1 -1
- package/dist/sync/next/test/mod.js +1 -1
- package/dist/sync/next/test/mod.js.map +1 -1
- package/dist/sync/sync.d.ts +3 -3
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/syncstate.d.ts +32 -32
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +31 -25
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +165 -175
- package/dist/sync/syncstate.test.js.map +1 -1
- package/dist/sync/validate-push-payload.d.ts +2 -2
- package/dist/sync/validate-push-payload.d.ts.map +1 -1
- package/dist/sync/validate-push-payload.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
- package/src/__tests__/fixture.ts +36 -15
- package/src/adapter-types.ts +23 -16
- package/src/debug-info.ts +1 -0
- package/src/devtools/devtools-messages-leader.ts +13 -13
- package/src/index.ts +2 -5
- package/src/leader-thread/LeaderSyncProcessor.ts +183 -122
- package/src/leader-thread/{apply-mutation.ts → apply-event.ts} +50 -74
- package/src/leader-thread/eventlog.ts +199 -0
- package/src/leader-thread/leader-worker-devtools.ts +18 -18
- package/src/leader-thread/make-leader-thread-layer.ts +51 -29
- package/src/leader-thread/mod.ts +1 -1
- package/src/leader-thread/recreate-db.ts +6 -9
- package/src/leader-thread/types.ts +12 -12
- package/src/materializer-helper.ts +110 -0
- package/src/query-builder/api.ts +79 -105
- package/src/query-builder/astToSql.ts +68 -39
- package/src/query-builder/impl.test.ts +264 -42
- package/src/query-builder/impl.ts +72 -56
- package/src/{rehydrate-from-mutationlog.ts → rehydrate-from-eventlog.ts} +33 -40
- package/src/schema/EventDef.ts +216 -0
- package/src/schema/EventId.ts +11 -3
- package/src/schema/{MutationEvent.ts → LiveStoreEvent.ts} +68 -69
- package/src/schema/client-document-def.test.ts +239 -0
- package/src/schema/client-document-def.ts +444 -0
- package/src/schema/db-schema/dsl/mod.ts +0 -1
- package/src/schema/events.ts +1 -0
- package/src/schema/mod.ts +4 -3
- package/src/schema/schema.ts +79 -69
- package/src/schema/sqlite-state.ts +62 -0
- package/src/schema/system-tables.ts +42 -53
- package/src/schema/table-def.ts +53 -209
- package/src/schema/view.ts +2 -0
- package/src/schema-management/common.ts +4 -4
- package/src/schema-management/migrations.ts +8 -9
- package/src/schema-management/validate-mutation-defs.ts +22 -24
- package/src/sync/ClientSessionSyncProcessor.ts +37 -36
- package/src/sync/next/facts.ts +31 -32
- package/src/sync/next/history-dag-common.ts +4 -4
- package/src/sync/next/history-dag.ts +1 -1
- package/src/sync/next/rebase-events.ts +13 -13
- package/src/sync/next/test/compact-events.calculator.test.ts +45 -45
- package/src/sync/next/test/compact-events.test.ts +73 -73
- package/src/sync/next/test/event-fixtures.ts +219 -0
- package/src/sync/next/test/mod.ts +1 -1
- package/src/sync/sync.ts +3 -3
- package/src/sync/syncstate.test.ts +168 -179
- package/src/sync/syncstate.ts +48 -38
- package/src/sync/validate-push-payload.ts +2 -2
- package/src/version.ts +1 -1
- package/tmp/pack.tgz +0 -0
- package/tsconfig.json +1 -0
- package/dist/derived-mutations.d.ts +0 -109
- package/dist/derived-mutations.d.ts.map +0 -1
- package/dist/derived-mutations.js +0 -54
- package/dist/derived-mutations.js.map +0 -1
- package/dist/derived-mutations.test.d.ts +0 -2
- package/dist/derived-mutations.test.d.ts.map +0 -1
- package/dist/derived-mutations.test.js +0 -93
- package/dist/derived-mutations.test.js.map +0 -1
- package/dist/init-singleton-tables.d.ts +0 -4
- package/dist/init-singleton-tables.d.ts.map +0 -1
- package/dist/init-singleton-tables.js +0 -16
- package/dist/init-singleton-tables.js.map +0 -1
- package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
- package/dist/leader-thread/apply-mutation.js +0 -122
- package/dist/leader-thread/apply-mutation.js.map +0 -1
- package/dist/leader-thread/mutationlog.d.ts +0 -27
- package/dist/leader-thread/mutationlog.d.ts.map +0 -1
- package/dist/leader-thread/mutationlog.js +0 -124
- package/dist/leader-thread/mutationlog.js.map +0 -1
- package/dist/leader-thread/pull-queue-set.d.ts +0 -7
- package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
- package/dist/leader-thread/pull-queue-set.js +0 -38
- package/dist/leader-thread/pull-queue-set.js.map +0 -1
- package/dist/mutation.d.ts +0 -20
- package/dist/mutation.d.ts.map +0 -1
- package/dist/mutation.js +0 -68
- package/dist/mutation.js.map +0 -1
- package/dist/query-info.d.ts +0 -41
- package/dist/query-info.d.ts.map +0 -1
- package/dist/query-info.js +0 -7
- package/dist/query-info.js.map +0 -1
- package/dist/rehydrate-from-mutationlog.d.ts +0 -15
- package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
- package/dist/rehydrate-from-mutationlog.js.map +0 -1
- package/dist/schema/MutationEvent.d.ts.map +0 -1
- package/dist/schema/MutationEvent.js.map +0 -1
- package/dist/schema/mutations.d.ts +0 -115
- package/dist/schema/mutations.d.ts.map +0 -1
- package/dist/schema/mutations.js +0 -42
- package/dist/schema/mutations.js.map +0 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
- package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
- package/src/derived-mutations.test.ts +0 -101
- package/src/derived-mutations.ts +0 -170
- package/src/init-singleton-tables.ts +0 -24
- package/src/leader-thread/mutationlog.ts +0 -202
- package/src/mutation.ts +0 -108
- package/src/query-info.ts +0 -83
- package/src/schema/mutations.ts +0 -193
- package/src/sync/next/test/mutation-fixtures.ts +0 -228
@@ -1,11 +1,11 @@
|
|
1
|
-
import type {
|
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 {
|
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:
|
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,
|
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
|
-
|
87
|
-
|
88
|
-
|
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:
|
94
|
-
{ id:
|
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
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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:
|
110
|
-
{ id:
|
111
|
-
{ id:
|
112
|
-
{ id:
|
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
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
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:
|
130
|
-
{ id:
|
131
|
-
{ id:
|
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
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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:
|
147
|
-
{ id:
|
148
|
-
{ id:
|
149
|
-
{ id:
|
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
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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:
|
166
|
-
{ id:
|
167
|
-
{ id:
|
168
|
-
{ id:
|
169
|
-
{ id:
|
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
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
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:
|
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
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
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:
|
204
|
-
{ id:
|
205
|
-
{ id:
|
206
|
-
{ id:
|
207
|
-
{ id:
|
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
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
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:
|
226
|
-
{ id:
|
227
|
-
{ id:
|
228
|
-
{ id:
|
229
|
-
{ id:
|
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 './
|
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
|
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
|
-
|
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<
|
66
|
+
batch: ReadonlyArray<LiveStoreEvent.AnyEncodedGlobal>,
|
67
67
|
) => Effect.Effect<void, IsOfflineError | InvalidPushError, HttpClient.HttpClient>
|
68
68
|
isConnected: SubscriptionRef.SubscriptionRef<boolean>
|
69
69
|
/**
|