Skip to content
Permalink
Browse files

fix: use localforage instance but avoid conflicting with any existing…

… localforage usage
  • Loading branch information...
danhayden committed Jul 19, 2017
1 parent 580b989 commit d918ab36d2978a60e7275e372b7a7e048303a095
Showing with 5 additions and 4 deletions.
  1. +5 −4 src/storage.js
@@ -1,11 +1,12 @@
import localforage from 'localforage'

let storage = {}
let store

function getItem (key) {
return new Promise(resolve => {
try {
localforage
store
.getItem(key)
.then(value => resolve(value || storage[key]))
.catch(() => resolve(storage[key]))
@@ -21,7 +22,7 @@ function setItem (key, value) {
try {
// avoid clone error from localforage
const safeValue = JSON.parse(JSON.stringify(value))
localforage.setItem(key, safeValue).then(resolve).catch(resolve)
store.setItem(key, safeValue).then(resolve).catch(resolve)
} catch (error) {
resolve()
}
@@ -32,15 +33,15 @@ function removeItem (key) {
if (storage[key]) delete storage[key]
return new Promise(resolve => {
try {
localforage.removeItem(key).then(resolve).catch(resolve)
store.removeItem(key).then(resolve).catch(resolve)
} catch (error) {
resolve()
}
})
}

export default function (options) {
localforage.config(options)
store = localforage.createInstance(options)
return {
getItem,
setItem,

0 comments on commit d918ab3

Please sign in to comment.
You can’t perform that action at this time.