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] Check if chart is already connected #1944

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from

Conversation

Shadow-Devil
Copy link

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
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
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
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
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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
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