Skip to content

Only unbind events passed from the parent component#613

Merged
hustcc merged 1 commit intohustcc:masterfrom
gggritso:fix/finish-event-race-condition
Nov 6, 2025
Merged

Only unbind events passed from the parent component#613
hustcc merged 1 commit intohustcc:masterfrom
gggritso:fix/finish-event-race-condition

Conversation

@gggritso
Copy link
Contributor

@gggritso gggritso commented Nov 4, 2025

There is a rare race condition in echarts-for-react where the "finished" event might not fire if the component props were updated very soon after initialization. There is a repro case in this repo: https://github.com/gggritso/echarts-race

To run the repro case, run npm install, and then npm run dev. When the page loads, you'll see that the chart does not automatically resize when the page changes.

Screen.Recording.2025-11-04.at.1.50.48.PM.mov

Here are the execution steps:

  • EChartsReactCore mounts. this.initEchartsInstance is called, and attaches a handler for the "finished" event
  • The parent component re-mounts from a useLayoutEffect call. EChartsReactCore is re-rendered, and componentDidUpdate is called. It sees that the onEvents prop changed, and calls offEvents with all the events
  • offEvents iterates through the events, and unbinds all of them using instance.off. events has a "finished" handler, so offEvents removes all finish handlers including the one added by initiEchartsInstance, since it doesn't specify which handler to remove
  • the "finished" callback never fires, so resize observer is never attached

This PR makes a change where offEvents only removes the handlers that changed between the renders, rather than all handlers. This preserves the "finished" handler that was attached during mount.

`initEchartsInstance` attached a handler for the `"finish"` event. If
`componentDidUpdate` is called between attaching the handler, and firing
the handler and the `onEvents` prop changed, this will fire
`"offEvents"` which will remove the handler for the `"finish"` event.
This means the finish handler never fires, so the chart auto-resize is
not attached, and the ready event handler never fires.

Instead, only remove the events that are on the previous props, and
leave the `"finish"` callback alone.
@gggritso
Copy link
Contributor Author

gggritso commented Nov 5, 2025

@hustcc would you mind taking a look? I'm happy to make any necessary changes, but as far as I can tell this is a pretty straight-forward bug fix.

@coveralls
Copy link

Coverage Status

coverage: 94.737%. remained the same
when pulling 2707239 on gggritso:fix/finish-event-race-condition
into 3739822 on hustcc:master.

@hustcc hustcc merged commit a6945e8 into hustcc:master Nov 6, 2025
3 checks passed
@hustcc
Copy link
Owner

hustcc commented Nov 6, 2025

https://github.com/hustcc/echarts-for-react/releases/tag/v3.0.5

gggritso added a commit to getsentry/sentry that referenced this pull request Nov 6, 2025
There's a bug in Explore charts, where the chart doesn't resize
correctly when the browser size changes. This bug is caused by a wacky
race condition in `echarts-for-react`, which was fixed in
hustcc/echarts-for-react#613 by yours truly, and
made its way into 3.0.5
Ahmed-Labs pushed a commit to getsentry/sentry that referenced this pull request Nov 6, 2025
There's a bug in Explore charts, where the chart doesn't resize
correctly when the browser size changes. This bug is caused by a wacky
race condition in `echarts-for-react`, which was fixed in
hustcc/echarts-for-react#613 by yours truly, and
made its way into 3.0.5
Jesse-Box pushed a commit to getsentry/sentry that referenced this pull request Nov 12, 2025
There's a bug in Explore charts, where the chart doesn't resize
correctly when the browser size changes. This bug is caused by a wacky
race condition in `echarts-for-react`, which was fixed in
hustcc/echarts-for-react#613 by yours truly, and
made its way into 3.0.5
andrewshie-sentry pushed a commit to getsentry/sentry that referenced this pull request Nov 13, 2025
There's a bug in Explore charts, where the chart doesn't resize
correctly when the browser size changes. This bug is caused by a wacky
race condition in `echarts-for-react`, which was fixed in
hustcc/echarts-for-react#613 by yours truly, and
made its way into 3.0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants