Skip to content

Commit 573e845

Browse files
committed
✨Improve state persistence
1 parent d67fd72 commit 573e845

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/Store.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const notImplementedErrorMsg = [
3030
`You must implement 'loadState' and 'saveState' to be able `,
3131
'to save state to your preffered storage. E.g \n',
3232
'store.persist({ \n',
33-
' saveState: function(key, state, isInitialSet){/*logics to save state to storage*/}, \n',
34-
' loadState: function(key, noState){/*logics to load state from storage*/} \n',
33+
' saveState: function(key, state, isInitialSet){/*Code to save state to your storage*/}, \n',
34+
' loadState: function(key, noState){/*Code to load state from your storage*/} \n',
3535
'}) \n'
3636
].join("");
3737

@@ -237,7 +237,11 @@ export default class Store {
237237
// Clear store
238238
this.states = new Map();
239239
if (this.persistentStorage.clear) {
240-
this.persistentStorage.clear()
240+
try {
241+
this.persistentStorage.clear()
242+
} catch (error) {
243+
// Ignore errors in clearing states
244+
}
241245
}
242246

243247
if (fn) {
@@ -276,10 +280,13 @@ export default class Store {
276280
this.states.delete(key);
277281
if (
278282
StatesToRemove.get(key).persist && // Is state persisted
279-
this.persistentStorage.removeState && // Is removeState Implemented
280-
this.persistentStorage.loadState(key, EMPTY) !== EMPTY // Is state to remove available
283+
this.persistentStorage.removeState // Is removeState Implemented
281284
) {
282-
this.persistentStorage.removeState(key)
285+
try {
286+
this.persistentStorage.removeState(key);
287+
} catch (error) {
288+
// Ignore error in removing state
289+
}
283290
}
284291
});
285292

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ export {
1111
DerivedState, createDerivedState,
1212
};
1313

14-
export default {
14+
15+
const StatePool = {
1516
State, createState,
1617
Store, createStore,
1718
useState, useReducer,
1819
DerivedState, createDerivedState,
19-
};
20+
}
21+
22+
export default StatePool;

0 commit comments

Comments
 (0)