-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement events for sync status (#353)
`y-websocket` has a `synced` flag, but it is a bit misleading because it only tells whether the initial handshake was completed. Knowing whether a document was synced _in general_ for `y-protocols` is a bit tricky, because we would need to compare the state vector and delete set. When using WebSockets, we can take advantage of the ordering guarantee and synchronous message processing in Y-Sweet to implement a very simple check of sync. The client stores two numbers, `lastSyncSent` and `lastSyncAcked`. When it sends an update to the server, it also increments `lastSyncSent` and then sends a separate `messageSyncStatus` message containing that number as the payload. When the server receives a `messageSyncStatus`, it simply echoes it verbatim. When the client receives the `messageSyncStatus` in return, it updates `lastSyncAcked` to the payload of the message. If `lastSyncAcked` = `lastSyncSent`, we know that all messages since the last update have been processed. Otherwise, there are outstanding changes. This builds on #352 and will enable #306. Demo: https://github.com/user-attachments/assets/60d4aec2-f025-4e84-a594-28fbc84c67cb
- Loading branch information
Showing
10 changed files
with
402 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { YDocProvider } from '@y-sweet/react' | ||
import { randomId } from '@/lib/utils' | ||
import { ColorGrid } from './ColorGrid' | ||
import StateIndicator from '@/components/StateIndicator' | ||
|
||
export default function Home({ searchParams }: { searchParams: { doc: string } }) { | ||
const docId = searchParams.doc ?? randomId() | ||
return ( | ||
<YDocProvider docId={docId} setQueryParam="doc" authEndpoint="/api/auth"> | ||
<ColorGrid /> | ||
<div className="p-4 lg:p-8"> | ||
<StateIndicator /> | ||
<ColorGrid /> | ||
</div> | ||
</YDocProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use client' | ||
|
||
import { STATUS_CONNECTED } from '@y-sweet/client' | ||
import { useConnectionStatus, useHasLocalChanges } from '@y-sweet/react' | ||
|
||
export default function StateIndicator() { | ||
let connectionStatus = useConnectionStatus() | ||
let hasLocalChanges = useHasLocalChanges() | ||
|
||
let statusColor = connectionStatus === STATUS_CONNECTED ? 'bg-green-500' : 'bg-red-500' | ||
let syncedColor = hasLocalChanges ? 'bg-red-500' : 'bg-green-500' | ||
|
||
return ( | ||
<div className="mb-4"> | ||
<div className="flex flex-row items-center text-xs space-x-1 w-fit bg-white rounded-md p-1 text-gray-500"> | ||
<div>CONNECTED:</div> | ||
<div | ||
className={`w-3 h-3 rounded-full transition-colors ${statusColor}`} | ||
title={connectionStatus} | ||
></div> | ||
<div>SYNCED:</div> | ||
<div | ||
className={`w-3 h-3 rounded-full transition-colors ${syncedColor}`} | ||
title={hasLocalChanges ? 'Unsynced local changes.' : 'No unsynced local changes.'} | ||
></div> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.