@livestore/common 0.2.0 → 0.3.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/__tests__/fixture.d.ts +163 -1
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +3 -1
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/adapter-types.d.ts +53 -38
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +5 -7
- package/dist/adapter-types.js.map +1 -1
- package/dist/bounded-collections.d.ts +2 -2
- package/dist/bounded-collections.d.ts.map +1 -1
- package/dist/debug-info.d.ts +13 -13
- package/dist/derived-mutations.d.ts +1 -1
- package/dist/derived-mutations.d.ts.map +1 -1
- package/dist/devtools/devtools-bridge.d.ts +2 -2
- package/dist/devtools/devtools-bridge.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.d.ts +84 -196
- package/dist/devtools/devtools-messages.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.js +55 -61
- package/dist/devtools/devtools-messages.js.map +1 -1
- package/dist/devtools/index.d.ts.map +1 -1
- package/dist/devtools/index.js +1 -2
- package/dist/devtools/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/init-singleton-tables.d.ts +1 -1
- package/dist/init-singleton-tables.d.ts.map +1 -1
- package/dist/leader-thread/apply-mutation.d.ts +8 -0
- package/dist/leader-thread/apply-mutation.d.ts.map +1 -0
- package/dist/leader-thread/apply-mutation.js +95 -0
- package/dist/leader-thread/apply-mutation.js.map +1 -0
- package/dist/leader-thread/connection.d.ts +11 -0
- package/dist/leader-thread/connection.d.ts.map +1 -0
- package/dist/leader-thread/connection.js +44 -0
- package/dist/leader-thread/connection.js.map +1 -0
- package/dist/leader-thread/leader-sync-processor.d.ts +47 -0
- package/dist/leader-thread/leader-sync-processor.d.ts.map +1 -0
- package/dist/leader-thread/leader-sync-processor.js +422 -0
- package/dist/leader-thread/leader-sync-processor.js.map +1 -0
- package/dist/leader-thread/leader-worker-devtools.d.ts +6 -0
- package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -0
- package/dist/leader-thread/leader-worker-devtools.js +216 -0
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -0
- package/dist/leader-thread/make-leader-thread-layer.d.ts +20 -0
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -0
- package/dist/leader-thread/make-leader-thread-layer.js +106 -0
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -0
- package/dist/leader-thread/mod.d.ts +7 -0
- package/dist/leader-thread/mod.d.ts.map +1 -0
- package/dist/leader-thread/mod.js +7 -0
- package/dist/leader-thread/mod.js.map +1 -0
- package/dist/leader-thread/mutationlog.d.ts +23 -0
- package/dist/leader-thread/mutationlog.d.ts.map +1 -0
- package/dist/leader-thread/mutationlog.js +27 -0
- package/dist/leader-thread/mutationlog.js.map +1 -0
- package/dist/leader-thread/pull-queue-set.d.ts +7 -0
- package/dist/leader-thread/pull-queue-set.d.ts.map +1 -0
- package/dist/leader-thread/pull-queue-set.js +39 -0
- package/dist/leader-thread/pull-queue-set.js.map +1 -0
- package/dist/leader-thread/recreate-db.d.ts +7 -0
- package/dist/leader-thread/recreate-db.d.ts.map +1 -0
- package/dist/leader-thread/recreate-db.js +69 -0
- package/dist/leader-thread/recreate-db.js.map +1 -0
- package/dist/leader-thread/shutdown-channel.d.ts +15 -0
- package/dist/leader-thread/shutdown-channel.d.ts.map +1 -0
- package/dist/leader-thread/shutdown-channel.js +7 -0
- package/dist/leader-thread/shutdown-channel.js.map +1 -0
- package/dist/leader-thread/types.d.ts +87 -0
- package/dist/leader-thread/types.d.ts.map +1 -0
- package/dist/leader-thread/types.js +11 -0
- package/dist/leader-thread/types.js.map +1 -0
- package/dist/mutation.d.ts +3 -4
- package/dist/mutation.d.ts.map +1 -1
- package/dist/mutation.js +0 -14
- package/dist/mutation.js.map +1 -1
- package/dist/otel.d.ts +7 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +11 -0
- package/dist/otel.js.map +1 -0
- package/dist/query-builder/api.d.ts +2 -2
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/query-builder/api.js.map +1 -1
- package/dist/query-builder/impl.d.ts +1 -1
- package/dist/query-builder/impl.d.ts.map +1 -1
- package/dist/query-builder/impl.js +21 -4
- package/dist/query-builder/impl.js.map +1 -1
- package/dist/query-builder/impl.test.js +24 -1
- package/dist/query-builder/impl.test.js.map +1 -1
- package/dist/query-info.d.ts +1 -1
- package/dist/query-info.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.d.ts +1 -1
- package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.js +6 -6
- package/dist/rehydrate-from-mutationlog.js.map +1 -1
- package/dist/schema/EventId.d.ts +37 -0
- package/dist/schema/EventId.d.ts.map +1 -0
- package/dist/schema/EventId.js +30 -0
- package/dist/schema/EventId.js.map +1 -0
- package/dist/schema/MutationEvent.d.ts +191 -0
- package/dist/schema/MutationEvent.d.ts.map +1 -0
- package/dist/schema/MutationEvent.js +56 -0
- package/dist/schema/MutationEvent.js.map +1 -0
- package/dist/schema/mod.d.ts +8 -0
- package/dist/schema/mod.d.ts.map +1 -0
- package/dist/schema/mod.js +8 -0
- package/dist/schema/mod.js.map +1 -0
- package/dist/schema/mutations.d.ts +3 -123
- package/dist/schema/mutations.d.ts.map +1 -1
- package/dist/schema/mutations.js +0 -26
- package/dist/schema/mutations.js.map +1 -1
- package/dist/schema/{index.d.ts → schema.d.ts} +1 -5
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/schema/{index.js → schema.js} +1 -5
- package/dist/schema/schema.js.map +1 -0
- package/dist/schema/system-tables.d.ts +55 -29
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +10 -5
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema-management/migrations.d.ts +1 -1
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +6 -1
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
- package/dist/sync/client-session-sync-processor.d.ts +45 -0
- package/dist/sync/client-session-sync-processor.d.ts.map +1 -0
- package/dist/sync/client-session-sync-processor.js +131 -0
- package/dist/sync/client-session-sync-processor.js.map +1 -0
- package/dist/sync/index.d.ts +2 -0
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +2 -0
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/next/compact-events.d.ts +1 -1
- package/dist/sync/next/compact-events.d.ts.map +1 -1
- package/dist/sync/next/compact-events.js +2 -1
- package/dist/sync/next/compact-events.js.map +1 -1
- package/dist/sync/next/facts.d.ts +5 -5
- 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 +30 -0
- package/dist/sync/next/history-dag-common.d.ts.map +1 -0
- package/dist/sync/next/history-dag-common.js +20 -0
- package/dist/sync/next/history-dag-common.js.map +1 -0
- package/dist/sync/next/history-dag.d.ts +4 -27
- package/dist/sync/next/history-dag.d.ts.map +1 -1
- package/dist/sync/next/history-dag.js +1 -19
- package/dist/sync/next/history-dag.js.map +1 -1
- package/dist/sync/next/mod.d.ts +1 -0
- package/dist/sync/next/mod.d.ts.map +1 -1
- package/dist/sync/next/mod.js +1 -0
- package/dist/sync/next/mod.js.map +1 -1
- package/dist/sync/next/rebase-events.d.ts +3 -2
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.d.ts.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +2 -1
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts +1 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.js +4 -3
- package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
- package/dist/sync/sync.d.ts +33 -12
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/sync.js +10 -1
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/syncstate.d.ts +123 -0
- package/dist/sync/syncstate.d.ts.map +1 -0
- package/dist/sync/syncstate.js +248 -0
- package/dist/sync/syncstate.js.map +1 -0
- package/dist/sync/syncstate.test.d.ts +2 -0
- package/dist/sync/syncstate.test.d.ts.map +1 -0
- package/dist/sync/syncstate.test.js +399 -0
- package/dist/sync/syncstate.test.js.map +1 -0
- package/dist/sync/validate-push-payload.d.ts +5 -0
- package/dist/sync/validate-push-payload.d.ts.map +1 -0
- package/dist/sync/validate-push-payload.js +15 -0
- package/dist/sync/validate-push-payload.js.map +1 -0
- package/dist/util.d.ts +2 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/version.d.ts +2 -2
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +2 -2
- package/package.json +13 -6
- package/src/__tests__/fixture.ts +5 -1
- package/src/adapter-types.ts +60 -34
- package/src/derived-mutations.test.ts +1 -1
- package/src/derived-mutations.ts +1 -1
- package/src/devtools/devtools-bridge.ts +2 -2
- package/src/devtools/devtools-messages.ts +70 -74
- package/src/devtools/index.ts +1 -2
- package/src/index.ts +2 -1
- package/src/init-singleton-tables.ts +1 -1
- package/src/leader-thread/apply-mutation.ts +143 -0
- package/src/leader-thread/connection.ts +67 -0
- package/src/leader-thread/leader-sync-processor.ts +666 -0
- package/src/leader-thread/leader-worker-devtools.ts +358 -0
- package/src/leader-thread/make-leader-thread-layer.ts +192 -0
- package/src/leader-thread/mod.ts +6 -0
- package/src/leader-thread/mutationlog.ts +42 -0
- package/src/leader-thread/pull-queue-set.ts +58 -0
- package/src/leader-thread/recreate-db.ts +109 -0
- package/src/leader-thread/shutdown-channel.ts +13 -0
- package/src/leader-thread/types.ts +129 -0
- package/src/mutation.ts +3 -21
- package/src/otel.ts +20 -0
- package/src/query-builder/api.ts +3 -2
- package/src/query-builder/impl.test.ts +28 -1
- package/src/query-builder/impl.ts +21 -5
- package/src/query-info.ts +1 -1
- package/src/rehydrate-from-mutationlog.ts +7 -11
- package/src/schema/EventId.ts +46 -0
- package/src/schema/MutationEvent.ts +161 -0
- package/src/schema/mod.ts +7 -0
- package/src/schema/mutations.ts +5 -126
- package/src/schema/{index.ts → schema.ts} +0 -5
- package/src/schema/system-tables.ts +18 -5
- package/src/schema-management/migrations.ts +9 -2
- package/src/schema-management/validate-mutation-defs.ts +1 -1
- package/src/sync/client-session-sync-processor.ts +207 -0
- package/src/sync/index.ts +2 -0
- package/src/sync/next/compact-events.ts +3 -2
- package/src/sync/next/facts.ts +11 -5
- package/src/sync/next/history-dag-common.ts +44 -0
- package/src/sync/next/history-dag.ts +3 -45
- package/src/sync/next/mod.ts +1 -0
- package/src/sync/next/rebase-events.ts +6 -5
- package/src/sync/next/test/compact-events.test.ts +3 -2
- package/src/sync/next/test/mutation-fixtures.ts +7 -6
- package/src/sync/sync.ts +32 -12
- package/src/sync/syncstate.test.ts +464 -0
- package/src/sync/syncstate.ts +385 -0
- package/src/sync/validate-push-payload.ts +18 -0
- package/src/version.ts +2 -2
- package/dist/schema/index.d.ts.map +0 -1
- package/dist/schema/index.js.map +0 -1
- package/dist/sync/next-mutation-event-id-pair.d.ts +0 -14
- package/dist/sync/next-mutation-event-id-pair.d.ts.map +0 -1
- package/dist/sync/next-mutation-event-id-pair.js +0 -13
- package/dist/sync/next-mutation-event-id-pair.js.map +0 -1
- package/src/sync/next-mutation-event-id-pair.ts +0 -20
@@ -0,0 +1,44 @@
|
|
1
|
+
import * as EventId from '../../schema/EventId.js'
|
2
|
+
import type { MutationEventFactsGroup } from '../../schema/mutations.js'
|
3
|
+
import { graphology } from './graphology_.js'
|
4
|
+
|
5
|
+
export const connectionTypeOptions = ['parent', 'facts'] as const
|
6
|
+
export type ConnectionType = (typeof connectionTypeOptions)[number]
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Eventlog represented as a multi-DAG including edges for
|
10
|
+
* - total-order (parent) relationships
|
11
|
+
* - dependency (requires/reads facts) relationships
|
12
|
+
*/
|
13
|
+
export type HistoryDag = graphology.IGraph<HistoryDagNode, { type: ConnectionType }>
|
14
|
+
|
15
|
+
export const emptyHistoryDag = (): HistoryDag =>
|
16
|
+
new graphology.Graph({
|
17
|
+
allowSelfLoops: false,
|
18
|
+
multi: true,
|
19
|
+
type: 'directed',
|
20
|
+
})
|
21
|
+
|
22
|
+
// TODO consider making `ROOT_ID` parent to itself
|
23
|
+
export const rootParentId = { global: EventId.ROOT.global - 1, local: 0 } satisfies EventId.EventId
|
24
|
+
|
25
|
+
export type HistoryDagNode = {
|
26
|
+
id: EventId.EventId
|
27
|
+
parentId: EventId.EventId
|
28
|
+
mutation: string
|
29
|
+
args: any
|
30
|
+
/** Facts are being used for conflict detection and history compaction */
|
31
|
+
factsGroup: MutationEventFactsGroup
|
32
|
+
meta?: any
|
33
|
+
}
|
34
|
+
|
35
|
+
export const rootEventNode: HistoryDagNode = {
|
36
|
+
id: EventId.ROOT,
|
37
|
+
parentId: rootParentId,
|
38
|
+
// unused below
|
39
|
+
mutation: '__Root__',
|
40
|
+
args: {},
|
41
|
+
factsGroup: { modifySet: new Map(), modifyUnset: new Map(), depRequire: new Map(), depRead: new Map() },
|
42
|
+
}
|
43
|
+
|
44
|
+
export const EMPTY_FACT_VALUE = Symbol('EMPTY_FACT_VALUE')
|
@@ -1,50 +1,8 @@
|
|
1
|
-
import
|
2
|
-
import type { MutationEventFactsGroup } from '../../schema/mutations.js'
|
1
|
+
import type * as EventId from '../../schema/EventId.js'
|
3
2
|
import { factsToString, validateFacts } from './facts.js'
|
4
|
-
import {
|
3
|
+
import { emptyHistoryDag, type HistoryDagNode, rootParentId } from './history-dag-common.js'
|
5
4
|
|
6
|
-
export const
|
7
|
-
export type ConnectionType = (typeof connectionTypeOptions)[number]
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Eventlog represented as a multi-DAG including edges for
|
11
|
-
* - total-order (parent) relationships
|
12
|
-
* - dependency (requires/reads facts) relationships
|
13
|
-
*/
|
14
|
-
export type HistoryDag = graphology.IGraph<HistoryDagNode, { type: ConnectionType }>
|
15
|
-
|
16
|
-
export const emptyHistoryDag = (): HistoryDag =>
|
17
|
-
new graphology.Graph({
|
18
|
-
allowSelfLoops: false,
|
19
|
-
multi: true,
|
20
|
-
type: 'directed',
|
21
|
-
})
|
22
|
-
|
23
|
-
// TODO consider making `ROOT_ID` parent to itself
|
24
|
-
const rootParentId = { global: ROOT_ID.global - 1, local: 0 } satisfies EventId
|
25
|
-
|
26
|
-
export type HistoryDagNode = {
|
27
|
-
id: EventId
|
28
|
-
parentId: EventId
|
29
|
-
mutation: string
|
30
|
-
args: any
|
31
|
-
/** Facts are being used for conflict detection and history compaction */
|
32
|
-
factsGroup: MutationEventFactsGroup
|
33
|
-
meta?: any
|
34
|
-
}
|
35
|
-
|
36
|
-
export const rootEventNode: HistoryDagNode = {
|
37
|
-
id: ROOT_ID,
|
38
|
-
parentId: rootParentId,
|
39
|
-
// unused below
|
40
|
-
mutation: '__Root__',
|
41
|
-
args: {},
|
42
|
-
factsGroup: { modifySet: new Map(), modifyUnset: new Map(), depRequire: new Map(), depRead: new Map() },
|
43
|
-
}
|
44
|
-
|
45
|
-
export const EMPTY_FACT_VALUE = Symbol('EMPTY_FACT_VALUE')
|
46
|
-
|
47
|
-
export const eventIdToString = (eventId: EventId) =>
|
5
|
+
export const eventIdToString = (eventId: EventId.EventId) =>
|
48
6
|
eventId.local === 0 ? eventId.global.toString() : `${eventId.global}.${eventId.local}`
|
49
7
|
|
50
8
|
export const historyDagFromNodes = (dagNodes: HistoryDagNode[], options?: { skipFactsCheck: boolean }) => {
|
package/src/sync/next/mod.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import type
|
2
|
-
import type
|
1
|
+
import type * as EventId from '../../schema/EventId.js'
|
2
|
+
import type * as MutationEvent from '../../schema/MutationEvent.js'
|
3
|
+
import type { MutationDef, MutationEventFactsSnapshot } from '../../schema/mutations.js'
|
3
4
|
import {
|
4
5
|
applyFactGroups,
|
5
6
|
factsIntersect,
|
@@ -7,7 +8,7 @@ import {
|
|
7
8
|
getFactsGroupForMutationArgs,
|
8
9
|
validateFacts,
|
9
10
|
} from './facts.js'
|
10
|
-
import type { HistoryDagNode } from './history-dag.js'
|
11
|
+
import type { HistoryDagNode } from './history-dag-common.js'
|
11
12
|
|
12
13
|
export type RebaseEventWithConflict = HistoryDagNode & {
|
13
14
|
conflictType: 'overlap' | 'missing-requirement'
|
@@ -88,8 +89,8 @@ export const rebaseEvents = ({
|
|
88
89
|
return rebasedLocalEvents.map(
|
89
90
|
(event, index) =>
|
90
91
|
({
|
91
|
-
id: { global: headGlobalId + index + 1, local: 0 } satisfies EventId,
|
92
|
-
parentId: { global: headGlobalId + index, local: 0 } satisfies EventId,
|
92
|
+
id: { global: headGlobalId + index + 1, local: 0 } satisfies EventId.EventId,
|
93
|
+
parentId: { global: headGlobalId + index, local: 0 } satisfies EventId.EventId,
|
93
94
|
mutation: event.mutation,
|
94
95
|
args: event.args,
|
95
96
|
}) satisfies MutationEvent.Any,
|
@@ -2,8 +2,9 @@ import type { MutationEventFacts } from '@livestore/common/schema'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
3
3
|
|
4
4
|
import { compactEvents } from '../compact-events.js'
|
5
|
-
import
|
6
|
-
import {
|
5
|
+
import { historyDagFromNodes } from '../history-dag.js'
|
6
|
+
import type { HistoryDagNode } from '../history-dag-common.js'
|
7
|
+
import { EMPTY_FACT_VALUE } from '../history-dag-common.js'
|
7
8
|
import { mutations, toEventNodes } from './mutation-fixtures.js'
|
8
9
|
|
9
10
|
const customStringify = (value: any): string => {
|
@@ -1,11 +1,12 @@
|
|
1
1
|
import { Schema } from '@livestore/utils/effect'
|
2
2
|
|
3
|
-
import
|
3
|
+
import * as EventId from '../../../schema/EventId.js'
|
4
4
|
import type { MutationDef } from '../../../schema/mutations.js'
|
5
5
|
import { defineFacts, defineMutation } from '../../../schema/mutations.js'
|
6
6
|
import { factsSnapshotForDag, getFactsGroupForMutationArgs } from '../facts.js'
|
7
|
-
import
|
8
|
-
import {
|
7
|
+
import { historyDagFromNodes } from '../history-dag.js'
|
8
|
+
import type { HistoryDagNode } from '../history-dag-common.js'
|
9
|
+
import { rootEventNode } from '../history-dag-common.js'
|
9
10
|
|
10
11
|
/** Used for conflict detection and event history compaction */
|
11
12
|
export const facts = defineFacts({
|
@@ -137,9 +138,9 @@ export const toEventNodes = (
|
|
137
138
|
): HistoryDagNode[] => {
|
138
139
|
const nodesAcc: HistoryDagNode[] = [rootEventNode]
|
139
140
|
|
140
|
-
let currentEventId: EventId =
|
141
|
+
let currentEventId: EventId.EventId = EventId.ROOT
|
141
142
|
|
142
|
-
const getNextEventId = (mutationDef: MutationDef.Any): EventId => {
|
143
|
+
const getNextEventId = (mutationDef: MutationDef.Any): EventId.EventId => {
|
143
144
|
if (mutationDef.options.localOnly) {
|
144
145
|
return { global: currentEventId.global, local: currentEventId.local + 1 }
|
145
146
|
}
|
@@ -218,7 +219,7 @@ export const toEventNodes = (
|
|
218
219
|
return eventNodes
|
219
220
|
}
|
220
221
|
|
221
|
-
const getParentId = (eventId: EventId): EventId => {
|
222
|
+
const getParentId = (eventId: EventId.EventId): EventId.EventId => {
|
222
223
|
const globalParentId = eventId.global
|
223
224
|
const localParentId = eventId.local - 1
|
224
225
|
|
package/src/sync/sync.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import type { Effect, HttpClient, Option, Stream, SubscriptionRef } from '@livestore/utils/effect'
|
2
2
|
import { Schema } from '@livestore/utils/effect'
|
3
3
|
|
4
|
-
import
|
5
|
-
import type
|
4
|
+
import * as EventId from '../schema/EventId.js'
|
5
|
+
import type * as MutationEvent from '../schema/MutationEvent.js'
|
6
6
|
|
7
7
|
export interface SyncBackendOptionsBase {
|
8
8
|
type: string
|
@@ -12,25 +12,33 @@ export interface SyncBackendOptionsBase {
|
|
12
12
|
export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
13
13
|
pull: (
|
14
14
|
args: Option.Option<{
|
15
|
-
cursor: EventId
|
15
|
+
cursor: EventId.EventId
|
16
16
|
metadata: Option.Option<TSyncMetadata>
|
17
17
|
}>,
|
18
|
-
options: { listenForNew: boolean },
|
19
18
|
) => Stream.Stream<
|
20
19
|
{
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
batch: ReadonlyArray<{
|
21
|
+
mutationEventEncoded: MutationEvent.AnyEncoded
|
22
|
+
metadata: Option.Option<TSyncMetadata>
|
23
|
+
}>
|
24
|
+
remaining: number
|
24
25
|
},
|
25
26
|
IsOfflineError | InvalidPullError,
|
26
27
|
HttpClient.HttpClient
|
27
28
|
>
|
28
|
-
// TODO support
|
29
|
+
// TODO support transactions (i.e. group of mutation events which need to be applied together)
|
29
30
|
push: (
|
30
|
-
|
31
|
-
|
31
|
+
/**
|
32
|
+
* Constraints for batch:
|
33
|
+
* - Number of events: 1-100
|
34
|
+
* - event ids must be in ascending order
|
35
|
+
* */
|
36
|
+
batch: ReadonlyArray<MutationEvent.AnyEncoded>,
|
32
37
|
) => Effect.Effect<
|
33
|
-
{
|
38
|
+
{
|
39
|
+
/** Indexes are relative to `batch` */
|
40
|
+
metadata: ReadonlyArray<Option.Option<TSyncMetadata>>
|
41
|
+
},
|
34
42
|
IsOfflineError | InvalidPushError,
|
35
43
|
HttpClient.HttpClient
|
36
44
|
>
|
@@ -39,7 +47,19 @@ export type SyncBackend<TSyncMetadata = Schema.JsonValue> = {
|
|
39
47
|
|
40
48
|
export class IsOfflineError extends Schema.TaggedError<IsOfflineError>()('IsOfflineError', {}) {}
|
41
49
|
export class InvalidPushError extends Schema.TaggedError<InvalidPushError>()('InvalidPushError', {
|
42
|
-
|
50
|
+
reason: Schema.Union(
|
51
|
+
Schema.TaggedStruct('Unexpected', {
|
52
|
+
message: Schema.String,
|
53
|
+
}),
|
54
|
+
Schema.TaggedStruct('ServerAhead', {
|
55
|
+
minimumExpectedId: Schema.Number,
|
56
|
+
providedId: Schema.Number,
|
57
|
+
}),
|
58
|
+
Schema.TaggedStruct('LeaderAhead', {
|
59
|
+
minimumExpectedId: EventId.EventId,
|
60
|
+
providedId: EventId.EventId,
|
61
|
+
}),
|
62
|
+
),
|
43
63
|
}) {}
|
44
64
|
export class InvalidPullError extends Schema.TaggedError<InvalidPullError>()('InvalidPullError', {
|
45
65
|
message: Schema.String,
|