Skip to content

Commit 85479ba

Browse files
committed
(fix): handle non-existent key in storage
- 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
1 parent 91b8758 commit 85479ba

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

persist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const persist = (name, store, options = {}) => {
3030
return storage.getItem(name)
3131
.then((data) => {
3232
const snapshot = !jsonify ? data : JSON.parse(data)
33+
// don't apply falsey (which will error), leave store in initial state
34+
if (!snapshot) { return }
3335
applySnapshot(store, snapshot)
3436
})
3537
}

0 commit comments

Comments
 (0)