@@ -89,7 +89,10 @@ class WebSocketComm {
8989 // the `Update` may be applied immediately.
9090 is_loading = false ;
9191
92- // A promise to serialize calls to `set_content`.
92+ // A promise to serialize calls to and from the Client. This is important: a
93+ // `CurrentFile` requires the Client to save, then switch to a new web page.
94+ // If an `Update` comes in, it should be applied after the `CurrentFile` is
95+ // finished executing.
9396 promise = Promise . resolve ( ) ;
9497
9598 constructor ( ws_url : string ) {
@@ -138,7 +141,8 @@ class WebSocketComm {
138141 case "Update" :
139142 // Load this data in.
140143 const current_update = value as UpdateMessageContents ;
141- // The rest of this should run after all other messages have been processed.
144+ // The rest of this should run after all other messages have
145+ // been processed.
142146 this . promise = this . promise . finally ( async ( ) => {
143147 // Check or update the `current_filename`.
144148 if ( this . current_filename === undefined ) {
@@ -154,19 +158,24 @@ class WebSocketComm {
154158 const contents = current_update . contents ;
155159 const cursor_position = current_update . cursor_position ;
156160 if ( contents !== undefined ) {
157- // I'd prefer to use a system-maintained value to determine the ready state of the iframe,
158- // such as `readyState`. However, this value only applies to the initial load of the iframe;
159- // it doesn't change when the iframe's `src` attribute is changed. So, we have to track
160- // this manually instead.
161+ // I'd prefer to use a system-maintained value to
162+ // determine the ready state of the iframe, such as
163+ // `readyState`. However, this value only applies to
164+ // the initial load of the iframe; it doesn't change
165+ // when the iframe's `src` attribute is changed. So,
166+ // we have to track this manually instead.
161167 if ( ! this . is_loading ) {
162- // Wait until after the DOM is ready, since we rely on content set in `on_dom_content_loaded` in the Client.
168+ // Wait until after the DOM is ready, since we
169+ // rely on content set in
170+ // `on_dom_content_loaded` in the Client.
163171 await set_content (
164172 contents ,
165173 current_update . cursor_position ,
166174 ) ;
167175 } else {
168- // If the page is still loading, wait until the load
169- // completes before updating the editable contents.
176+ // If the page is still loading, wait until the
177+ // load completes before updating the editable
178+ // contents.
170179 //
171180 // Construct the promise to use; this causes the
172181 // `onload` callback to be set immediately.
@@ -184,8 +193,8 @@ class WebSocketComm {
184193 }
185194 } else if ( cursor_position !== undefined ) {
186195 // We might receive a message while the Client is
187- // reloading; during this period, `scroll_to_line` isn't
188- // defined.
196+ // reloading; during this period, `scroll_to_line`
197+ // isn't defined.
189198 root_iframe ! . contentWindow ?. CodeChatEditor ?. scroll_to_line ?.(
190199 cursor_position ,
191200 ) ;
@@ -206,15 +215,16 @@ class WebSocketComm {
206215 ? "?test"
207216 : "&test"
208217 : "" ;
209- // Execute this after all other messages have been processed.
218+ // Execute this after all other messages have been
219+ // processed.
210220 this . promise = this . promise . finally ( async ( ) => {
211- // If the page is still loading, then don't save. Otherwise,
212- // save the editor contents if necessary.
221+ // If the page is still loading, then don't save.
222+ // Otherwise, save the editor contents if necessary.
213223 const cce = get_client ( ) ;
214224 await cce ?. on_save ( true ) ;
215- // Now, it's safe to load a new file.
216- // Tell the client to allow this navigation -- the
217- // document it contains has already been saved.
225+ // Now, it's safe to load a new file. Tell the client to
226+ // allow this navigation -- the document it contains has
227+ // already been saved.
218228 if ( cce !== undefined ) {
219229 cce . allow_navigation = true ;
220230 }
@@ -313,7 +323,8 @@ class WebSocketComm {
313323 current_file = ( url : URL ) => {
314324 this . promise = this . promise . finally ( ( ) => {
315325 if ( url . host === window . location . host ) {
316- // If this points to the Server, then tell the IDE to load a new file.
326+ // If this points to the Server, then tell the IDE to load a new
327+ // file.
317328 this . send_message (
318329 { CurrentFile : [ url . toString ( ) , null ] } ,
319330 ( ) => {
0 commit comments