From 5143ae7f72c7535872188d9887dc61628add09fb Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Mar 2017 08:43:48 +0100 Subject: Switch to localforage to use indexeddb by default. --- src/lib/persisted_state.js | 47 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js index 6a17ca99..a518cb51 100644 --- a/src/lib/persisted_state.js +++ b/src/lib/persisted_state.js @@ -1,7 +1,7 @@ import merge from 'lodash.merge' import objectPath from 'object-path' +import localforage from 'localforage' import { throttle } from 'lodash' -import lzstring from 'lz-string' const defaultReducer = (state, paths) => ( paths.length === 0 ? state : paths.reduce((substate, path) => { @@ -11,32 +11,11 @@ const defaultReducer = (state, paths) => ( ) const defaultStorage = (() => { - const hasLocalStorage = typeof window !== 'undefined' && window.localStorage - if (hasLocalStorage) { - return window.localStorage - } - - class InternalStorage { - setItem (key, item) { - this[key] = item - return item - } - getItem (key) { - return this[key] - } - removeItem (key) { - delete this[key] - } - clear () { - Object.keys(this).forEach(key => delete this[key]) - } - } - - return new InternalStorage() + return localforage })() const defaultSetState = (key, state, storage) => { - return storage.setItem(key, lzstring.compressToUTF16(JSON.stringify(state))) + return storage.setItem(key, state) } export default function createPersistedState ({ @@ -44,12 +23,7 @@ export default function createPersistedState ({ paths = [], getState = (key, storage) => { let value = storage.getItem(key) - try { - value = lzstring.decompressFromUTF16(value) // inflate(value, { to: 'string' }) - } catch (e) { - console.log("Couldn't inflate value... Maybe upgrading") - } - return value && value !== 'undefined' ? JSON.parse(value) : undefined + return value }, setState = throttle(defaultSetState, 60000), reducer = defaultReducer, @@ -57,12 +31,13 @@ export default function createPersistedState ({ subscriber = store => handler => store.subscribe(handler) } = {}) { return store => { - const savedState = getState(key, storage) - if (typeof savedState === 'object') { - store.replaceState( - merge({}, store.state, savedState) - ) - } + getState(key, storage).then((savedState) => { + if (typeof savedState === 'object') { + store.replaceState( + merge({}, store.state, savedState) + ) + } + }) subscriber(store)((mutation, state) => { try { -- cgit v1.2.3-70-g09d2