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) Prevent state updates on unmounted components #446

Merged
merged 1 commit into from
Dec 11, 2024
Merged

Conversation

denniskigen
Copy link
Member

@denniskigen denniskigen commented Dec 11, 2024

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR fixes potential memory leaks by preventing state updates on unmounted components. It does so by adding cleanup functions to useEffect hook that fetch data. Per the React docs, if your effect fetches some data, the cleanup function should either abort the fetch or ignore its result.

This PR adds abort controller signals to effects that directly leverage openmrsFetch and implements the ignore flag pattern for custom promises (like loadDependencies) that don't directly use openmrsFetch.

Screenshots

Related Issue

Other

Big caveat - I've not actually tested that this code works without breaking anything.

This PR fixes potential memory leaks by preventing state updates on unmounted components. It does so by adding cleanup functions to useEffect hook that fetch data. Per the React docs, if your effect fetches some data, the cleanup function should either abort the fetch or ignore its result. This PR adds AbortController to cancel network requests via openmrsFetch. It also uses the ignore flag pattern for custom promises (like loadDependencies) that don't directly use openmrsFetch.
Copy link

Size Change: +174 B (+0.01%)

Total Size: 1.26 MB

ℹ️ View Unchanged
Filename Size Change
dist/151.js 382 kB 0 B
dist/219.js 264 kB +135 B (+0.05%)
dist/225.js 2.57 kB 0 B
dist/277.js 1.85 kB 0 B
dist/289.js 88.9 kB 0 B
dist/300.js 645 B 0 B
dist/335.js 968 B 0 B
dist/353.js 3.02 kB 0 B
dist/41.js 3.37 kB 0 B
dist/540.js 2.63 kB 0 B
dist/55.js 758 B 0 B
dist/585.js 112 kB 0 B
dist/635.js 14.4 kB 0 B
dist/690.js 11.5 kB 0 B
dist/70.js 483 B 0 B
dist/979.js 6.87 kB 0 B
dist/99.js 691 B 0 B
dist/993.js 3.09 kB 0 B
dist/main.js 355 kB +39 B (+0.01%)
dist/openmrs-esm-form-engine-lib.js 3.79 kB 0 B

compressed-size-action

Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

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

We probably need to make similar updates across the application. These would actually make great intro tickets!

@ibacher ibacher merged commit 39c7051 into main Dec 11, 2024
6 checks passed
@ibacher ibacher deleted the fix/state-updates branch December 11, 2024 15:19
@pirupius pirupius mentioned this pull request Dec 18, 2024
3 tasks
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.

2 participants