Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat #30 add params onBeforeReplace and onBeforeSave #65

Conversation

abernh
Copy link
Contributor

@abernh abernh commented Oct 18, 2021

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 and onBeforeSave

Allowing to "hook" into the process via parameters onBeforeReplace and onBeforeSave, functions which both get the current state 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, the store.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 falsyvalue is returned, the localStorage.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) in onBeforeReplace

both hooks get the current state passed and if they return a falsy value
the operation is canceled
this allows to pre-filter/-validate a state once more before it is
restored and/or saved

see also gabrielmbmb#30
@codecov
Copy link

codecov bot commented Oct 18, 2021

Codecov Report

Merging #65 (a9b8c08) into master (1fb073c) will increase coverage by 0.23%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #65      +/-   ##
==========================================
+ Coverage   98.24%   98.48%   +0.23%     
==========================================
  Files           2        2              
  Lines          57       66       +9     
  Branches       10       14       +4     
==========================================
+ Hits           56       65       +9     
  Partials        1        1              
Impacted Files Coverage Δ
src/index.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1fb073c...a9b8c08. Read the comment docs.

@gabrielmbmb gabrielmbmb merged commit c6933ac into gabrielmbmb:master Oct 23, 2021
@gabrielmbmb
Copy link
Owner

Hey @abernh , thank you so much for your PRs. Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants