Skip to content

Commit

Permalink
(fix): handle non-existent key in storage
Browse files Browse the repository at this point in the history
- if key is falsey, i.e. when storage has been cleared or has yet to
  be set to initial state, applySnapshot will fail and throw an error
  - so don't apply it if falsey, just skip the applySnapshot call
    - was thinking of applying `{}`, an empty object, if falsey, but
      this will reset the initial state of the store to its defaults
      - one might set initial state in `create({ ... })`, so that
        alternative doesn't work
    - getting the initial snapshot and setting it to itself was another
      alternative considered, but that would be quite complex with
      unnecessary operations, and might cause some unintended behavior
      due to whitelists, blacklists, etc applying
      - still need to think about the ramifications of those since
        applySnapshot doesn't merge, it just sets (and defaults are
        applied on top)
    - see also #1
  • Loading branch information
agilgur5 committed Jul 7, 2019
1 parent 91b8758 commit 85479ba
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const persist = (name, store, options = {}) => {
return storage.getItem(name)
.then((data) => {
const snapshot = !jsonify ? data : JSON.parse(data)
// don't apply falsey (which will error), leave store in initial state
if (!snapshot) { return }
applySnapshot(store, snapshot)
})
}
Expand Down

0 comments on commit 85479ba

Please sign in to comment.