Giter VIP home page Giter VIP logo

Comments (5)

ArtemZip avatar ArtemZip commented on June 20, 2024 1

solved by this https://medium.com/@artemzip/chrome-storage-with-vue-js-in-chrome-extension-ec268a5289d5

from vuex-persist.

evanb2 avatar evanb2 commented on June 20, 2024

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.

VSKut avatar VSKut commented on June 20, 2024

https://gist.github.com/VSKut/f930eb78755d7ffb5227a4cd327cd7f4

from vuex-persist.

mrleblanc101 avatar mrleblanc101 commented on June 20, 2024

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.

mrleblanc101 avatar mrleblanc101 commented on June 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.