Skip to content

Commit 49c2bbd

Browse files
committed
always delete ydoc on disconnect
1 parent d23e9f7 commit 49c2bbd

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/docprovider/ydrive.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export class RtcContentProvider
143143
if (typeof options.format !== 'string') {
144144
return;
145145
}
146-
147146

148147
try {
149148
const provider = new WebSocketProvider({

src/docprovider/yprovider.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,10 @@ export class WebSocketProvider implements IDocumentProvider {
9494

9595
private async _connect(): Promise<void> {
9696
// Fetch file ID from the file ID service.
97-
let resp = await requestAPI(
98-
"api/fileid/index?path=" + this._path,
99-
{
100-
method: "POST"
101-
}
102-
)
103-
const fileId = resp["id"];
97+
const resp = await requestAPI(`api/fileid/index?path=${this._path}`, {
98+
method: 'POST'
99+
});
100+
const fileId: string = resp['id'];
104101

105102
this._yWebsocketProvider = new YWebsocketProvider(
106103
this._serverUrl,
@@ -131,18 +128,33 @@ export class WebSocketProvider implements IDocumentProvider {
131128
this._awareness.setLocalStateField('user', user.identity);
132129
}
133130

131+
/**
132+
* Handles disconnections from the YRoom Websocket.
133+
*
134+
* TODO: handle disconnections more gracefully by reseting the YDoc to an
135+
* empty state on disconnect. Unfortunately the shared model does not provide
136+
* any methods for this, so we are just asking disconnected clients to
137+
* refresh for now.
138+
*
139+
* TODO: distinguish between disconnects when server YDoc history is the same
140+
* (i.e. SS1 + SS2 is sufficient), and when the history
141+
* differs (client's YDoc has to be reset before SS1 + SS2).
142+
*/
134143
private _onConnectionClosed = (event: any): void => {
135-
if (event.code === 1003) {
136-
console.error('Document provider closed:', event.reason);
137-
138-
showErrorMessage(this._trans.__('Document session error'), event.reason, [
139-
Dialog.okButton()
140-
]);
144+
// Log error to console for debugging
145+
console.error('WebSocket connection was closed. Close event: ', event);
146+
147+
// Show dialog to tell user to refresh the page
148+
showErrorMessage(
149+
this._trans.__('Document session error'),
150+
'Please refresh the browser tab.',
151+
[Dialog.okButton()]
152+
);
141153

142-
// Dispose shared model immediately. Better break the document model,
143-
// than overriding data on disk.
144-
this._sharedModel.dispose();
145-
}
154+
// Delete this client's YDoc by disposing of the shared model.
155+
// This is the only way we know of to stop `y-websocket` from constantly
156+
// attempting to re-connect.
157+
this._sharedModel.dispose();
146158
};
147159

148160
private _onSync = (isSynced: boolean) => {

0 commit comments

Comments
 (0)