tldraw/packages/sync-core/src/lib/protocol.ts
alex ce493dcfaf
[4/5] sync -> sync-core, sync-react -> sync (#4123)
Renames `@tldraw/sync` to `@tldraw/sync-core`, and `@tldraw/sync-react`
to `@tldraw/sync`. This also adds an export * from sync-core to sync.

- [x] `other`
2024-07-10 16:09:10 +00:00

89 lines
2 KiB
TypeScript

import { SerializedSchema, UnknownRecord } from '@tldraw/store'
import { NetworkDiff, ObjectDiff, RecordOpType } from './diff'
/** @public */
const TLSYNC_PROTOCOL_VERSION = 6
export function getTlsyncProtocolVersion() {
return TLSYNC_PROTOCOL_VERSION
}
/** @public */
export const TLIncompatibilityReason = {
ClientTooOld: 'clientTooOld',
ServerTooOld: 'serverTooOld',
InvalidRecord: 'invalidRecord',
InvalidOperation: 'invalidOperation',
RoomNotFound: 'roomNotFound',
} as const
/** @public */
export type TLIncompatibilityReason =
(typeof TLIncompatibilityReason)[keyof typeof TLIncompatibilityReason]
/** @public */
export type TLSocketServerSentEvent<R extends UnknownRecord> =
| {
type: 'connect'
hydrationType: 'wipe_all' | 'wipe_presence'
connectRequestId: string
protocolVersion: number
schema: SerializedSchema
diff: NetworkDiff<R>
serverClock: number
}
| {
type: 'incompatibility_error'
reason: TLIncompatibilityReason
}
| {
type: 'error'
error?: any
}
| {
type: 'pong'
}
| { type: 'data'; data: TLSocketServerSentDataEvent<R>[] }
| TLSocketServerSentDataEvent<R>
/** @public */
export type TLSocketServerSentDataEvent<R extends UnknownRecord> =
| {
type: 'patch'
diff: NetworkDiff<R>
serverClock: number
}
| {
type: 'push_result'
clientClock: number
serverClock: number
action: 'discard' | 'commit' | { rebaseWithDiff: NetworkDiff<R> }
}
/** @public */
export interface TLPushRequest<R extends UnknownRecord> {
type: 'push'
clientClock: number
diff?: NetworkDiff<R>
presence?: [typeof RecordOpType.Patch, ObjectDiff] | [typeof RecordOpType.Put, R]
}
/** @public */
export interface TLConnectRequest {
type: 'connect'
connectRequestId: string
lastServerClock: number
protocolVersion: number
schema: SerializedSchema
}
/** @public */
export interface TLPingRequest {
type: 'ping'
}
/** @public */
export type TLSocketClientSentEvent<R extends UnknownRecord> =
| TLPushRequest<R>
| TLConnectRequest
| TLPingRequest