Skip to content

Commit 00b7111

Browse files
committed
Misc
1 parent 00aa36c commit 00b7111

File tree

1 file changed

+12
-7
lines changed
  • src/webviews/webview-side/ipywidgets/kernel

1 file changed

+12
-7
lines changed

src/webviews/webview-side/ipywidgets/kernel/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ class WidgetManagerComponent {
7676
}
7777

7878
const outputDisposables = new Map<string, { dispose(): void }>();
79-
const htmlDisposables = new WeakMap<HTMLElement, { dispose(): void }>();
80-
const renderedWidgets = new Map<string, { widget?: { dispose: Function } }>();
79+
const renderedWidgets = new Map<string, { container: HTMLElement; widget?: { dispose: Function } }>();
8180
/**
8281
* Called from renderer to render output.
8382
* This will be exposed as a public method on window for renderer to render output.
@@ -105,8 +104,6 @@ export function renderOutput(outputItem: OutputItem, element: HTMLElement, logge
105104
}
106105
export function disposeOutput(outputId?: string) {
107106
if (outputId) {
108-
logMessage(`Disposing widget ${outputId}`);
109-
renderedWidgets.get(outputId)?.widget?.dispose();
110107
// We can't delete the widgets because they may be rerendered when we scroll them into view.
111108
// See issue: https://github.com/microsoft/vscode-jupyter/issues/10485
112109
// However we can mark them as not being currently rendered.
@@ -129,9 +126,13 @@ function renderIPyWidget(
129126
logger: (message: string) => void
130127
) {
131128
logger(`Rendering IPyWidget ${outputId} with model ${model.model_id}`);
132-
if (renderedWidgets.has(outputId)) {
129+
if (renderedWidgets.has(outputId) && renderedWidgets.get(outputId)?.container === container) {
133130
return logger('already rendering');
134131
}
132+
if (renderedWidgets.has(outputId)) {
133+
logger('Widget was already rendering for another container, dispose that widget so we can re-render it');
134+
renderedWidgets.get(outputId)?.widget?.dispose();
135+
}
135136
const output = document.createElement('div');
136137
output.className = 'cell-output cell-output';
137138
if (typeof model._vsc_test_cellIndex === 'number') {
@@ -141,9 +142,14 @@ function renderIPyWidget(
141142
ele.className = 'cell-output-ipywidget-background';
142143
container.appendChild(ele);
143144
ele.appendChild(output);
144-
renderedWidgets.set(outputId, {});
145+
renderedWidgets.set(outputId, { container });
145146
createWidgetView(model, ele)
146147
.then((w) => {
148+
if (renderedWidgets.get(outputId)?.container !== container) {
149+
logger('Widget container changed, hence disposing the widget');
150+
w?.dispose();
151+
return;
152+
}
147153
if (renderedWidgets.has(outputId)) {
148154
renderedWidgets.get(outputId)!.widget = w;
149155
}
@@ -155,7 +161,6 @@ function renderIPyWidget(
155161
}
156162
};
157163
outputDisposables.set(outputId, disposable);
158-
htmlDisposables.set(ele, disposable);
159164
// Keep track of the fact that we have successfully rendered a widget for this outputId.
160165
const statusInfo = stackOfWidgetsRenderStatusByOutputId.find((item) => item.outputId === outputId);
161166
if (statusInfo) {

0 commit comments

Comments
 (0)