Skip to content

Commit 2099f88

Browse files
fix: clean up resize listener when view gets removed
Fixes plotly#3100 If this is not cleaned up, we see a lot of stacktraces in the JS console: index.js?v=20220623202309:2 Uncaught (in promise) Error: Resize must be passed a displayed plot div element. at index.js?v=20220623202309:2:2385247 at new Promise (<anonymous>) at b.resize (index.js?v=20220623202309:2:2385199) at h.autosizeFigure (index.js?v=20220623202309:2:3736430) at index.js?v=20220623202309:2:3736222 Also, the destroy methods is renamed to remove. Destroy will never be called, but remove is.
1 parent 1f19f5f commit 2099f88

File tree

1 file changed

+8
-5
lines changed
  • packages/javascript/jupyterlab-plotly/src

1 file changed

+8
-5
lines changed

packages/javascript/jupyterlab-plotly/src/Figure.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ export class FigureModel extends DOMWidgetModel {
804804
*/
805805
export class FigureView extends DOMWidgetView {
806806
viewID: string;
807+
resizeEventListener: () => void;
807808

808809
/**
809810
* The perform_render method is called by processPhosphorMessage
@@ -920,10 +921,10 @@ export class FigureView extends DOMWidgetView {
920921
xaxis: axisHidden,
921922
yaxis: axisHidden,
922923
});
923-
924-
window.addEventListener("resize", function () {
925-
that.autosizeFigure();
926-
});
924+
this.resizeEventListener = () => {
925+
this.autosizeFigure();
926+
}
927+
window.addEventListener("resize", this.resizeEventListener);
927928
break;
928929
case "after-attach":
929930
// Rendering actual figure in the after-attach event allows
@@ -953,8 +954,10 @@ export class FigureView extends DOMWidgetView {
953954
* Purge Plotly.js data structures from the notebook output display
954955
* element when the view is destroyed
955956
*/
956-
destroy() {
957+
remove() {
958+
super.remove();
957959
Plotly.purge(this.el);
960+
window.removeEventListener("resize", this.resizeEventListener);
958961
}
959962

960963
/**

0 commit comments

Comments
 (0)