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

refactor: use useSyncExternalStore to subscribe for i18n updates #1746

Merged
merged 1 commit into from
Aug 18, 2023

Conversation

vonovak
Copy link
Collaborator

@vonovak vonovak commented Aug 17, 2023

Description

this refactors the change subscription mechanism used in Lingui React Provider to use the useSyncExternalStore hook, which according to this offers better support for "concurrent rendering features like selective hydration and time slicing". That being said, I'm not sure how exactly this should be better. Maybe someone will chime in and add more insight.

Everything should work the same except:

Previously, when the Provider was re-rendered with different i18n instance of different defaultComponent, it would ignore them. This likely never was an issue, but technically speaking, it was a bug. Now that is fixed.

What I removed is detection of the cases when i18n.change would happen (catalog update, locale update) before the Provider subscribed for the change event. I removed the detection and a related test case, because I think it's a very unlikely scenario, but I can put it back if desired.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Aug 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 17, 2023 2:34pm

@vonovak vonovak changed the title refactor: use useSyncExternalStore to subscribe for context updates refactor: use useSyncExternalStore to subscribe for i18n updates Aug 17, 2023
@github-actions
Copy link

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.76 KB (0%)
./packages/detect-locale/dist/index.mjs 721 B (0%)
./packages/react/dist/index.mjs 1.66 KB (0%)
./packages/remote-loader/dist/index.mjs 7.24 KB (0%)

@codecov
Copy link

codecov bot commented Aug 17, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02% 🎉

Comparison is base (4af4448) 75.95% compared to head (365b612) 75.98%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1746      +/-   ##
==========================================
+ Coverage   75.95%   75.98%   +0.02%     
==========================================
  Files          80       80              
  Lines        2063     2065       +2     
  Branches      529      530       +1     
==========================================
+ Hits         1567     1569       +2     
  Misses        383      383              
  Partials      113      113              
Files Changed Coverage Δ
packages/react/src/I18nProvider.tsx 92.00% <100.00%> (+0.69%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@timofei-iatsenko
Copy link
Collaborator

don't have a react competency to review this one. If you think this would work better, i'll trust you.

Copy link
Contributor

@andrii-bodnar andrii-bodnar left a comment

Choose a reason for hiding this comment

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

@vonovak thank you!

@andrii-bodnar andrii-bodnar merged commit 47d4fdb into lingui:main Aug 18, 2023
16 checks passed
@timofei-iatsenko
Copy link
Collaborator

@vonovak this change increased size of react package from 2 kB to 2.27 kB due to some issue in the workflows it wasn't caught directly in this PR. Could you increase a size limit budget, or re-consider the need of this change.

@vonovak
Copy link
Collaborator Author

vonovak commented Aug 28, 2023

hi @thekip,
I believe that there should be next to no effect on bundle size for people using React 18 because the useSyncExternalStore hook is shipped as part of the react package. For version 17 there is going to be some size increase there but it's likely that the use-sync-external-store shim will be installed by those users anyway.

I therefore don't see an issue with increasing the size budget at this point.

With regard for the need for this change, it's true that this change is not in response to some bug reported or something like that, it's more of a pre-emptive change with not-so-clear-necessity. I'll try to get another opinion on this diff.

@timofei-iatsenko
Copy link
Collaborator

@vonovak got your point, just increase bundle budget, please

@vonovak
Copy link
Collaborator Author

vonovak commented Aug 28, 2023

@vonovak got your point, just increase bundle budget, please

can you send me a link to a pipeline that's failing or some more info about this? If you can't change the bundle budget, I'm not sure I'll be able to do it, I only have read rights here... Thanks :)

@timofei-iatsenko
Copy link
Collaborator

@vonovak FYI #1753

vonovak added a commit to vonovak/js-lingui that referenced this pull request Aug 31, 2023
andrii-bodnar pushed a commit that referenced this pull request Aug 31, 2023
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