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: Load initial value from storage #232

Merged
merged 2 commits into from
Jan 4, 2024
Merged

Fix: Load initial value from storage #232

merged 2 commits into from
Jan 4, 2024

Conversation

joshnuss
Copy link
Owner

@joshnuss joshnuss commented Jan 4, 2024

This fixes a bug where the initial value set incorrectly. Closes #231

Previously, it only read the initial value inside store's closure:

writable(initialValue, set => {
  // read from storage *inside* closure
  const json = storage.getItem(key)
  const existingValue = JSON.parse(parsedValue)
  
  // ...
})

But that closure is only called on first get.

Instead, the initial read should happen before the store is even created:

// read from storage **outside** closure, before initializing store
const json = storage.getItem(key)
const initialOrExisting = json ? JSON.parse(json) : initialValue

writable(initialOrExisting, set => {
  // ...
})

@joshnuss joshnuss merged commit cc0a3d1 into master Jan 4, 2024
4 checks passed
@joshnuss joshnuss deleted the update-before-read branch January 4, 2024 20:54
Copy link

github-actions bot commented Jan 4, 2024

🎉 This PR is included in version 0.8.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling "update" before reading store at least once always returns initial data in callback
1 participant