Comments (5)
solved by this https://medium.com/@artemzip/chrome-storage-with-vue-js-in-chrome-extension-ec268a5289d5
from vuex-persist.
I was having issues trying to get this plugin to work with chrome.storage
for my extension so I just rolled my own and it seems to be working well:
import * as types from './mutation-types'
export const chromeStorageSyncPlugin = (store) => {
// This is just so we know the state path for each mutation
// we want to listen to since we can't derive it from the `mutation` arg.
// There may be an easier way to do this,
// or you can omit it entirely if you want to sync your entire state tree.
const STORAGE_MUTATIONS_MAP = {
[types.SET_SCREEN]: 'currentScreen',
[types.SET_IS_DRAWER_OPEN]: 'isDrawerOpen',
[types.INCREMENT_BADGE_COUNT]: 'badgeCount',
[types.CLEAR_BADGE_COUNT]: 'badgeCount',
[types.SET_ALERT_SUBSCRIPTIONS]: 'alertSubscriptions',
[types.TOGGLE_ALERT_SUBSCRIPTION]: 'alertSubscriptions',
[types.TOGGLE_ALL_ALERT_SUBSCRIPTIONS]: 'alertSubscriptions',
}
// Re-hydrate the store on init from storage.
// I'm toggling this `isLoading` state so I can show a loading screen
// to avoid UI blips when the storage data is loaded.
// Not necessary but you'll probably want to handle this somehow since it's async.
store.commit({ type: types.SET_IS_LOADING, isLoading: true })
chrome.storage.sync
.get([...Object.values(STORAGE_MUTATIONS_MAP)])
.then((storageData) => {
store.replaceState({ ...store.state, ...storageData })
})
.catch((error) => {
console.log(error)
})
.finally(() => store.commit({ type: types.SET_IS_LOADING, isLoading: false }))
store.subscribe((mutation, state) => {
if (Object.keys(STORAGE_MUTATIONS_MAP).includes(mutation.type)) {
const stateKey = STORAGE_MUTATIONS_MAP[mutation.type]
// This step is necessary to parse state values,
// they are stored as Proxys
const newValue = JSON.parse(JSON.stringify(state[stateKey]))
chrome.storage.sync.set({ [stateKey]: newValue })
}
})
}
// in your store setup...
export default createStore({
// ...
plugins: [chromeStorageSyncPlugin]
})
Hope that helps! I literally just wrote this so YMMV 😆
from vuex-persist.
https://gist.github.com/VSKut/f930eb78755d7ffb5227a4cd327cd7f4
from vuex-persist.
Looking at the README and CHANGELOG, it seem like it support async storage and has done so since 0.4.0 in 2017.
Did you try it and have problems ?
I'm currently building a WebExtension, and I noticed that vuex-persistedstate
does not support async storage, but I found this issue through Google.
from vuex-persist.
solved by this https://medium.com/@artemzip/chrome-storage-with-vue-js-in-chrome-extension-ec268a5289d5
I don't pay for medium so I can't
from vuex-persist.
Related Issues (20)
- Is the persistent data obtained after refresh old ? HOT 2
- saveState get error in detail example: Type 'string | undefined' is not assignable to type 'void | Promise<void>' HOT 1
- Incompatible localForage type HOT 2
- Newbe question on restoring store from localstore to vuex HOT 1
- Unable to access data from Vuex after rehydration in Nuxt.js HOT 4
- After loading from LocalForage, to Vuex all types of class objects are being lost. HOT 1
- When chosen storage method isn't available, how can we capture the error? HOT 1
- TypeError: hook.flushStoreModules is not a function error on fresh install
- Nuxt and vuex-persist: npm ERR! code ERESOLVE HOT 3
- How to refresh from local /store XXX.js? HOT 1
- How can I only persist one specific store js file and not all of them? HOT 1
- vuex-persist does not store in localStorage: vue3 migration HOT 2
- Question: is there a way to manually trigger restoreState?
- Question: Can I update only modified value in indexedDB store?
- Uncaught ReferenceError: require is not defined (vite, SSR) HOT 4
- my state is not persisted after page refresh HOT 1
- Not sure how to use browser.storage.sync with localForage driver HOT 2
- Unhandled Promise Rejection: QuotaExceededError: The quota has been exceeded. HOT 2
- AsyncStorage - documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-persist.