-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
chrome.storage support #251
Comments
I was having issues trying to get this plugin to work with 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 😆 |
Looking at the README and CHANGELOG, it seem like it support async storage and has done so since 0.4.0 in 2017. |
I don't pay for medium so I can't |
I'm using vuex for my Chrome extension.
And extensions use special kind of storage.
Chrome storage. It is similiar to localStorage except it is async.
https://developer.chrome.com/docs/extensions/reference/storage/
Can vuex work with it?
The text was updated successfully, but these errors were encountered: