Feat #30 add params onBeforeReplace and onBeforeSave #65
+104
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
is already discussed in #30 - if the store structure changes, an old structure might be pulled into the store, causing new functionality to break.
Solution: new parameters
onBeforeReplace
andonBeforeSave
Allowing to "hook" into the process via parameters
onBeforeReplace
andonBeforeSave
, functions which both get the currentstate
as parameter, allows the developer to make adjustments before the state is altered.Functionality
onBeforeReplace(state): state|falsy
Is triggered with the restored-from-localstorage
state
.If a
falsy
value is returned, thestore.replaceState
will not be executed.This allows e.g. to validate a state before it is pushed into the store.
Another usecase would be removing of some parts of the store state.
onBeforeSave(state): state|falsy
Is triggered after filtering and before state is saved to local storage.
If a
falsy
value is returned, thelocalStorage.setItem
will not be executed.This allows e.g. to add additional information to the state, e.g. a time flag
state.__time = Date.now()
, which in return can be validated (and filtered out again) inonBeforeReplace