Skip to content

Commit e181feb

Browse files
authored
Add a dispose method to IJupyterYWidget (#20)
* Call a 'dispose()' method of the widget when the view is disposed * Check if the output widget is disposed before disposing of the content widget
1 parent 5c1eebb commit e181feb

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/notebookrenderer/model.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IDisposable } from '@lumino/disposable';
22

33
import { IJupyterYModel } from '../types';
4-
import { IJupyterYWidgetManager } from './types';
4+
import { IJupyterYWidget, IJupyterYWidgetManager } from './types';
55

66
export class NotebookRendererModel implements IDisposable {
77
constructor(options: NotebookRendererModel.IOptions) {
@@ -26,14 +26,19 @@ export class NotebookRendererModel implements IDisposable {
2626
}
2727
}
2828

29-
createYWidget(commId: string, node: HTMLElement): void {
29+
createYWidget(
30+
commId: string,
31+
node: HTMLElement
32+
): IJupyterYWidget | undefined {
3033
if (this._kernelId) {
3134
const yModel = this._widgetManager.getWidgetModel(this._kernelId, commId);
3235
if (yModel) {
3336
const widgetFactory = this._widgetManager.getWidgetFactory(
3437
yModel.yModelName
3538
);
36-
new widgetFactory(yModel, node);
39+
if (widgetFactory) {
40+
return new widgetFactory(yModel, node);
41+
}
3742
}
3843
}
3944
}

src/notebookrenderer/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface IJupyterYWidgetManager {
2222
yWidgetFactory: IJupyterYWidgetFactory
2323
): void;
2424
getWidgetModel(kernelId: string, commId: string): IJupyterYModel | undefined;
25-
getWidgetFactory(modelName: string): any | undefined;
25+
getWidgetFactory(modelName: string): IJupyterYWidgetFactory | undefined;
2626
}
2727

2828
export const IJupyterYWidgetManager = new Token<IJupyterYWidgetManager>(
@@ -33,4 +33,5 @@ export const IJupyterYWidgetManager = new Token<IJupyterYWidgetManager>(
3333
export interface IJupyterYWidget {
3434
node: HTMLElement;
3535
yModel: IJupyterYModel;
36+
dispose?(): void;
3637
}

src/notebookrenderer/view.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Widget } from '@lumino/widgets';
44
import { NotebookRendererModel } from './model';
55
import { IRenderMime } from '@jupyterlab/rendermime';
66
import { IJupyterYModel } from '../types';
7+
import { IJupyterYWidget } from './types';
78

89
export const CLASS_NAME = 'mimerenderer-jupyterywidget';
910

@@ -25,6 +26,7 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer {
2526
if (this.isDisposed) {
2627
return;
2728
}
29+
this._ywidget?.dispose?.();
2830
this._yModel?.dispose();
2931
super.dispose();
3032
}
@@ -35,10 +37,11 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer {
3537
if (!this._yModel) {
3638
return;
3739
}
38-
this._modelFactory.createYWidget(modelId, this.node);
40+
this._ywidget = this._modelFactory.createYWidget(modelId, this.node);
3941
}
4042

4143
private _modelFactory: NotebookRendererModel;
4244
private _mimeType: string;
4345
private _yModel?: IJupyterYModel;
46+
private _ywidget?: IJupyterYWidget;
4447
}

src/notebookrenderer/widgetManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class JupyterYWidgetManager implements IJupyterYWidgetManager {
3434
return this._registry.get(kernelId)?.getModel(commId);
3535
}
3636

37-
getWidgetFactory(modelName: string) {
37+
getWidgetFactory(modelName: string): IJupyterYWidgetFactory | undefined {
3838
return this._yWidgetFactories.get(modelName);
3939
}
4040

0 commit comments

Comments
 (0)