Skip to content

Commit 009f93f

Browse files
committed
remove old chart instance when screen resizes to prevent memory leak
1 parent bb0a27e commit 009f93f

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/apexcharts.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,9 @@ export default class ApexCharts {
399399
window.removeEventListener('resize', this.windowResizeHandler)
400400

401401
removeResizeListener(this.el.parentNode, this.parentResizeHandler)
402-
// remove the chart's instance from the global Apex._chartInstances
403-
const chartID = this.w.config.chart.id
404-
if (chartID) {
405-
Apex._chartInstances.forEach((c, i) => {
406-
if (c.id === Utils.escapeString(chartID)) {
407-
Apex._chartInstances.splice(i, 1)
408-
}
409-
})
410-
}
402+
403+
this._removeOldChartInstance()
404+
411405
new Destroy(this.ctx).clear({ isUpdating: false })
412406
}
413407

@@ -545,6 +539,8 @@ export default class ApexCharts {
545539
return resolve(this)
546540
}
547541

542+
this._removeOldChartInstance()
543+
548544
this.lastUpdateOptions = Utils.clone(options)
549545

550546
new Destroy(this.ctx).clear({ isUpdating: true })
@@ -795,7 +791,19 @@ export default class ApexCharts {
795791
this._windowResize()
796792
}
797793
}
798-
794+
/**
795+
* Remove the chart's old instance from the global Apex._chartInstances
796+
*/
797+
_removeOldChartInstance() {
798+
const chartID = this.w.config.chart.id
799+
if (chartID) {
800+
Apex._chartInstances.forEach((c, i) => {
801+
if (c.id === Utils.escapeString(chartID)) {
802+
Apex._chartInstances.splice(i, 1)
803+
}
804+
})
805+
}
806+
}
799807
/**
800808
* Handle window resize and re-draw the whole chart.
801809
*/

0 commit comments

Comments
 (0)