Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removing event listeners #541

Merged
merged 1 commit into from
Jun 8, 2021
Merged

Conversation

joshkel
Copy link
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
Member

kurkle commented Jun 7, 2021

Fixes #533?

@joshkel
Copy link
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