joshnuss / svelte-persisted-store

A Svelte store that persists to localStorage
MIT License
994 stars 41 forks source link

Fix: Load initial value from storage #232

Closed joshnuss closed 8 months ago

joshnuss commented 8 months ago

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 => {
  // ...
})
github-actions[bot] commented 8 months ago

:tada: This PR is included in version 0.8.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: