Skip to content

Commit

Permalink
Merge branch 'main' into release/0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb authored Dec 18, 2024
2 parents 2985df8 + 9bdebdd commit 96a6261
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion js-pkg/client/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ export class YSweetProvider {

private retries: number = 0

/**
* Older versions of the Y-Sweet server did not support the sync message, and would ignore it.
* This may lead to the client thinking the server is offline, when really it just doesn't
* know how to return a heartbeat.
*
* Eventually, we will build protocol version negotiation into the handshake. Until then, we
* use a simple approach: until we receive the first sync message back, we assume the server
* is an older version for the purpose of the heartbeat logic.
*/
private receivedAtLeastOneSyncResponse: boolean = false

constructor(
private authEndpoint: AuthEndpoint,
private docId: string,
Expand Down Expand Up @@ -226,6 +237,13 @@ export class YSweetProvider {
if (this.connectionTimeoutHandle) {
return
}

if (!this.receivedAtLeastOneSyncResponse) {
// Until we receive the first sync response on the connection, we assume
// the server is an older version.
return
}

this.connectionTimeoutHandle = setTimeout(() => {
if (this.websocket) {
this.websocket.close()
Expand Down Expand Up @@ -265,6 +283,8 @@ export class YSweetProvider {
if (emit) {
this.emit(EVENT_LOCAL_CHANGES, false)
}

this.receivedAtLeastOneSyncResponse = true
}

private setStatus(status: YSweetStatus) {
Expand All @@ -282,7 +302,7 @@ export class YSweetProvider {
return
}

if (this.indexedDBProvider && origin !== this.indexedDBProvider) {
if (this.indexedDBProvider && origin === this.indexedDBProvider) {
// Ignore updates from our own IndexedDB provider.
return
}
Expand Down Expand Up @@ -482,6 +502,8 @@ export class YSweetProvider {
this.checkSync()
this.broadcastAwareness()
this.resetHeartbeat()

this.receivedAtLeastOneSyncResponse = false
}

private receiveMessage(event: MessageEvent) {
Expand Down

0 comments on commit 96a6261

Please sign in to comment.