Compare commits

...

2 commits

Author SHA1 Message Date
lain
3880da9749 Localforage 2: Add support for localstorage. 2020-06-12 14:30:30 +02:00
lain
94a24e1754 Localforage: Add simple replacement. 2020-06-10 16:28:31 +02:00
3 changed files with 79 additions and 11 deletions

View file

@ -0,0 +1,65 @@
const wrap = (request) => {
return new Promise((resolve, reject) => {
request.onerror = (event) => reject(event)
request.onsuccess = (event) => resolve(event.target.result)
})
}
const getDb = (database, version) => {
const request = window.indexedDB.open(database, version)
request.onupgradeneeded = (event) => {
const db = event.target.result
db.createObjectStore('keyvaluepairs')
}
return wrap(request)
}
const storageBuilder = async (database = 'storage_2', version = 1) => {
try {
let db = await getDb(database, version)
const storage = {
getItem: async (key) => {
const dataRequest =
db
.transaction(['keyvaluepairs'])
.objectStore('keyvaluepairs')
.get(key)
const dataRequestPromise = wrap(dataRequest)
return dataRequestPromise
},
setItem: async (key, value) => {
const dataRequest =
db
.transaction(['keyvaluepairs'], 'readwrite')
.objectStore('keyvaluepairs')
.put(value, key)
return wrap(dataRequest)
}
}
return storage
} catch (e) {
console.log("Can't get indexeddb going, let's use localstorage")
const storage = {
getItem: async (key) => {
const result = window.localStorage.getItem(`${database}/${key}`)
try {
return JSON.parse(result)
} catch (e) {
return undefined
}
},
setItem: async (key, value) => {
const json = JSON.stringify(value)
return window.localStorage.setItem(`${database}/${key}`, json)
}
}
return storage
}
}
export default storageBuilder

View file

@ -1,6 +1,5 @@
import merge from 'lodash.merge' import localforage2 from './localforage_redux'
import localforage from 'localforage' import { merge, each, get, set } from 'lodash'
import { each, get, set } from 'lodash'
let loaded = false let loaded = false
@ -22,11 +21,13 @@ const saveImmedeatelyActions = [
'clearToken' 'clearToken'
] ]
const defaultStorage = (() => { const defaultStorageBuilder = () => {
return localforage // Use this to use the existing localforage db
})() // return localforage2('localforage', 2)
return localforage2()
}
export default function createPersistedState ({ export default async function createPersistedState ({
key = 'vuex-lz', key = 'vuex-lz',
paths = [], paths = [],
getState = (key, storage) => { getState = (key, storage) => {
@ -42,9 +43,10 @@ export default function createPersistedState ({
} }
}, },
reducer = defaultReducer, reducer = defaultReducer,
storage = defaultStorage, storageBuilder = defaultStorageBuilder,
subscriber = store => handler => store.subscribe(handler) subscriber = store => handler => store.subscribe(handler)
} = {}) { } = {}) {
const storage = await storageBuilder()
return getState(key, storage).then((savedState) => { return getState(key, storage).then((savedState) => {
return store => { return store => {
try { try {

View file

@ -1,9 +1,10 @@
/* eslint-env serviceworker */ /* eslint-env serviceworker */
import localForage from 'localforage' import localForage from './lib/localforage_redux'
function isEnabled () { async function isEnabled () {
return localForage.getItem('vuex-lz') const storage = await localForage()
return storage.getItem('vuex-lz')
.then(data => data.config.webPushNotifications) .then(data => data.config.webPushNotifications)
} }