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

[Chart.js] Listen to Stimulus disconnect event to destroy the chart #1944

Merged
merged 1 commit into from
Jan 5, 2025

Conversation

Shadow-Devil
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
Issues Fix #1408
License MIT

Adds an extra check if the chart is already connected and short circuits, since otherwise creating the new Chart would throw an error.

@carsonbot carsonbot added Bug Bug Fix Status: Needs Review Needs to be reviewed labels Jun 28, 2024
@Shadow-Devil Shadow-Devil changed the title Check if chart is already connected [Chart.js] Check if chart is already connected Jun 29, 2024
@Shadow-Devil

This comment was marked as outdated.

@Shadow-Devil
Copy link
Contributor Author

@smnandre will you be able to merge this or is something missing?

@smnandre
Copy link
Member

@smnandre will you be able to merge this or is something missing?

Hi @Shadow-Devil! Sorry we've all been very busy on our "day lifes"..

II have a doubt here i'd like the opinion on others..

This situation (a chart already initialized on the element) should not happen, right ?

So i totally agree we should add a early exit there, but.... should we throw an error ?

@kbond @WebMamba

@Shadow-Devil
Copy link
Contributor Author

@smnandre Sorry, I didn't want to sound rude😅.
I just thought this issue might have gone under the radar.

This situation (a chart already initialized on the element) should not happen, right ?

This situation happened for me, because of Google's Material Design Lite which would disconnect and reconnect the chart from the DOM.

A second solution would be to call this.chart.destroy() when disconnecting the chart.

@Kocal
Copy link
Member

Kocal commented Jul 17, 2024

Hey, either throwing the error or exiting silently won't really fix the initial issue right?

Throwing an error looks better to me, because it will really help the developer to understand that something wrong is happening.

But, if the Chart is previously initialized and connect() is called a second time, to me it's a not-wanted scenario, the Chart should NOT exists yet when connect() is called. I believe this should be fix at the root, destroying the Chart on controller disconnect().

WDYT?

@smnandre
Copy link
Member

because of Google's Material Design Lite which would disconnect and reconnect the chart from the DOM.

Everytime, or in particular scenarios ? It feels a bit strange :|

--

But, if the Chart is previously initialized and connect() is called a second time, to me it's a not-wanted scenario, the Chart should NOT exists yet when connect() is called.

That's my opinion too: in this scenario, throwing an Error after an early exit seems the correct thing to do (this PR + an error)

--

A second solution would be to call this.chart.destroy() when disconnecting the chart.

I believe this should be fix at the root, destroying the Chart on controller disconnect().

This feels a good thing to do yes! (there may be a reason why it has not be done before ( 🤷 ))

(thanks @Kocal)

@Shadow-Devil
Copy link
Contributor Author

Everytime, or in particular scenarios ? It feels a bit strange :|

Once on page reload.

But, if the Chart is previously initialized and connect() is called a second time, to me it's a not-wanted scenario, the Chart should NOT exists yet when connect() is called.

The connect method can be called multiple times by stimulus. See also the stimulus docs about lifecycle callbacks:

A disconnected controller may become connected again at a later time.
When this happens, such as after removing the controller’s element from the document and then re-attaching it, Stimulus will reuse the element’s previous controller instance, calling its connect() method multiple times.

I will try to update the PR to destroy the chart on disconnect 👍🏻

@Kocal
Copy link
Member

Kocal commented Jul 17, 2024

A disconnected controller may become connected again at a later time.
When this happens, such as after removing the controller’s element from the document and then re-attaching it, Stimulus will reuse the element’s previous controller instance, calling its connect() method multiple times.

Didn't know about that, thanks :)

@smnandre
Copy link
Member

The connect method can be called multiple times by stimulus. See also the stimulus docs about lifecycle callbacks:

Only after beeing deconnected. So if the Chart.js instance is destroyed this would not create problem right ?

@Shadow-Devil
Copy link
Contributor Author

Hm, so calling destroy in the disconnect works for my scenario, but now it has a little "stutter" since it stops the rendering and rerenders it again.
I think a better solution would be to not use connect but initialize and do all of the setup in there. This would only be called once and we don't have this creation/deletion problem. (Docs)
What do you think?

@smnandre
Copy link
Member

In your case i understand this can be not perfect... (but still don't understand why your framework deconnect then reconnect div on page load)

But to me, it seems logical to use connect there, because it can happen multiple times.

That makes me think.... is this not the reason there is no disconnect: https://ux.symfony.com/demos/live-component/chartjs

Would this demo still work if we did disconnect every time? (genuine question).

Let's continue tomorrow !

@Shadow-Devil
Copy link
Contributor Author

Shadow-Devil commented Jan 3, 2025

Would this demo still work if we did disconnect every time? (genuine question).

Pretty sure yes, since otherwise you would run into the same issue as me.
There is an overall logic error IMO: Stimulus can call the connect method multiple times, but the code inside it will throw an error if it is called a second time (without destroying the chartjs instance in between).

@smnandre
Copy link
Member

smnandre commented Jan 4, 2025

You're right @Shadow-Devil!

I'm very sorry this MR took so long.

Made a minor comment, let's fix this, please rebase + add an entry in the CHANGELOG.md file (next release will be 2.23)
... and let's merge this :)

Copy link

github-actions bot commented Jan 4, 2025

📊 Packages dist files size difference

ℹ️ No difference in dist packagesFiles.

@Shadow-Devil
Copy link
Contributor Author

Hi @smnandre I rebased the PR, added your review suggestion and added an entry in the CHANGELOG file.

Please let me know if there is anything else you need from me :)

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Jan 4, 2025
Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your patience @Shadow-Devil

@Kocal Kocal changed the title [Chart.js] Check if chart is already connected [Chart.js] Listen to Stimulus disconnect event to destroy the chart Jan 5, 2025
@Kocal
Copy link
Member

Kocal commented Jan 5, 2025

Thank you @Shadow-Devil.

@Kocal Kocal merged commit fd26660 into symfony:2.x Jan 5, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Chart.js] Stimulus connect is called multiple time causing errors
4 participants