Skip to content

Commit 8f3351b

Browse files
Fix event listener memory leak
Event listeners are attached on every draw causing a memory leak Fixed by removing event listener from event before attaching
1 parent 8a7c210 commit 8f3351b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

dist/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -136,60 +136,79 @@ var PlotlyChart = /** @class */ (function (_super) {
136136
// this.container!.on('plotly_buttonclicked', this.props.onButtonClicked);
137137
// }
138138
if (this.props.onClick) {
139+
this.container.removeAllListeners("plotly_click");
139140
this.container.on('plotly_click', this.props.onClick);
140141
}
141142
if (this.props.onClickAnnotation) {
143+
this.container.removeAllListeners("plotly_clickannotation");
142144
this.container.on('plotly_clickannotation', this.props.onClickAnnotation);
143145
}
144146
if (this.props.onDeselect) {
147+
this.container.removeAllListeners("plotly_deselect");
145148
this.container.on('plotly_deselect', this.props.onDeselect);
146149
}
147150
if (this.props.onDoubleClick) {
151+
this.container.removeAllListeners("plotly_doubleclick");
148152
this.container.on('plotly_doubleclick', this.props.onDoubleClick);
149153
}
150154
if (this.props.onFramework) {
155+
this.container.removeAllListeners("plotly_framework");
151156
this.container.on('plotly_framework', this.props.onFramework);
152157
}
153158
if (this.props.onHover) {
159+
this.container.removeAllListeners("plotly_hover");
154160
this.container.on('plotly_hover', this.props.onHover);
155161
}
156162
if (this.props.onLegendClick) {
163+
this.container.removeAllListeners("plotly_legendclick");
157164
this.container.on('plotly_legendclick', this.props.onLegendClick);
158165
}
159166
if (this.props.onLegendDoubleClick) {
167+
this.container.removeAllListeners("plotly_legenddoubleclick");
160168
this.container.on('plotly_legenddoubleclick', this.props.onLegendDoubleClick);
161169
}
162170
if (this.props.onRelayout) {
171+
this.container.removeAllListeners("plotly_relayout");
163172
this.container.on('plotly_relayout', this.props.onRelayout);
164173
}
165174
if (this.props.onRestyle) {
175+
this.container.removeAllListeners("plotly_restyle");
166176
this.container.on('plotly_restyle', this.props.onRestyle);
167177
}
168178
if (this.props.onRedraw) {
179+
this.container.removeAllListeners("plotly_redraw");
169180
this.container.on('plotly_redraw', this.props.onRedraw);
170181
}
171182
if (this.props.onSelecting) {
183+
this.container.removeAllListeners("plotly_selecting");
172184
this.container.on('plotly_selecting', this.props.onSelecting);
173185
}
174186
if (this.props.onSliderChange) {
187+
this.container.removeAllListeners("plotly_sliderchange");
175188
this.container.on('plotly_sliderchange', this.props.onSliderChange);
176189
}
177190
if (this.props.onSliderEnd) {
191+
this.container.removeAllListeners("plotly_sliderend");
178192
this.container.on('plotly_sliderend', this.props.onSliderEnd);
179193
}
180194
if (this.props.onSliderStart) {
195+
this.container.removeAllListeners("plotly_sliderstart");
181196
this.container.on('plotly_sliderstart', this.props.onSliderStart);
182197
}
183198
if (this.props.onTransitioning) {
199+
this.container.removeAllListeners("plotly_transitioning");
184200
this.container.on('plotly_transitioning', this.props.onTransitioning);
185201
}
186202
if (this.props.onTransitionInterrupted) {
203+
this.container.removeAllListeners("plotly_transitioninterrupted");
187204
this.container.on('plotly_transitioninterrupted', this.props.onTransitionInterrupted);
188205
}
189206
if (this.props.onUnHover) {
207+
this.container.removeAllListeners("plotly_unhover");
190208
this.container.on('plotly_unhover', this.props.onUnHover);
191209
}
192210
if (this.props.onEvent) {
211+
this.container.removeAllListeners("plotly_event");
193212
this.container.on('plotly_event', this.props.onEvent);
194213
}
195214
window.addEventListener('resize', this.resize);

src/index.tsx

+20-1
Original file line numberDiff line numberDiff line change
@@ -73,60 +73,79 @@ class PlotlyChart extends React.Component<IPlotlyChartProps, any> {
7373
// this.container!.on('plotly_buttonclicked', this.props.onButtonClicked);
7474
// }
7575
if (this.props.onClick) {
76+
this.container!.removeAllListeners("plotly_click");
7677
this.container!.on('plotly_click', this.props.onClick);
7778
}
7879
if (this.props.onClickAnnotation) {
80+
this.container!.removeAllListeners("plotly_clickannotation");
7981
this.container!.on('plotly_clickannotation', this.props.onClickAnnotation);
8082
}
8183
if (this.props.onDeselect) {
84+
this.container!.removeAllListeners("plotly_deselect");
8285
this.container!.on('plotly_deselect', this.props.onDeselect);
8386
}
8487
if (this.props.onDoubleClick) {
88+
this.container!.removeAllListeners("plotly_doubleclick");
8589
this.container!.on('plotly_doubleclick', this.props.onDoubleClick);
8690
}
8791
if (this.props.onFramework) {
92+
this.container!.removeAllListeners("plotly_framework");
8893
this.container!.on('plotly_framework', this.props.onFramework);
8994
}
9095
if (this.props.onHover) {
96+
this.container!.removeAllListeners("plotly_hover");
9197
this.container!.on('plotly_hover', this.props.onHover);
9298
}
9399
if (this.props.onLegendClick) {
100+
this.container!.removeAllListeners("plotly_legendclick");
94101
this.container!.on('plotly_legendclick', this.props.onLegendClick);
95102
}
96103
if (this.props.onLegendDoubleClick) {
104+
this.container!.removeAllListeners("plotly_legenddoubleclick");
97105
this.container!.on('plotly_legenddoubleclick', this.props.onLegendDoubleClick);
98106
}
99107
if (this.props.onRelayout) {
108+
this.container!.removeAllListeners("plotly_relayout");
100109
this.container!.on('plotly_relayout', this.props.onRelayout);
101110
}
102111
if (this.props.onRestyle) {
112+
this.container!.removeAllListeners("plotly_restyle");
103113
this.container!.on('plotly_restyle', this.props.onRestyle);
104114
}
105115
if (this.props.onRedraw) {
116+
this.container!.removeAllListeners("plotly_redraw");
106117
this.container!.on('plotly_redraw', this.props.onRedraw);
107118
}
108119
if (this.props.onSelecting) {
120+
this.container!.removeAllListeners("plotly_selecting");
109121
this.container!.on('plotly_selecting', this.props.onSelecting);
110122
}
111123
if (this.props.onSliderChange) {
124+
this.container!.removeAllListeners("plotly_sliderchange");
112125
this.container!.on('plotly_sliderchange', this.props.onSliderChange);
113126
}
114127
if (this.props.onSliderEnd) {
128+
this.container!.removeAllListeners("plotly_sliderend");
115129
this.container!.on('plotly_sliderend', this.props.onSliderEnd);
116130
}
117131
if (this.props.onSliderStart) {
132+
this.container!.removeAllListeners("plotly_sliderstart");
118133
this.container!.on('plotly_sliderstart', this.props.onSliderStart);
119134
}
120135
if (this.props.onTransitioning) {
136+
this.container!.removeAllListeners("plotly_transitioning");
121137
this.container!.on('plotly_transitioning', this.props.onTransitioning);
122138
}
123139
if (this.props.onTransitionInterrupted) {
140+
this.container!.removeAllListeners("plotly_transitioninterrupted");
124141
this.container!.on('plotly_transitioninterrupted', this.props.onTransitionInterrupted);
125142
}
126143
if (this.props.onUnHover) {
144+
this.container!.removeAllListeners("plotly_unhover");
127145
this.container!.on('plotly_unhover', this.props.onUnHover);
128146
}
129147
if (this.props.onEvent) {
148+
this.container!.removeAllListeners("plotly_event");
130149
this.container!.on('plotly_event', this.props.onEvent);
131150
}
132151
window.addEventListener('resize', this.resize);
@@ -210,4 +229,4 @@ class PlotlyChart extends React.Component<IPlotlyChartProps, any> {
210229
}
211230
}
212231

213-
export default PlotlyChart;
232+
export default PlotlyChart;

0 commit comments

Comments
 (0)