Skip to content

Fix removing event listeners#541

Merged
kurkle merged 1 commit intochartjs:masterfrom
joshkel:remove-events
Jun 8, 2021
Merged

Fix removing event listeners#541
kurkle merged 1 commit intochartjs:masterfrom
joshkel:remove-events

Conversation

@joshkel
Copy link
Copy Markdown
Contributor

@joshkel joshkel commented Jun 7, 2021

The plugin only removed its event listeners if it could access the chart's canvas. However, Chart.js clears its canvas before notifying plugins, so this didn't work:

https://github.com/chartjs/Chart.js/blob/v3.3.2/src/core/core.controller.js#L857

I noticed this when I destroyed and recreated a Chart.js object against the same HTML <canvas> element; the original chart's event handlers still fired and threw errors because the getState object had been removed and no longer had needed information.

To address this, I'm now saving the event listener's target as a custom property on the event listener function; that way, removeHandler always knows exactly what the target is.

I also noticed that mouseUp was calling removeHandler with incorrect parameters, so I fixed it.

The plugin only removed its event listeners if it could access the chart's canvas.  However, Chart.js clears its canvas before notifying plugins, so this didn't work:

https://github.com/chartjs/Chart.js/blob/v3.3.2/src/core/core.controller.js#L857

I noticed this when I destroyed and recreated a Chart.js object against the same HTML `<canvas>` element; the original chart's event handlers still fired and threw errors because the `getState` object had been removed and no longer had needed information.

To address this, I'm now saving the event listener's target as a custom property on the event listener function; that way, `removeHandler` always knows exactly what the target is.

I also noticed that `mouseUp` was calling `removeHandler` with incorrect parameters, so I fixed it.
@kurkle
Copy link
Copy Markdown
Member

kurkle commented Jun 7, 2021

Fixes #533?

@joshkel
Copy link
Copy Markdown
Contributor Author

joshkel commented Jun 8, 2021

From a simple local test (with only basic plot features enabled), yes, it looks like it fixes #533.

@kurkle kurkle linked an issue Jun 8, 2021 that may be closed by this pull request
@kurkle kurkle merged commit 75bce9a into chartjs:master Jun 8, 2021
@kurkle kurkle added the bug label Jun 8, 2021
@joshkel joshkel deleted the remove-events branch June 8, 2021 11:23
@kurkle kurkle added this to the 1.1.0 milestone Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chart instances persist in memory after destroy

2 participants