@@ -241,7 +241,7 @@ export abstract class JupyterLabWidgetAdapter
241
241
// recreate virtual document using current path and language
242
242
this . virtual_editor . create_virtual_document ( ) ;
243
243
// reconnect
244
- this . connect_document ( this . virtual_editor . virtual_document ) . catch (
244
+ this . connect_document ( this . virtual_editor . virtual_document , true ) . catch (
245
245
console . warn
246
246
) ;
247
247
}
@@ -287,36 +287,62 @@ export abstract class JupyterLabWidgetAdapter
287
287
} ) ;
288
288
}
289
289
290
- protected async connect_document ( virtual_document : VirtualDocument ) {
290
+ /**
291
+ * Opens a connection for the document. The connection may or may
292
+ * not be initialized, yet, and depending on when this is called, the client
293
+ * may not be fully connected. In
294
+ *
295
+ * @param virtual_document a VirtualDocument
296
+ * @param send_open whether to open the document immediately
297
+ */
298
+ protected async connect_document (
299
+ virtual_document : VirtualDocument ,
300
+ send_open = false
301
+ ) : Promise < void > {
291
302
virtual_document . changed . connect ( this . document_changed , this ) ;
292
303
293
304
virtual_document . foreign_document_opened . connect (
294
305
this . on_foreign_document_opened ,
295
306
this
296
307
) ;
297
- return await this . connect ( virtual_document ) . catch ( console . warn ) ;
308
+
309
+ const connection_context = await this . connect ( virtual_document ) . catch (
310
+ console . warn
311
+ ) ;
312
+
313
+ if ( ! send_open ) {
314
+ return ;
315
+ }
316
+
317
+ if ( connection_context && connection_context . connection ) {
318
+ connection_context . connection . sendOpenWhenReady (
319
+ virtual_document . document_info
320
+ ) ;
321
+ } else {
322
+ console . warn ( `Connection for ${ virtual_document . path } was not opened` ) ;
323
+ }
298
324
}
299
325
326
+ /**
327
+ * Handler for opening a document contained in a larger document. The assumption
328
+ * is that the editor already exists for this this, and as such the document
329
+ * should be queued for immediate opening.
330
+ *
331
+ * @param host the VirtualDocument that contains the VirtualDocument in another language
332
+ * @param context information about the foreign VirtualDocument
333
+ */
300
334
protected async on_foreign_document_opened (
301
335
host : VirtualDocument ,
302
336
context : IForeignContext
303
337
) {
304
338
const { foreign_document } = context ;
305
339
306
- const connection_context = await this . connect_document ( foreign_document ) ;
340
+ await this . connect_document ( foreign_document , true ) ;
307
341
308
342
foreign_document . foreign_document_closed . connect (
309
343
this . on_foreign_document_closed ,
310
344
this
311
345
) ;
312
-
313
- if ( connection_context ) {
314
- connection_context . connection . sendOpenWhenReady (
315
- foreign_document . document_info
316
- ) ;
317
- } else {
318
- console . warn ( `Connection for ${ foreign_document . path } was not be opened` ) ;
319
- }
320
346
}
321
347
322
348
on_foreign_document_closed ( host : VirtualDocument , context : IForeignContext ) {
0 commit comments