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

Allow custom replaceState #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

robbevp
Copy link

@robbevp robbevp commented Jan 19, 2021

Summary

Add extra option to provide a custom replaceState to the plugin.
If a replaceState is provided we overwrite both the RESTORE_MUTATION and default replaceState.

Why?

I'm trying to use this plugin in a project where some of the modules get very large. One of the modules contains an object which has ±50.000 objects itself.
Currently he default way of replacing the state causing huge spikes in memory usage (usually somewhere between 1GB and 2GB). Being able to provide our own function means that we can trigger a few mutations that are optimized to work with our large objects.

Using a custom RESTORE_MUTATION doesn't work since:

  1. We use both localStorage and localForage for different modules
  2. This one mutation would get very complex to handle changes in different modules
  3. We already have mutations to set this data after fetching it from the api, so writing this again wouldn't be very DRY.

@codecov
Copy link

codecov bot commented Jan 19, 2021

Codecov Report

Merging #218 (83ba66c) into master (9b7dd48) will decrease coverage by 2.97%.
The diff coverage is 28.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #218      +/-   ##
==========================================
- Coverage   81.81%   78.84%   -2.98%     
==========================================
  Files           4        4              
  Lines          99      104       +5     
  Branches       36       38       +2     
==========================================
+ Hits           81       82       +1     
- Misses          6        8       +2     
- Partials       12       14       +2     
Impacted Files Coverage Δ
src/index.ts 75.00% <28.57%> (-3.67%) ⬇️

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 9b7dd48...83ba66c. Read the comment docs.

@nathanbrizzee
Copy link

Hi @robbevp, I have the same problem in that what we are writing into localForage (indexedDB) can get quite large (12MB) and it taxes a mobile phone. Do you have an example of a custom ReplaceState you could post with skeleton usage?

@robbevp
Copy link
Author

robbevp commented Mar 10, 2021

Hi @nathanbrizzee
You'd have to provide one/multiple mutations to handle this. IMHO it's best to write restoreState in such a way that you get a similar output as you get from your API. The mutation would be responsible for updating the store in a memory safe way.

A replaceState could then look something like:

async (store, savedState) => {
  if (savedState.items) store.commit('items/setItems', savedState.items)
}

Or could grow more complex and trigger several mutations:

async (store, savedState) => {
  if (savedState.items) store.commit('items/setItems', savedState.items)
  if (savedState.otherProp) store.commit('items/setOtherProp', savedState.otherProp)
}

For the project I needed this for, I actually went further and created a subclass that sets all the options per module (this also allows me to have a seperate store.restored per plugin). It's open source, so you can see it here. (I've skipped restoreState here, since I could just as well do this in one step.

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