@@ -140,16 +140,49 @@ export class WebSocketProvider implements IDocumentProvider {
140140 this . _connect ( ) ;
141141 }
142142
143+ /**
144+ * Gets the file ID for this path. This should only be called once when the
145+ * provider connects for the first time, because any future in-band moves may
146+ * cause `this._path` to not refer to the correct file.
147+ */
148+ private async _getFileId ( ) : Promise < string | null > {
149+ let fileId : string | null = null ;
150+ try {
151+ const resp = await requestAPI ( `api/fileid/index?path=${ this . _path } ` , {
152+ method : 'POST'
153+ } ) ;
154+ if ( resp && 'id' in resp && typeof resp [ 'id' ] === 'string' ) {
155+ fileId = resp [ 'id' ] ;
156+ }
157+ } catch ( e ) {
158+ console . error ( `Could not get file ID for path '${ this . _path } '.` ) ;
159+ return null ;
160+ }
161+ return fileId ;
162+ }
163+
143164 private async _connect ( ) : Promise < void > {
144- // Fetch file ID from the file ID service.
145- const resp = await requestAPI ( `api/fileid/index?path=${ this . _path } ` , {
146- method : 'POST'
147- } ) ;
148- const fileId : string = resp [ 'id' ] ;
165+ // Fetch file ID from the file ID service, if not cached
166+ if ( ! this . _fileId ) {
167+ this . _fileId = await this . _getFileId ( ) ;
168+ }
169+
170+ // If file ID could not be retrieved, show an error dialog asking for a bug
171+ // report, as this error is irrecoverable.
172+ if ( ! this . _fileId ) {
173+ showErrorMessage (
174+ this . _trans . __ ( 'File ID error' ) ,
175+ `The file '${ this . _path } ' cannot be opened because its file ID could not be retrieved. Please report this issue on GitHub.` ,
176+ [ Dialog . okButton ( ) ]
177+ ) ;
178+ return ;
179+ }
149180
181+ // Otherwise, initialize the `YWebsocketProvider` to connect
182+ console . log ( { fileId : this . _fileId } ) ;
150183 this . _yWebsocketProvider = new YWebsocketProvider (
151184 this . _serverUrl ,
152- `${ this . _format } :${ this . _contentType } :${ fileId } ` ,
185+ `${ this . _format } :${ this . _contentType } :${ this . _fileId } ` ,
153186 this . _sharedModel . ydoc ,
154187 {
155188 disableBc : true ,
@@ -305,6 +338,7 @@ export class WebSocketProvider implements IDocumentProvider {
305338 private _sharedModel : YDocument < DocumentChange > ;
306339 private _yWebsocketProvider : YWebsocketProvider | null ;
307340 private _trans : TranslationBundle ;
341+ private _fileId : string | null = null ;
308342}
309343
310344/**
0 commit comments