Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8672b5e

Browse files
authored
Account for scenarios where module name is undefined (#11248)
For #11241
1 parent 1768483 commit 8672b5e

File tree

1 file changed

+4
-81
lines changed

1 file changed

+4
-81
lines changed

src/client/datascience/ipywidgets/ipyWidgetScriptSource.ts

+4-81
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
'use strict';
55
import type * as jupyterlabService from '@jupyterlab/services';
6-
import type * as serialize from '@jupyterlab/services/lib/kernel/serialize';
76
import { sha256 } from 'hash.js';
87
import { inject, injectable } from 'inversify';
98
import { IDisposable } from 'monaco-editor';
109
import * as path from 'path';
1110
import { Event, EventEmitter, Uri } from 'vscode';
12-
import type { Data as WebSocketData } from 'ws';
1311
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
1412
import { traceError, traceInfo } from '../../common/logger';
1513
import { IFileSystem } from '../../common/platform/types';
@@ -29,13 +27,7 @@ import {
2927
InteractiveWindowMessages,
3028
IPyWidgetMessages
3129
} 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';
3931
import { IPyWidgetScriptSourceProvider } from './ipyWidgetScriptSourceProvider';
4032
import { WidgetScriptSource } from './types';
4133
// tslint:disable: no-var-requires no-require-imports
@@ -51,13 +43,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
5143
public get postInternalMessage(): Event<{ message: string; payload: any }> {
5244
return this.postInternalMessageEmitter.event;
5345
}
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-
}
6146
private readonly resourcesMappedToExtensionFolder = new Map<string, Promise<Uri>>();
6247
private notebookIdentity?: Uri;
6348
private postEmitter = new EventEmitter<{
@@ -75,13 +60,10 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
7560
private scriptProvider?: IPyWidgetScriptSourceProvider;
7661
private disposables: IDisposable[] = [];
7762
private interpreterForWhichWidgetSourcesWereFetched?: PythonInterpreter;
78-
private kernelSocketInfo?: KernelSocketInformation;
79-
private subscribedToKernelSocket: boolean = false;
8063
/**
8164
* Key value pair of widget modules along with the version that needs to be loaded.
8265
*/
83-
private pendingModuleRequests = new Map<string, string>();
84-
private jupyterSerialize?: typeof serialize;
66+
private pendingModuleRequests = new Map<string, string | undefined>();
8567
private readonly uriConversionPromises = new Map<string, Deferred<Uri>>();
8668
private readonly targetWidgetScriptsFolder: string;
8769
private readonly createTargetWidgetScriptsFolder: Promise<string>;
@@ -195,9 +177,9 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
195177
* Send the widget script source for a specific widget module & version.
196178
* This is a request made when a widget is certainly used in a notebook.
197179
*/
198-
private async sendWidgetSource(moduleName: string, moduleVersion: string) {
180+
private async sendWidgetSource(moduleName?: string, moduleVersion: string = '*') {
199181
// Standard widgets area already available, hence no need to look for them.
200-
if (moduleName.startsWith('@jupyter')) {
182+
if (!moduleName || moduleName.startsWith('@jupyter')) {
201183
return;
202184
}
203185
if (!this.notebook || !this.scriptProvider) {
@@ -264,7 +246,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
264246
if (!this.notebook) {
265247
return;
266248
}
267-
this.subscribeToKernelSocket();
268249
this.notebook.onDisposed(() => this.dispose());
269250
// When changing a kernel, we might have a new interpreter.
270251
this.notebook.onKernelChanged(
@@ -285,64 +266,6 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal
285266
);
286267
this.handlePendingRequests();
287268
}
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-
}
346269
private handlePendingRequests() {
347270
const pendingModuleNames = Array.from(this.pendingModuleRequests.keys());
348271
while (pendingModuleNames.length) {

0 commit comments

Comments
 (0)