Skip to content
Discussion options

You must be logged in to vote
// state.js
import { proxy, subscribe } from 'valtio'

const emptySymbol = Symbol('Empty Value')

// First create a proxy with default values
export const state = proxy({
  isLoaded: false, // optional
  foo: emptySymbol
})

// Then load the stored values asynchronously
chrome.storage.local.get(['foo'], (result) => {
  if (result.foo) {
    const storedState = JSON.parse(result.foo)
    // Update the proxy with stored values
    Object.assign(state, storedState)
  }

  // state is loaded, so we can tell our application to use the values
  state.isLoaded = true
})

// Set up subscription to persist changes
subscribe(state, () => {
  chrome.storage.local.set({ foo: JSON.stringify(state) })
})

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@buiducnhat
Comment options

Comment options

You must be logged in to vote
3 replies
@overthemike
Comment options

@buiducnhat
Comment options

@overthemike
Comment options

Answer selected by buiducnhat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants