@tldraw/sync-core 4.2.0-next.d70e0650108f → 4.2.0-next.e824a30c434e

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.
@@ -31,7 +31,7 @@ import {
31
31
  * @param cb - Callback function that receives the event value
32
32
  * @returns Function to call when you want to unsubscribe from the events
33
33
  *
34
- * @internal
34
+ * @public
35
35
  */
36
36
  export type SubscribingFn<T> = (cb: (val: T) => void) => () => void
37
37
 
@@ -149,9 +149,9 @@ export type TLCustomMessageHandler = (this: null, data: any) => void
149
149
  * Event object describing changes in socket connection status.
150
150
  * Contains either a basic status change or an error with details.
151
151
  *
152
- * @internal
152
+ * @public
153
153
  */
154
- export type TlSocketStatusChangeEvent =
154
+ export type TLSocketStatusChangeEvent =
155
155
  | {
156
156
  /** Connection came online or went offline */
157
157
  status: 'online' | 'offline'
@@ -169,7 +169,7 @@ export type TlSocketStatusChangeEvent =
169
169
  *
170
170
  * @internal
171
171
  */
172
- export type TLSocketStatusListener = (params: TlSocketStatusChangeEvent) => void
172
+ export type TLSocketStatusListener = (params: TLSocketStatusChangeEvent) => void
173
173
 
174
174
  /**
175
175
  * Possible connection states for a persistent client socket.
@@ -183,7 +183,7 @@ export type TLPersistentClientSocketStatus = 'online' | 'offline' | 'error'
183
183
  * Mode for handling presence information in sync sessions.
184
184
  * Controls whether presence data (cursors, selections) is shared with other clients.
185
185
  *
186
- * @internal
186
+ * @public
187
187
  */
188
188
  export type TLPresenceMode =
189
189
  /** No presence sharing - client operates independently */
@@ -217,9 +217,12 @@ export type TLPresenceMode =
217
217
  * }
218
218
  * ```
219
219
  *
220
- * @internal
220
+ * @public
221
221
  */
222
- export interface TLPersistentClientSocket<R extends UnknownRecord = UnknownRecord> {
222
+ export interface TLPersistentClientSocket<
223
+ ClientSentMessage extends object = object,
224
+ ServerSentMessage extends object = object,
225
+ > {
223
226
  /** Current connection state - online means actively connected and ready */
224
227
  connectionStatus: 'online' | 'offline' | 'error'
225
228
 
@@ -227,27 +230,32 @@ export interface TLPersistentClientSocket<R extends UnknownRecord = UnknownRecor
227
230
  * Send a protocol message to the sync server
228
231
  * @param msg - Message to send (connect, push, ping, etc.)
229
232
  */
230
- sendMessage(msg: TLSocketClientSentEvent<R>): void
233
+ sendMessage(msg: ClientSentMessage): void
231
234
 
232
235
  /**
233
236
  * Subscribe to messages received from the server
234
237
  * @param callback - Function called for each received message
235
238
  * @returns Cleanup function to remove the listener
236
239
  */
237
- onReceiveMessage: SubscribingFn<TLSocketServerSentEvent<R>>
240
+ onReceiveMessage: SubscribingFn<ServerSentMessage>
238
241
 
239
242
  /**
240
243
  * Subscribe to connection status changes
241
244
  * @param callback - Function called when connection status changes
242
245
  * @returns Cleanup function to remove the listener
243
246
  */
244
- onStatusChange: SubscribingFn<TlSocketStatusChangeEvent>
247
+ onStatusChange: SubscribingFn<TLSocketStatusChangeEvent>
245
248
 
246
249
  /**
247
250
  * Force a connection restart (disconnect then reconnect)
248
251
  * Used for error recovery or when connection health checks fail
249
252
  */
250
253
  restart(): void
254
+
255
+ /**
256
+ * Close the connection
257
+ */
258
+ close(): void
251
259
  }
252
260
 
253
261
  const PING_INTERVAL = 5000
@@ -312,7 +320,7 @@ const MAX_TIME_TO_WAIT_FOR_SERVER_INTERACTION_BEFORE_RESETTING_CONNECTION = PING
312
320
  * })
313
321
  * ```
314
322
  *
315
- * @internal
323
+ * @public
316
324
  */
317
325
  export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>> {
318
326
  /** The last clock time from the most recent server update */
@@ -335,14 +343,19 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
335
343
 
336
344
  private disposables: Array<() => void> = []
337
345
 
346
+ /** @internal */
338
347
  readonly store: S
339
- readonly socket: TLPersistentClientSocket<R>
348
+ /** @internal */
349
+ readonly socket: TLPersistentClientSocket<TLSocketClientSentEvent<R>, TLSocketServerSentEvent<R>>
340
350
 
351
+ /** @internal */
341
352
  readonly presenceState: Signal<R | null> | undefined
353
+ /** @internal */
342
354
  readonly presenceMode: Signal<TLPresenceMode> | undefined
343
355
 
344
356
  // isOnline is true when we have an open socket connection and we have
345
357
  // established a connection with the server room (i.e. we have received a 'connect' message)
358
+ /** @internal */
346
359
  isConnectedToRoom = false
347
360
 
348
361
  /**
@@ -366,7 +379,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
366
379
  * @param details - Connection details
367
380
  * - isReadonly - Whether the connection is in read-only mode
368
381
  */
369
- public readonly onAfterConnect?: (self: this, details: { isReadonly: boolean }) => void
382
+ private readonly onAfterConnect?: (self: this, details: { isReadonly: boolean }) => void
370
383
 
371
384
  private readonly onCustomMessageReceived?: TLCustomMessageHandler
372
385
 
@@ -380,7 +393,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
380
393
 
381
394
  private readonly presenceType: R['typeName'] | null
382
395
 
383
- didCancel?: () => boolean
396
+ private didCancel?: () => boolean
384
397
 
385
398
  /**
386
399
  * Creates a new TLSyncClient instance to manage synchronization with a remote server.
@@ -400,7 +413,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
400
413
  */
401
414
  constructor(config: {
402
415
  store: S
403
- socket: TLPersistentClientSocket<R>
416
+ socket: TLPersistentClientSocket<any, any>
404
417
  presence: Signal<R | null>
405
418
  presenceMode?: Signal<TLPresenceMode>
406
419
  onLoad(self: TLSyncClient<R, S>): void
@@ -516,6 +529,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
516
529
  }
517
530
  }
518
531
 
532
+ /** @internal */
519
533
  latestConnectRequestId: string | null = null
520
534
 
521
535
  /**
@@ -641,7 +655,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
641
655
  this.lastServerClock = event.serverClock
642
656
  }
643
657
 
644
- incomingDiffBuffer: TLSocketServerSentDataEvent<R>[] = []
658
+ private incomingDiffBuffer: TLSocketServerSentDataEvent<R>[] = []
645
659
 
646
660
  /** Handle events received from the server */
647
661
  private handleServerEvent(event: TLSocketServerSentEvent<R>) {
@@ -701,7 +715,7 @@ export class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>
701
715
  this.scheduleRebase.cancel?.()
702
716
  }
703
717
 
704
- lastPushedPresenceState: R | null = null
718
+ private lastPushedPresenceState: R | null = null
705
719
 
706
720
  private pushPresence(nextPresence: R | null) {
707
721
  // make sure we push any document changes first
@@ -62,7 +62,7 @@ export class TestSocketPair<R extends UnknownRecord> {
62
62
  }
63
63
  didReceiveFromClient?: (msg: TLSocketClientSentEvent<R>) => void = undefined
64
64
  clientDisconnected?: () => void = undefined
65
- clientSocket: TLPersistentClientSocket<R> = {
65
+ clientSocket: TLPersistentClientSocket<TLSocketClientSentEvent<R>, TLSocketServerSentEvent<R>> = {
66
66
  connectionStatus: 'offline',
67
67
  onStatusChange: (cb) => {
68
68
  this.callbacks.onStatusChange = cb
@@ -76,7 +76,7 @@ export class TestSocketPair<R extends UnknownRecord> {
76
76
  this.callbacks.onReceiveMessage = null
77
77
  }
78
78
  },
79
- sendMessage: (msg: TLSocketClientSentEvent<R>) => {
79
+ sendMessage: (msg) => {
80
80
  if (this.clientSocket.connectionStatus !== 'online') {
81
81
  throw new Error('trying to send before open')
82
82
  }
@@ -87,6 +87,9 @@ export class TestSocketPair<R extends UnknownRecord> {
87
87
  this.disconnect()
88
88
  this.connect()
89
89
  },
90
+ close: () => {
91
+ this.disconnect()
92
+ },
90
93
  }
91
94
 
92
95
  callbacks = {