2024-01-16 14:38:05 +00:00
|
|
|
import { SerializedSchema, UnknownRecord } from '@tldraw/store'
|
|
|
|
import { NetworkDiff, ObjectDiff, RecordOpType } from './diff'
|
|
|
|
|
|
|
|
/** @public */
|
2024-03-11 13:33:47 +00:00
|
|
|
export const TLSYNC_PROTOCOL_VERSION = 5
|
2024-01-16 14:38:05 +00:00
|
|
|
|
|
|
|
/** @public */
|
2024-03-13 17:18:25 +00:00
|
|
|
export const TLIncompatibilityReason = {
|
|
|
|
ClientTooOld: 'clientTooOld',
|
|
|
|
ServerTooOld: 'serverTooOld',
|
|
|
|
InvalidRecord: 'invalidRecord',
|
|
|
|
InvalidOperation: 'invalidOperation',
|
|
|
|
} as const
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLIncompatibilityReason =
|
|
|
|
(typeof TLIncompatibilityReason)[keyof typeof TLIncompatibilityReason]
|
2024-01-16 14:38:05 +00:00
|
|
|
|
|
|
|
/** @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
|
|
|
|
}
|
2024-03-11 13:33:47 +00:00
|
|
|
| {
|
|
|
|
type: 'error'
|
|
|
|
error?: any
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'pong'
|
|
|
|
}
|
|
|
|
| { type: 'data'; data: TLSocketServerSentDataEvent<R>[] }
|
2024-03-16 10:21:37 +00:00
|
|
|
| TLSocketServerSentDataEvent<R>
|
2024-03-11 13:33:47 +00:00
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLSocketServerSentDataEvent<R extends UnknownRecord> =
|
2024-01-16 14:38:05 +00:00
|
|
|
| {
|
|
|
|
type: 'patch'
|
|
|
|
diff: NetworkDiff<R>
|
|
|
|
serverClock: number
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'push_result'
|
|
|
|
clientClock: number
|
|
|
|
serverClock: number
|
|
|
|
action: 'discard' | 'commit' | { rebaseWithDiff: NetworkDiff<R> }
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLPushRequest<R extends UnknownRecord> =
|
|
|
|
| {
|
|
|
|
type: 'push'
|
|
|
|
clientClock: number
|
2024-03-13 17:18:25 +00:00
|
|
|
presence: [typeof RecordOpType.Patch, ObjectDiff] | [typeof RecordOpType.Put, R]
|
2024-01-16 14:38:05 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'push'
|
|
|
|
clientClock: number
|
|
|
|
diff: NetworkDiff<R>
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLConnectRequest = {
|
|
|
|
type: 'connect'
|
|
|
|
connectRequestId: string
|
|
|
|
lastServerClock: number
|
|
|
|
protocolVersion: number
|
|
|
|
schema: SerializedSchema
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLPingRequest = {
|
|
|
|
type: 'ping'
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLSocketClientSentEvent<R extends UnknownRecord> =
|
|
|
|
| TLPushRequest<R>
|
|
|
|
| TLConnectRequest
|
|
|
|
| TLPingRequest
|