3
3
4
4
'use strict' ;
5
5
import type * as jupyterlabService from '@jupyterlab/services' ;
6
- import type * as serialize from '@jupyterlab/services/lib/kernel/serialize' ;
7
6
import { sha256 } from 'hash.js' ;
8
7
import { inject , injectable } from 'inversify' ;
9
8
import { IDisposable } from 'monaco-editor' ;
10
9
import * as path from 'path' ;
11
10
import { Event , EventEmitter , Uri } from 'vscode' ;
12
- import type { Data as WebSocketData } from 'ws' ;
13
11
import { IApplicationShell , IWorkspaceService } from '../../common/application/types' ;
14
12
import { traceError , traceInfo } from '../../common/logger' ;
15
13
import { IFileSystem } from '../../common/platform/types' ;
@@ -29,13 +27,7 @@ import {
29
27
InteractiveWindowMessages ,
30
28
IPyWidgetMessages
31
29
} from '../interactive-common/interactiveWindowTypes' ;
32
- import {
33
- IInteractiveWindowListener ,
34
- ILocalResourceUriConverter ,
35
- INotebook ,
36
- INotebookProvider ,
37
- KernelSocketInformation
38
- } from '../types' ;
30
+ import { IInteractiveWindowListener , ILocalResourceUriConverter , INotebook , INotebookProvider } from '../types' ;
39
31
import { IPyWidgetScriptSourceProvider } from './ipyWidgetScriptSourceProvider' ;
40
32
import { WidgetScriptSource } from './types' ;
41
33
// tslint:disable: no-var-requires no-require-imports
@@ -51,13 +43,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
51
43
public get postInternalMessage ( ) : Event < { message : string ; payload : any } > {
52
44
return this . postInternalMessageEmitter . event ;
53
45
}
54
- private get deserialize ( ) : typeof serialize . deserialize {
55
- if ( ! this . jupyterSerialize ) {
56
- // tslint:disable-next-line: no-require-imports
57
- this . jupyterSerialize = require ( '@jupyterlab/services/lib/kernel/serialize' ) as typeof serialize ;
58
- }
59
- return this . jupyterSerialize . deserialize ;
60
- }
61
46
private readonly resourcesMappedToExtensionFolder = new Map < string , Promise < Uri > > ( ) ;
62
47
private notebookIdentity ?: Uri ;
63
48
private postEmitter = new EventEmitter < {
@@ -75,13 +60,10 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
75
60
private scriptProvider ?: IPyWidgetScriptSourceProvider ;
76
61
private disposables : IDisposable [ ] = [ ] ;
77
62
private interpreterForWhichWidgetSourcesWereFetched ?: PythonInterpreter ;
78
- private kernelSocketInfo ?: KernelSocketInformation ;
79
- private subscribedToKernelSocket : boolean = false ;
80
63
/**
81
64
* Key value pair of widget modules along with the version that needs to be loaded.
82
65
*/
83
- private pendingModuleRequests = new Map < string , string > ( ) ;
84
- private jupyterSerialize ?: typeof serialize ;
66
+ private pendingModuleRequests = new Map < string , string | undefined > ( ) ;
85
67
private readonly uriConversionPromises = new Map < string , Deferred < Uri > > ( ) ;
86
68
private readonly targetWidgetScriptsFolder : string ;
87
69
private readonly createTargetWidgetScriptsFolder : Promise < string > ;
@@ -195,9 +177,9 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
195
177
* Send the widget script source for a specific widget module & version.
196
178
* This is a request made when a widget is certainly used in a notebook.
197
179
*/
198
- private async sendWidgetSource ( moduleName : string , moduleVersion : string ) {
180
+ private async sendWidgetSource ( moduleName ? : string , moduleVersion : string = '*' ) {
199
181
// Standard widgets area already available, hence no need to look for them.
200
- if ( moduleName . startsWith ( '@jupyter' ) ) {
182
+ if ( ! moduleName || moduleName . startsWith ( '@jupyter' ) ) {
201
183
return ;
202
184
}
203
185
if ( ! this . notebook || ! this . scriptProvider ) {
@@ -264,7 +246,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
264
246
if ( ! this . notebook ) {
265
247
return ;
266
248
}
267
- this . subscribeToKernelSocket ( ) ;
268
249
this . notebook . onDisposed ( ( ) => this . dispose ( ) ) ;
269
250
// When changing a kernel, we might have a new interpreter.
270
251
this . notebook . onKernelChanged (
@@ -285,64 +266,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
285
266
) ;
286
267
this . handlePendingRequests ( ) ;
287
268
}
288
- private subscribeToKernelSocket ( ) {
289
- if ( this . subscribedToKernelSocket || ! this . notebook ) {
290
- return ;
291
- }
292
- this . subscribedToKernelSocket = true ;
293
- // Listen to changes to kernel socket (e.g. restarts or changes to kernel).
294
- this . notebook . kernelSocket . subscribe ( ( info ) => {
295
- // Remove old handlers.
296
- this . kernelSocketInfo ?. socket ?. removeReceiveHook ( this . onKernelSocketMessage . bind ( this ) ) ; // NOSONAR
297
-
298
- if ( ! info || ! info . socket ) {
299
- // No kernel socket information, hence nothing much we can do.
300
- this . kernelSocketInfo = undefined ;
301
- return ;
302
- }
303
-
304
- this . kernelSocketInfo = info ;
305
- this . kernelSocketInfo . socket ?. addReceiveHook ( this . onKernelSocketMessage . bind ( this ) ) ; // NOSONAR
306
- } ) ;
307
- }
308
- /**
309
- * If we get a comm open message, then we know a widget will be displayed.
310
- * In this case get hold of the name and send it up (pre-fetch it before UI makes a request for it).
311
- */
312
- private async onKernelSocketMessage ( message : WebSocketData ) : Promise < void > {
313
- // tslint:disable-next-line: no-any
314
- const msg = this . deserialize ( message as any ) ;
315
- if ( this . jupyterLab ?. KernelMessage . isCommOpenMsg ( msg ) && msg . content . target_module ) {
316
- this . sendWidgetSource ( msg . content . target_module , '' ) . catch (
317
- traceError . bind ( 'Failed to pre-load Widget Script' )
318
- ) ;
319
- } else if (
320
- this . jupyterLab ?. KernelMessage . isCommOpenMsg ( msg ) &&
321
- msg . content . data &&
322
- msg . content . data . state &&
323
- // tslint:disable-next-line: no-any
324
- ( ( msg . content . data . state as any ) . _view_module || ( msg . content . data . state as any ) . _model_module )
325
- ) {
326
- // tslint:disable-next-line: no-any
327
- const viewModule : string = ( msg . content . data . state as any ) . _view_module ;
328
- // tslint:disable-next-line: no-any
329
- const viewModuleVersion : string = ( msg . content . data . state as any ) . _view_module_version ;
330
- // tslint:disable-next-line: no-any
331
- const modelModule = ( msg . content . data . state as any ) . _model_module ;
332
- // tslint:disable-next-line: no-any
333
- const modelModuleVersion = ( msg . content . data . state as any ) . _model_module_version ;
334
- if ( viewModule ) {
335
- this . sendWidgetSource ( viewModule , modelModuleVersion || '' ) . catch (
336
- traceError . bind ( 'Failed to pre-load Widget Script' )
337
- ) ;
338
- }
339
- if ( modelModule ) {
340
- this . sendWidgetSource ( viewModule , viewModuleVersion || '' ) . catch (
341
- traceError . bind ( 'Failed to pre-load Widget Script' )
342
- ) ;
343
- }
344
- }
345
- }
346
269
private handlePendingRequests ( ) {
347
270
const pendingModuleNames = Array . from ( this . pendingModuleRequests . keys ( ) ) ;
348
271
while ( pendingModuleNames . length ) {
0 commit comments