@livestore/common 0.3.0-dev.2 → 0.3.0-dev.22
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 +21 -21
- package/dist/adapter-types.d.ts +97 -53
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +17 -0
- 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 +1 -1
- package/dist/debug-info.d.ts.map +1 -1
- package/dist/derived-mutations.d.ts +5 -5
- package/dist/derived-mutations.d.ts.map +1 -1
- package/dist/derived-mutations.js +3 -3
- package/dist/derived-mutations.js.map +1 -1
- package/dist/derived-mutations.test.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +389 -0
- package/dist/devtools/devtools-messages-client-session.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-client-session.js +96 -0
- package/dist/devtools/devtools-messages-client-session.js.map +1 -0
- package/dist/devtools/devtools-messages-common.d.ts +61 -0
- package/dist/devtools/devtools-messages-common.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-common.js +54 -0
- package/dist/devtools/devtools-messages-common.js.map +1 -0
- package/dist/devtools/devtools-messages-leader.d.ts +393 -0
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-leader.js +148 -0
- package/dist/devtools/devtools-messages-leader.js.map +1 -0
- package/dist/devtools/devtools-messages.d.ts +3 -592
- package/dist/devtools/devtools-messages.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.js +3 -171
- package/dist/devtools/devtools-messages.js.map +1 -1
- package/dist/devtools/devtools-sessioninfo.d.ts +28 -0
- package/dist/devtools/devtools-sessioninfo.d.ts.map +1 -0
- package/dist/devtools/devtools-sessioninfo.js +34 -0
- package/dist/devtools/devtools-sessioninfo.js.map +1 -0
- package/dist/devtools/mod.d.ts +39 -0
- package/dist/devtools/mod.d.ts.map +1 -0
- package/dist/devtools/mod.js +27 -0
- package/dist/devtools/mod.js.map +1 -0
- package/dist/index.d.ts +2 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/init-singleton-tables.d.ts +2 -2
- package/dist/init-singleton-tables.d.ts.map +1 -1
- package/dist/init-singleton-tables.js.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts +39 -0
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -0
- package/dist/leader-thread/LeaderSyncProcessor.js +527 -0
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -0
- package/dist/leader-thread/apply-mutation.d.ts +5 -2
- package/dist/leader-thread/apply-mutation.d.ts.map +1 -1
- package/dist/leader-thread/apply-mutation.js +55 -35
- package/dist/leader-thread/apply-mutation.js.map +1 -1
- package/dist/leader-thread/connection.d.ts +34 -6
- package/dist/leader-thread/connection.d.ts.map +1 -1
- package/dist/leader-thread/connection.js +22 -7
- package/dist/leader-thread/connection.js.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.d.ts +1 -1
- package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.js +147 -124
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +12 -11
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +55 -18
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mutationlog.d.ts +6 -19
- package/dist/leader-thread/mutationlog.d.ts.map +1 -1
- package/dist/leader-thread/mutationlog.js +12 -9
- package/dist/leader-thread/mutationlog.js.map +1 -1
- package/dist/leader-thread/pull-queue-set.d.ts +3 -3
- package/dist/leader-thread/pull-queue-set.d.ts.map +1 -1
- package/dist/leader-thread/pull-queue-set.js +9 -0
- package/dist/leader-thread/pull-queue-set.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts +4 -2
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +32 -21
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/shutdown-channel.d.ts +2 -5
- package/dist/leader-thread/shutdown-channel.d.ts.map +1 -1
- package/dist/leader-thread/shutdown-channel.js +2 -4
- package/dist/leader-thread/shutdown-channel.js.map +1 -1
- package/dist/leader-thread/types.d.ts +58 -26
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/leader-thread/types.js +1 -3
- package/dist/leader-thread/types.js.map +1 -1
- package/dist/mutation.d.ts +9 -2
- package/dist/mutation.d.ts.map +1 -1
- package/dist/mutation.js +5 -5
- package/dist/mutation.js.map +1 -1
- package/dist/otel.d.ts +2 -0
- package/dist/otel.d.ts.map +1 -1
- package/dist/otel.js +5 -0
- package/dist/otel.js.map +1 -1
- package/dist/query-builder/api.d.ts +3 -3
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/query-builder/impl.d.ts +4 -4
- package/dist/query-builder/impl.d.ts.map +1 -1
- package/dist/query-builder/impl.js.map +1 -1
- package/dist/query-builder/impl.test.js +16 -1
- package/dist/query-builder/impl.test.js.map +1 -1
- package/dist/query-info.d.ts +3 -3
- package/dist/query-info.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.d.ts +5 -5
- package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.js +23 -27
- package/dist/rehydrate-from-mutationlog.js.map +1 -1
- package/dist/schema/EventId.d.ts +27 -16
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +36 -11
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/EventId.test.d.ts +2 -0
- package/dist/schema/EventId.test.d.ts.map +1 -0
- package/dist/schema/EventId.test.js +11 -0
- package/dist/schema/EventId.test.js.map +1 -0
- package/dist/schema/MutationEvent.d.ts +76 -82
- package/dist/schema/MutationEvent.d.ts.map +1 -1
- package/dist/schema/MutationEvent.js +53 -20
- package/dist/schema/MutationEvent.js.map +1 -1
- package/dist/schema/db-schema/ast/sqlite.d.ts +69 -0
- package/dist/schema/db-schema/ast/sqlite.d.ts.map +1 -0
- package/dist/schema/db-schema/ast/sqlite.js +71 -0
- package/dist/schema/db-schema/ast/sqlite.js.map +1 -0
- package/dist/schema/db-schema/ast/validate.d.ts +3 -0
- package/dist/schema/db-schema/ast/validate.d.ts.map +1 -0
- package/dist/schema/db-schema/ast/validate.js +12 -0
- package/dist/schema/db-schema/ast/validate.js.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.d.ts +90 -0
- package/dist/schema/db-schema/dsl/field-defs.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.js +87 -0
- package/dist/schema/db-schema/dsl/field-defs.js.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.test.d.ts +2 -0
- package/dist/schema/db-schema/dsl/field-defs.test.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.test.js +29 -0
- package/dist/schema/db-schema/dsl/field-defs.test.js.map +1 -0
- package/dist/schema/db-schema/dsl/mod.d.ts +88 -0
- package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/mod.js +35 -0
- package/dist/schema/db-schema/dsl/mod.js.map +1 -0
- package/dist/schema/db-schema/hash.d.ts +2 -0
- package/dist/schema/db-schema/hash.d.ts.map +1 -0
- package/dist/schema/db-schema/hash.js +14 -0
- package/dist/schema/db-schema/hash.js.map +1 -0
- package/dist/schema/db-schema/mod.d.ts +3 -0
- package/dist/schema/db-schema/mod.d.ts.map +1 -0
- package/dist/schema/db-schema/mod.js +3 -0
- package/dist/schema/db-schema/mod.js.map +1 -0
- package/dist/schema/mod.d.ts +1 -0
- package/dist/schema/mod.d.ts.map +1 -1
- package/dist/schema/mod.js +1 -0
- package/dist/schema/mod.js.map +1 -1
- package/dist/schema/mutations.d.ts +8 -9
- package/dist/schema/mutations.d.ts.map +1 -1
- package/dist/schema/mutations.js +2 -2
- package/dist/schema/mutations.js.map +1 -1
- package/dist/schema/schema-helpers.d.ts.map +1 -1
- package/dist/schema/schema-helpers.js +1 -1
- package/dist/schema/schema-helpers.js.map +1 -1
- package/dist/schema/schema.d.ts +5 -2
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +20 -9
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/system-tables.d.ts +65 -47
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +24 -13
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema/table-def.d.ts +18 -24
- package/dist/schema/table-def.d.ts.map +1 -1
- package/dist/schema/table-def.js +3 -4
- package/dist/schema/table-def.js.map +1 -1
- package/dist/schema-management/common.d.ts +3 -3
- package/dist/schema-management/common.d.ts.map +1 -1
- package/dist/schema-management/common.js.map +1 -1
- package/dist/schema-management/migrations.d.ts +6 -6
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +13 -8
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
- package/dist/schema-management/validate-mutation-defs.js +2 -2
- package/dist/schema-management/validate-mutation-defs.js.map +1 -1
- package/dist/sql-queries/misc.d.ts.map +1 -1
- package/dist/sql-queries/sql-queries.d.ts +1 -1
- package/dist/sql-queries/sql-queries.d.ts.map +1 -1
- package/dist/sql-queries/sql-queries.js.map +1 -1
- package/dist/sql-queries/sql-query-builder.d.ts +1 -1
- package/dist/sql-queries/sql-query-builder.d.ts.map +1 -1
- package/dist/sql-queries/sql-query-builder.js.map +1 -1
- package/dist/sql-queries/types.d.ts +2 -1
- package/dist/sql-queries/types.d.ts.map +1 -1
- package/dist/sql-queries/types.js.map +1 -1
- package/dist/sync/{client-session-sync-processor.d.ts → ClientSessionSyncProcessor.d.ts} +25 -14
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -0
- package/dist/sync/ClientSessionSyncProcessor.js +199 -0
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -0
- package/dist/sync/index.d.ts +1 -1
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +1 -1
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/next/compact-events.d.ts.map +1 -1
- package/dist/sync/next/facts.d.ts.map +1 -1
- package/dist/sync/next/facts.js +1 -1
- package/dist/sync/next/facts.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +3 -4
- package/dist/sync/next/history-dag-common.d.ts.map +1 -1
- package/dist/sync/next/history-dag-common.js +3 -1
- package/dist/sync/next/history-dag-common.js.map +1 -1
- package/dist/sync/next/history-dag.d.ts.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 +6 -4
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +6 -3
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.calculator.test.js +12 -12
- package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +43 -43
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts +4 -4
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.js +12 -16
- package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
- package/dist/sync/sync.d.ts +31 -16
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/sync.js +7 -3
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/syncstate.d.ts +177 -44
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +188 -56
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +162 -92
- 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 +2 -2
- package/dist/sync/validate-push-payload.js.map +1 -1
- package/dist/util.d.ts +2 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +6 -6
- package/src/adapter-types.ts +80 -56
- package/src/derived-mutations.test.ts +1 -1
- package/src/derived-mutations.ts +13 -9
- package/src/devtools/devtools-messages-client-session.ts +141 -0
- package/src/devtools/devtools-messages-common.ts +106 -0
- package/src/devtools/devtools-messages-leader.ts +192 -0
- package/src/devtools/devtools-messages.ts +3 -243
- package/src/devtools/devtools-sessioninfo.ts +99 -0
- package/src/devtools/mod.ts +36 -0
- package/src/index.ts +2 -8
- package/src/init-singleton-tables.ts +2 -2
- package/src/leader-thread/LeaderSyncProcessor.ts +833 -0
- package/src/leader-thread/apply-mutation.ts +87 -43
- package/src/leader-thread/connection.ts +54 -9
- package/src/leader-thread/leader-worker-devtools.ts +199 -174
- package/src/leader-thread/make-leader-thread-layer.ts +89 -35
- package/src/leader-thread/mutationlog.ts +20 -14
- package/src/leader-thread/pull-queue-set.ts +10 -1
- package/src/leader-thread/recreate-db.ts +38 -25
- package/src/leader-thread/shutdown-channel.ts +2 -4
- package/src/leader-thread/types.ts +68 -34
- package/src/mutation.ts +17 -7
- package/src/otel.ts +8 -0
- package/src/query-builder/api.ts +4 -4
- package/src/query-builder/impl.test.ts +22 -1
- package/src/query-builder/impl.ts +2 -2
- package/src/query-info.ts +3 -3
- package/src/rehydrate-from-mutationlog.ts +28 -34
- package/src/schema/EventId.test.ts +12 -0
- package/src/schema/EventId.ts +49 -15
- package/src/schema/MutationEvent.ts +78 -31
- package/src/schema/db-schema/ast/sqlite.ts +142 -0
- package/src/schema/db-schema/ast/validate.ts +13 -0
- package/src/schema/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +206 -0
- package/src/schema/db-schema/dsl/field-defs.test.ts +35 -0
- package/src/schema/db-schema/dsl/field-defs.ts +242 -0
- package/src/schema/db-schema/dsl/mod.ts +195 -0
- package/src/schema/db-schema/hash.ts +14 -0
- package/src/schema/db-schema/mod.ts +2 -0
- package/src/schema/mod.ts +1 -0
- package/src/schema/mutations.ts +10 -20
- package/src/schema/schema-helpers.ts +1 -1
- package/src/schema/schema.ts +22 -10
- package/src/schema/system-tables.ts +24 -13
- package/src/schema/table-def.ts +17 -17
- package/src/schema-management/common.ts +3 -3
- package/src/schema-management/migrations.ts +19 -15
- package/src/schema-management/validate-mutation-defs.ts +2 -2
- package/src/sql-queries/sql-queries.ts +1 -1
- package/src/sql-queries/sql-query-builder.ts +1 -2
- package/src/sql-queries/types.ts +3 -1
- package/src/sync/ClientSessionSyncProcessor.ts +313 -0
- package/src/sync/index.ts +1 -1
- package/src/sync/next/facts.ts +1 -1
- package/src/sync/next/history-dag-common.ts +5 -1
- package/src/sync/next/history-dag.ts +1 -1
- package/src/sync/next/rebase-events.ts +13 -7
- package/src/sync/next/test/compact-events.calculator.test.ts +12 -12
- package/src/sync/next/test/compact-events.test.ts +43 -43
- package/src/sync/next/test/mutation-fixtures.ts +16 -19
- package/src/sync/sync.ts +26 -10
- package/src/sync/syncstate.test.ts +178 -98
- package/src/sync/syncstate.ts +170 -83
- package/src/sync/validate-push-payload.ts +7 -4
- package/src/version.ts +1 -1
- package/tmp/pack.tgz +0 -0
- package/tsconfig.json +1 -1
- package/dist/devtools/devtools-bridge.d.ts +0 -12
- package/dist/devtools/devtools-bridge.d.ts.map +0 -1
- package/dist/devtools/devtools-bridge.js +0 -2
- package/dist/devtools/devtools-bridge.js.map +0 -1
- package/dist/devtools/index.d.ts +0 -42
- package/dist/devtools/index.d.ts.map +0 -1
- package/dist/devtools/index.js +0 -48
- package/dist/devtools/index.js.map +0 -1
- package/dist/leader-thread/leader-sync-processor.d.ts +0 -47
- package/dist/leader-thread/leader-sync-processor.d.ts.map +0 -1
- package/dist/leader-thread/leader-sync-processor.js +0 -422
- package/dist/leader-thread/leader-sync-processor.js.map +0 -1
- package/dist/sync/client-session-sync-processor.d.ts.map +0 -1
- package/dist/sync/client-session-sync-processor.js +0 -131
- package/dist/sync/client-session-sync-processor.js.map +0 -1
- package/src/devtools/devtools-bridge.ts +0 -13
- package/src/devtools/index.ts +0 -48
- package/src/leader-thread/leader-sync-processor.ts +0 -666
- package/src/sync/client-session-sync-processor.ts +0 -207
@@ -37,7 +37,7 @@ const customStringify = (value: any): string => {
|
|
37
37
|
const valStr =
|
38
38
|
key === 'facts'
|
39
39
|
? `"${factsToString(val)}"`
|
40
|
-
: (key === 'id' || key === 'parentId') && Object.keys(val).length === 2 && val.
|
40
|
+
: (key === 'id' || key === 'parentId') && Object.keys(val).length === 2 && val.client === 0
|
41
41
|
? val.global
|
42
42
|
: customStringify(val)
|
43
43
|
|
@@ -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))
|
74
|
+
const dag = historyDagFromNodes(toEventNodes(events, mutations, 'client-id', 'session-id'))
|
75
75
|
const compacted = compactEvents(dag)
|
76
76
|
|
77
77
|
return Array.from(compacted.dag.nodeEntries())
|
@@ -81,17 +81,17 @@ const compact = (events: any[]) => {
|
|
81
81
|
}
|
82
82
|
|
83
83
|
describe('compactEvents todo app', () => {
|
84
|
-
it('
|
84
|
+
it('todoCompleted', () => {
|
85
85
|
const expected = compact([
|
86
86
|
mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
|
87
|
-
mutations.
|
88
|
-
mutations.
|
87
|
+
mutations.todoCompleted({ id: 'A' }), // 1
|
88
|
+
mutations.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" }, facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
|
94
|
-
{ id: 2, parentId: 0, mutation: "
|
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" }
|
95
95
|
]
|
96
96
|
`)
|
97
97
|
})
|
@@ -106,29 +106,29 @@ describe('compactEvents todo app', () => {
|
|
106
106
|
|
107
107
|
expect(expected).toMatchInlineSnapshot(`
|
108
108
|
[
|
109
|
-
{ id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
|
110
|
-
{ id: 1, parentId: 0, mutation: "toggleTodo", args: { id: "A" }, 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" }, 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" }, facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=true" }
|
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" }
|
113
113
|
]
|
114
114
|
`)
|
115
115
|
})
|
116
116
|
|
117
|
-
it('
|
117
|
+
it('todoCompleted / toggleTodo', () => {
|
118
118
|
const expected = compact([
|
119
119
|
mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
|
120
120
|
mutations.toggleTodo({ id: 'A' }), // 1
|
121
121
|
mutations.toggleTodo({ id: 'A' }), // 2
|
122
|
-
mutations.
|
123
|
-
mutations.
|
122
|
+
mutations.todoCompleted({ id: 'A' }), // 3
|
123
|
+
mutations.todoCompleted({ id: 'A' }), // 4
|
124
124
|
mutations.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" }, facts: "+todo-exists-A +todo-is-writeable-A=true +todo-completed-A=false" }
|
130
|
-
{ id: 4, parentId: 0, mutation: "
|
131
|
-
{ id: 5, parentId: 4, mutation: "toggleTodo", args: { id: "A" }, facts: "↖todo-exists-A ↖todo-is-writeable-A=true ?todo-completed-A +todo-completed-A=false" }
|
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" }
|
132
132
|
]
|
133
133
|
`)
|
134
134
|
})
|
@@ -143,10 +143,10 @@ describe('compactEvents todo app', () => {
|
|
143
143
|
|
144
144
|
expect(expected).toMatchInlineSnapshot(`
|
145
145
|
[
|
146
|
-
{ id: 0, parentId: -1, mutation: "createTodo", args: { id: "A", text: "buy milk" }, 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 }, facts: "↖todo-exists-A +todo-is-writeable-A=true" }
|
148
|
-
{ id: 2, parentId: 1, mutation: "setTextTodo", args: { id: "A", text: "buy soy milk" }, 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 }, facts: "↖todo-exists-A +todo-is-writeable-A=false" }
|
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" }
|
150
150
|
]
|
151
151
|
`)
|
152
152
|
})
|
@@ -155,18 +155,18 @@ describe('compactEvents todo app', () => {
|
|
155
155
|
const expected = compact([
|
156
156
|
mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
|
157
157
|
mutations.setReadonlyTodo({ id: 'A', readonly: false }), // 1
|
158
|
-
mutations.
|
158
|
+
mutations.todoCompleted({ id: 'A' }), // 2
|
159
159
|
mutations.setTextTodo({ id: 'A', text: 'buy soy milk' }), // 3
|
160
160
|
mutations.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" }, 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 }, facts: "↖todo-exists-A +todo-is-writeable-A=true" }
|
167
|
-
{ id: 2, parentId: 1, mutation: "
|
168
|
-
{ id: 3, parentId: 2, mutation: "setTextTodo", args: { id: "A", text: "buy soy milk" }, 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 }, facts: "↖todo-exists-A +todo-is-writeable-A=false" }
|
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" }
|
170
170
|
]
|
171
171
|
`)
|
172
172
|
})
|
@@ -188,45 +188,45 @@ describe('compactEvents todo app', () => {
|
|
188
188
|
`)
|
189
189
|
})
|
190
190
|
|
191
|
-
it('
|
191
|
+
it('todoCompleteds', () => {
|
192
192
|
const expected = compact([
|
193
193
|
mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
|
194
194
|
mutations.createTodo({ id: 'B', text: 'buy bread' }), // 1
|
195
195
|
mutations.createTodo({ id: 'C', text: 'buy cheese' }), // 2
|
196
|
-
mutations.
|
196
|
+
mutations.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 3
|
197
197
|
mutations.toggleTodo({ id: 'A' }), // 4
|
198
|
-
mutations.
|
198
|
+
mutations.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" }, 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" }, 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" }, facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
|
206
|
-
{ id: 3, parentId: 2, mutation: "
|
207
|
-
{ id: 5, parentId: 3, mutation: "
|
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" }
|
208
208
|
]
|
209
209
|
`)
|
210
210
|
})
|
211
211
|
|
212
|
-
it('
|
212
|
+
it('todoCompleteds 2', () => {
|
213
213
|
const expected = compact([
|
214
214
|
mutations.createTodo({ id: 'A', text: 'buy milk' }), // 0
|
215
215
|
mutations.createTodo({ id: 'B', text: 'buy bread' }), // 1
|
216
216
|
mutations.createTodo({ id: 'C', text: 'buy cheese' }), // 2
|
217
217
|
mutations.toggleTodo({ id: 'A' }), // 3
|
218
|
-
mutations.
|
218
|
+
mutations.todoCompleteds({ ids: ['A', 'B', 'C'] }), // 4
|
219
219
|
mutations.toggleTodo({ id: 'A' }), // 5
|
220
|
-
mutations.
|
220
|
+
mutations.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" }, 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" }, 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" }, facts: "+todo-exists-C +todo-is-writeable-C=true +todo-completed-C=false" }
|
228
|
-
{ id: 4, parentId: 2, mutation: "
|
229
|
-
{ id: 6, parentId: 4, mutation: "
|
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" }
|
230
230
|
]
|
231
231
|
`)
|
232
232
|
})
|
@@ -42,8 +42,8 @@ export const mutations = {
|
|
42
42
|
: { modify: { set: [facts.todoExists(id), facts.todoIsWriteable(id, true), facts.todoTextUpdated(id)] } },
|
43
43
|
},
|
44
44
|
),
|
45
|
-
|
46
|
-
'
|
45
|
+
todoCompleted: defineMutation(
|
46
|
+
'todoCompleted',
|
47
47
|
Schema.Struct({ id: Schema.String }),
|
48
48
|
// consider `RETURNING` to validate before applying facts
|
49
49
|
'UPDATE todos SET completed = true WHERE id = $id',
|
@@ -58,8 +58,8 @@ export const mutations = {
|
|
58
58
|
}),
|
59
59
|
},
|
60
60
|
),
|
61
|
-
|
62
|
-
'
|
61
|
+
todoUncompleted: defineMutation(
|
62
|
+
'todoUncompleted',
|
63
63
|
Schema.Struct({ id: Schema.String }),
|
64
64
|
'UPDATE todos SET completed = false WHERE id = $id',
|
65
65
|
{
|
@@ -69,8 +69,8 @@ export const mutations = {
|
|
69
69
|
}),
|
70
70
|
},
|
71
71
|
),
|
72
|
-
|
73
|
-
'
|
72
|
+
todoCompleteds: defineMutation(
|
73
|
+
'todoCompleteds',
|
74
74
|
Schema.Struct({ ids: Schema.Array(Schema.String) }),
|
75
75
|
'UPDATE todos SET completed = true WHERE id IN ($ids:csv)',
|
76
76
|
{
|
@@ -124,7 +124,7 @@ export const mutations = {
|
|
124
124
|
Schema.Struct({ id: Schema.String, text: Schema.String }),
|
125
125
|
'UPDATE todos SET text = $text WHERE id = $id',
|
126
126
|
{
|
127
|
-
|
127
|
+
clientOnly: true,
|
128
128
|
facts: ({ id }) => ({ modify: { set: [facts.inputValue(id)] } }),
|
129
129
|
},
|
130
130
|
),
|
@@ -135,21 +135,16 @@ export type PartialEvent = { mutation: string; args: any }
|
|
135
135
|
export const toEventNodes = (
|
136
136
|
partialEvents: PartialEvent[],
|
137
137
|
mutationDefs: Record<string, MutationDef.Any>,
|
138
|
+
clientId: string,
|
139
|
+
sessionId: string | undefined,
|
138
140
|
): HistoryDagNode[] => {
|
139
141
|
const nodesAcc: HistoryDagNode[] = [rootEventNode]
|
140
142
|
|
141
143
|
let currentEventId: EventId.EventId = EventId.ROOT
|
142
144
|
|
143
|
-
const getNextEventId = (mutationDef: MutationDef.Any): EventId.EventId => {
|
144
|
-
if (mutationDef.options.localOnly) {
|
145
|
-
return { global: currentEventId.global, local: currentEventId.local + 1 }
|
146
|
-
}
|
147
|
-
return { global: currentEventId.global + 1, local: 0 }
|
148
|
-
}
|
149
|
-
|
150
145
|
const eventNodes = partialEvents.map((partialEvent) => {
|
151
146
|
const mutationDef = mutationDefs[partialEvent.mutation]!
|
152
|
-
const eventId =
|
147
|
+
const eventId = EventId.nextPair(currentEventId, mutationDef.options.clientOnly).id
|
153
148
|
currentEventId = eventId
|
154
149
|
|
155
150
|
const factsSnapshot = factsSnapshotForDag(historyDagFromNodes(nodesAcc, { skipFactsCheck: true }), undefined)
|
@@ -207,6 +202,8 @@ export const toEventNodes = (
|
|
207
202
|
mutation: partialEvent.mutation,
|
208
203
|
args: partialEvent.args,
|
209
204
|
factsGroup: facts,
|
205
|
+
clientId,
|
206
|
+
sessionId,
|
210
207
|
} satisfies HistoryDagNode
|
211
208
|
nodesAcc.push(node)
|
212
209
|
return node
|
@@ -221,11 +218,11 @@ export const toEventNodes = (
|
|
221
218
|
|
222
219
|
const getParentId = (eventId: EventId.EventId): EventId.EventId => {
|
223
220
|
const globalParentId = eventId.global
|
224
|
-
const
|
221
|
+
const clientParentId = eventId.client - 1
|
225
222
|
|
226
|
-
if (
|
227
|
-
return { global: globalParentId - 1,
|
223
|
+
if (clientParentId < 0) {
|
224
|
+
return EventId.make({ global: globalParentId - 1, client: EventId.clientDefault })
|
228
225
|
}
|
229
226
|
|
230
|
-
return { global: globalParentId,
|
227
|
+
return EventId.make({ global: globalParentId, client: clientParentId })
|
231
228
|
}
|
package/src/sync/sync.ts
CHANGED
@@ -1,12 +1,23 @@
|
|
1
|
-
import type { Effect, HttpClient, Option, Stream, SubscriptionRef } from '@livestore/utils/effect'
|
1
|
+
import type { Effect, HttpClient, Option, Scope, Stream, SubscriptionRef } from '@livestore/utils/effect'
|
2
2
|
import { Schema } from '@livestore/utils/effect'
|
3
3
|
|
4
|
+
import type { UnexpectedError } from '../adapter-types.js'
|
5
|
+
import type { InitialSyncOptions } from '../leader-thread/types.js'
|
4
6
|
import * as EventId from '../schema/EventId.js'
|
5
7
|
import type * as MutationEvent from '../schema/MutationEvent.js'
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
/**
|
10
|
+
* Those arguments can be used to implement multi-tenancy etc and are passed in from the store.
|
11
|
+
*/
|
12
|
+
export type MakeBackendArgs = {
|
13
|
+
storeId: string
|
14
|
+
clientId: string
|
15
|
+
}
|
16
|
+
|
17
|
+
export type SyncOptions = {
|
18
|
+
makeBackend?: (args: MakeBackendArgs) => Effect.Effect<SyncBackend<any>, UnexpectedError, Scope.Scope>
|
19
|
+
/** @default { _tag: 'Skip' } */
|
20
|
+
initialSyncOptions?: InitialSyncOptions
|
10
21
|
}
|
11
22
|
|
12
23
|
export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
@@ -18,7 +29,7 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
|
18
29
|
) => Stream.Stream<
|
19
30
|
{
|
20
31
|
batch: ReadonlyArray<{
|
21
|
-
mutationEventEncoded: MutationEvent.
|
32
|
+
mutationEventEncoded: MutationEvent.AnyEncodedGlobal
|
22
33
|
metadata: Option.Option<TSyncMetadata>
|
23
34
|
}>
|
24
35
|
remaining: number
|
@@ -33,7 +44,7 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
|
33
44
|
* - Number of events: 1-100
|
34
45
|
* - event ids must be in ascending order
|
35
46
|
* */
|
36
|
-
batch: ReadonlyArray<MutationEvent.
|
47
|
+
batch: ReadonlyArray<MutationEvent.AnyEncodedGlobal>,
|
37
48
|
) => Effect.Effect<
|
38
49
|
{
|
39
50
|
/** Indexes are relative to `batch` */
|
@@ -46,6 +57,7 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
|
46
57
|
}
|
47
58
|
|
48
59
|
export class IsOfflineError extends Schema.TaggedError<IsOfflineError>()('IsOfflineError', {}) {}
|
60
|
+
|
49
61
|
export class InvalidPushError extends Schema.TaggedError<InvalidPushError>()('InvalidPushError', {
|
50
62
|
reason: Schema.Union(
|
51
63
|
Schema.TaggedStruct('Unexpected', {
|
@@ -55,12 +67,16 @@ export class InvalidPushError extends Schema.TaggedError<InvalidPushError>()('In
|
|
55
67
|
minimumExpectedId: Schema.Number,
|
56
68
|
providedId: Schema.Number,
|
57
69
|
}),
|
58
|
-
Schema.TaggedStruct('LeaderAhead', {
|
59
|
-
minimumExpectedId: EventId.EventId,
|
60
|
-
providedId: EventId.EventId,
|
61
|
-
}),
|
62
70
|
),
|
63
71
|
}) {}
|
72
|
+
|
64
73
|
export class InvalidPullError extends Schema.TaggedError<InvalidPullError>()('InvalidPullError', {
|
65
74
|
message: Schema.String,
|
66
75
|
}) {}
|
76
|
+
|
77
|
+
export class LeaderAheadError extends Schema.TaggedError<LeaderAheadError>()('LeaderAheadError', {
|
78
|
+
minimumExpectedId: EventId.EventId,
|
79
|
+
providedId: EventId.EventId,
|
80
|
+
/** Generation number the client session should use for subsequent pushes */
|
81
|
+
// nextGeneration: Schema.Number,
|
82
|
+
}) {}
|