MCPcopy
hub / github.com/tldraw/tldraw / TLSyncClient

Class TLSyncClient

packages/sync-core/src/lib/TLSyncClient.ts:365–977  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

363 * @public
364 */
365export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>> {
366 /** The last clock time from the most recent server update */
367 private lastServerClock = -1
368 private lastServerInteractionTimestamp = Date.now()
369
370 /** The queue of in-flight push requests that have not yet been acknowledged by the server */
371 private pendingPushRequests: TLPushRequest<R>[] = []
372 private unsentChanges: {
373 nextDiff?: RecordsDiff<R>
374 nextPresence?: R | null
375 } = { nextDiff: undefined, nextPresence: undefined }
376
377 /**
378 * The diff of 'unconfirmed', 'optimistic' changes that have been made locally by the user if we
379 * take this diff, reverse it, and apply that to the store, our store will match exactly the most
380 * recent state of the server that we know about
381 */
382 private speculativeChanges: RecordsDiff<R> = {
383 added: {} as any,
384 updated: {} as any,
385 removed: {} as any,
386 }
387
388 private disposables: Array<() => void> = []
389
390 /** Separate scheduler instance for network sync operations */
391 private readonly fpsScheduler: FpsScheduler
392
393 /** Send any unsent push requests to the server */
394 private readonly sendUnsentChanges: {
395 (): void
396 cancel?(): void
397 }
398
399 /** Schedule a rebase operation */
400 private readonly scheduleRebase: {
401 (): void
402 cancel?(): void
403 }
404
405 /** @internal */
406 readonly store: S
407 /** @internal */
408 readonly socket: TLPersistentClientSocket<TLSocketClientSentEvent<R>, TLSocketServerSentEvent<R>>
409
410 /** @internal */
411 readonly presenceState: Signal<R | null> | undefined
412 /** @internal */
413 readonly presenceMode: Signal<TLPresenceMode> | undefined
414
415 // isOnline is true when we have an open socket connection and we have
416 // established a connection with the server room (i.e. we have received a 'connect' message)
417 /** @internal */
418 isConnectedToRoom = false
419
420 /**
421 * The client clock is essentially a counter for push requests Each time a push request is created
422 * the clock is incremented. This clock is sent with the push request to the server, and the

Callers

nothing calls this directly

Calls 11

applyNetworkDiffMethod · 0.95
resetConnectionMethod · 0.95
reverseRecordsDiffFunction · 0.90
getNetworkDiffFunction · 0.90
nowMethod · 0.80
_flushHistoryMethod · 0.80
mergeRemoteChangesMethod · 0.80
applyDiffMethod · 0.80
extractingChangesMethod · 0.80
ensureStoreIsUsableMethod · 0.80
errorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…