Skip to content

Commit

Permalink
Merge branch 'main' into paulgb/fix-provider-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb authored Dec 18, 2024
2 parents e703638 + 87f4224 commit 0fd490e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 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 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 0fd490e

Please sign in to comment.