Skip to content

Instantly share code, notes, and snippets.

@jschr
Last active August 3, 2016 19:09
Show Gist options
  • Save jschr/e69ee94f66811b17bb7bb3717a89a6e6 to your computer and use it in GitHub Desktop.
Save jschr/e69ee94f66811b17bb7bb3717a89a6e6 to your computer and use it in GitHub Desktop.
chrome local storage module for redux-persist
export default {
getItem(key, cb) {
chrome.storage.local.get(null, (data) => {
if (chrome.runtime.lastError) return cb(chrome.runtime.lastError)
// data will be null if local storage is empty
data = data || {}
cb(null, data[key])
})
},
setItem(key, value, cb) {
chrome.storage.local.set({ [key]: value }, () => {
if (chrome.runtime.lastError) return cb(chrome.runtime.lastError)
cb(null)
})
},
removeItem(key, cb) {
chrome.storage.local.remove(key, () => {
if (chrome.runtime.lastError) return cb(chrome.runtime.lastError)
cb(null)
})
},
getAllKeys(cb) {
chrome.storage.local.get(null, (data) => {
if (chrome.runtime.lastError) return cb(chrome.runtime.lastError)
data = data || {}
cb(null, Object.keys(data))
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment