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(middleware/persist): ensure persist does not drop updates in onRehydrateStorage when using a synchronous storage API #1689

Merged
merged 6 commits into from
Mar 31, 2023
Merged

fix(middleware/persist): ensure persist does not drop updates in onRehydrateStorage when using a synchronous storage API #1689

merged 6 commits into from
Mar 31, 2023

Conversation

coffeebeats
Copy link
Contributor

Related Issues or Discussions

Fixes #1688

Summary

See #1688 for a detailed description of the problem. This PR resolves the issue by ensuring stateFromStorage gets updated to the most recent value. I also added tests verifying this update pattern works for both synchronous and asynchronous storage APIs.

One note for reviewers: I added a comment describing how, in the asynchronous case, it's possible that the state passed to postRehydrationCallback is not the most up-to-date state. This would occur if there was a state update between callbacks in the hydration promise chain. Please let me know if I should leave the comment as-is, make the change, or simply remove the comment.

Check List

  • yarn run prettier for formatting code and docs

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 14, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0698a73:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration
@pavlobu/zustand demo Configuration
zustand-4.3.X-bug (forked) Issue #1688

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

looks correct.
can anyone else review too?

{
name: 'test-storage',
storage: createJSONStorage(() => storage),
onRehydrateStorage: () => (s) => s?.inc(),
Copy link
Member

Choose a reason for hiding this comment

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

Do we need ??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do - the type signature for the method returned by onRehydrateStorage is (state?: S, error?: unknown) => void, so state might not be defined. If somehow s is undefined here, the test will fail as expected.

Copy link
Collaborator

@sewera sewera left a comment

Choose a reason for hiding this comment

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

Looks correct also to me.

@sewera
Copy link
Collaborator

sewera commented Mar 14, 2023

It's a tricky topic that will not have to be rediscovered with this comment, so I'd leave it as-is.

@coffeebeats
Copy link
Contributor Author

Does this also need to be fixed for the old implementation? I originally assumed no, but after looking it some more it seems like it probably should be. If that's the case, are there separate tests for the old implementation?

@dai-shi
Copy link
Member

dai-shi commented Mar 15, 2023

Does this also need to be fixed for the old implementation?

My plan is to leave the old implementation unchanged. People shouldn't use the deprecated one moving forward.

@dai-shi dai-shi added this to the v4.3.7 milestone Mar 31, 2023
tests/persistSync.test.tsx Outdated Show resolved Hide resolved
@dai-shi dai-shi merged commit 309c672 into pmndrs:main Mar 31, 2023
22 checks passed
bodinsamuel pushed a commit to specfy/specfy that referenced this pull request Jul 8, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [zustand](https://togithub.com/pmndrs/zustand) | [`4.3.6` ->
`4.3.9`](https://renovatebot.com/diffs/npm/zustand/4.3.6/4.3.9) |
[![age](https://badges.renovateapi.com/packages/npm/zustand/4.3.9/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/zustand/4.3.9/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/zustand/4.3.9/compatibility-slim/4.3.6)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/zustand/4.3.9/confidence-slim/4.3.6)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>pmndrs/zustand (zustand)</summary>

### [`v4.3.9`](https://togithub.com/pmndrs/zustand/releases/tag/v4.3.9)

[Compare
Source](https://togithub.com/pmndrs/zustand/compare/v4.3.8...v4.3.9)

This includes a small improvement for production build mostly for Vite
users. Docs have been improved too!

#### What's Changed

- fix(build): mode env for "import" condition" by
[@&#8203;dai-shi](https://togithub.com/dai-shi) in
[pmndrs/zustand#1845

#### New Contributors

- [@&#8203;Debbl](https://togithub.com/Debbl) made their first
contribution in
[pmndrs/zustand#1792
- [@&#8203;Nipodemos](https://togithub.com/Nipodemos) made their first
contribution in
[pmndrs/zustand#1782
- [@&#8203;vadimshvetsov](https://togithub.com/vadimshvetsov) made their
first contribution in
[pmndrs/zustand#1800
- [@&#8203;bobylito](https://togithub.com/bobylito) made their first
contribution in
[pmndrs/zustand#1810
- [@&#8203;celineling](https://togithub.com/celineling) made their first
contribution in
[pmndrs/zustand#1804
- [@&#8203;valerii15298](https://togithub.com/valerii15298) made their
first contribution in
[pmndrs/zustand#1819
- [@&#8203;Hecmatyar](https://togithub.com/Hecmatyar) made their first
contribution in
[pmndrs/zustand#1816
- [@&#8203;203x](https://togithub.com/203x) made their first
contribution in
[pmndrs/zustand#1826
- [@&#8203;Ciensprog](https://togithub.com/Ciensprog) made their first
contribution in
[pmndrs/zustand#1844
- [@&#8203;Romainlg29](https://togithub.com/Romainlg29) made their first
contribution in
[pmndrs/zustand#1843
- [@&#8203;dcorb](https://togithub.com/dcorb) made their first
contribution in
[pmndrs/zustand#1847
- [@&#8203;ivoilic](https://togithub.com/ivoilic) made their first
contribution in
[pmndrs/zustand#1856
- [@&#8203;Kiku-CN](https://togithub.com/Kiku-CN) made their first
contribution in
[pmndrs/zustand#1889
- [@&#8203;LartyHD](https://togithub.com/LartyHD) made their first
contribution in
[pmndrs/zustand#1893
- [@&#8203;ChoiHyunjin](https://togithub.com/ChoiHyunjin) made their
first contribution in
[pmndrs/zustand#1903
- [@&#8203;thesuryavivek](https://togithub.com/thesuryavivek) made their
first contribution in
[pmndrs/zustand#1906
- [@&#8203;thedipankarroy](https://togithub.com/thedipankarroy) made
their first contribution in
[pmndrs/zustand#1913

**Full Changelog**:
pmndrs/zustand@v4.3.8...v4.3.9

### [`v4.3.8`](https://togithub.com/pmndrs/zustand/releases/tag/v4.3.8)

[Compare
Source](https://togithub.com/pmndrs/zustand/compare/v4.3.7...v4.3.8)

For persist middleware, a new option for createJSONStorage in introduced
to support more cases. Note that createJSONStorage isn't a required
function (and it's not very recommended as it's not type safe), and one
should create a custom storage for more use cases.

#### What's Changed

- chore: add extension in imports by
[@&#8203;dai-shi](https://togithub.com/dai-shi) in
[pmndrs/zustand#1678
- feat(middleware/persist): improve createJSONStorage for Maps by
[@&#8203;lauhon](https://togithub.com/lauhon) in
[pmndrs/zustand#1763
- chore(tests): migrate to vitest by
[@&#8203;arjunvegda](https://togithub.com/arjunvegda) in
[pmndrs/zustand#1753

#### New Contributors

- [@&#8203;JacobWeisenburger](https://togithub.com/JacobWeisenburger)
made their first contribution in
[pmndrs/zustand#1737
- [@&#8203;spacemeowx2](https://togithub.com/spacemeowx2) made their
first contribution in
[pmndrs/zustand#1742
- [@&#8203;arjunvegda](https://togithub.com/arjunvegda) made their first
contribution in
[pmndrs/zustand#1754
- [@&#8203;zc627788](https://togithub.com/zc627788) made their first
contribution in
[pmndrs/zustand#1752
- [@&#8203;arvinxx](https://togithub.com/arvinxx) made their first
contribution in
[pmndrs/zustand#1758
- [@&#8203;SilentFlute](https://togithub.com/SilentFlute) made their
first contribution in
[pmndrs/zustand#1762
- [@&#8203;dannobytes](https://togithub.com/dannobytes) made their first
contribution in
[pmndrs/zustand#1779
- [@&#8203;BLooDBRothER](https://togithub.com/BLooDBRothER) made their
first contribution in
[pmndrs/zustand#1777
- [@&#8203;lauhon](https://togithub.com/lauhon) made their first
contribution in
[pmndrs/zustand#1763

**Full Changelog**:
pmndrs/zustand@v4.3.7...v4.3.8

### [`v4.3.7`](https://togithub.com/pmndrs/zustand/releases/tag/v4.3.7)

[Compare
Source](https://togithub.com/pmndrs/zustand/compare/v4.3.6...v4.3.7)

This includes a couple of improvements in `persist` middleware.

#### What's Changed

- feat(middleware/persist): add skip hydration option
[#&#8203;405](https://togithub.com/pmndrs/zustand/issues/405) by
[@&#8203;gmanninglive](https://togithub.com/gmanninglive) in
[pmndrs/zustand#1647
- fix(middleware/persist): ensure `persist` does not drop updates in
`onRehydrateStorage` when using a synchronous storage API by
[@&#8203;coffeebeats](https://togithub.com/coffeebeats) in
[pmndrs/zustand#1689
- fix(middleware/persist): ensure argument for `onRehydrateStorage` and
`onHydrate` is defined on first hydration by
[@&#8203;coffeebeats](https://togithub.com/coffeebeats) in
[pmndrs/zustand#1692

#### New Contributors

- [@&#8203;hi-otto](https://togithub.com/hi-otto) made their first
contribution in
[pmndrs/zustand#1687
- [@&#8203;pastelmind](https://togithub.com/pastelmind) made their first
contribution in
[pmndrs/zustand#1707
- [@&#8203;gmanninglive](https://togithub.com/gmanninglive) made their
first contribution in
[pmndrs/zustand#1647
- [@&#8203;coffeebeats](https://togithub.com/coffeebeats) made their
first contribution in
[pmndrs/zustand#1689

**Full Changelog**:
pmndrs/zustand@v4.3.6...v4.3.7

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 4pm on friday,before 9am on
monday,every weekend" in timezone Europe/Paris, Automerge - At any time
(no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/specfy/specfy).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNTkuNyIsInVwZGF0ZWRJblZlciI6IjM1LjE1OS43IiwidGFyZ2V0QnJhbmNoIjoiY2hvcmUvcmVub3ZhdGVCYXNlQnJhbmNoIn0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

State updates within the 'onRehydrateStorage' callback using a synchronous storage API get dropped
3 participants