From 5143ae7f72c7535872188d9887dc61628add09fb Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Mar 2017 08:43:48 +0100 Subject: [PATCH] Switch to localforage to use indexeddb by default. --- package.json | 2 +- src/lib/persisted_state.js | 47 ++++------------ yarn.lock | 112 +++++++++++++++++++++++++++++++++---- 3 files changed, 114 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index e7aa7241..74706389 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "babel-plugin-lodash": "^3.2.11", "diff": "^3.0.1", "karma-mocha-reporter": "^2.2.1", - "lz-string": "^1.4.4", + "localforage": "^1.5.0", "node-sass": "^3.10.1", "object-path": "^0.11.3", "sanitize-html": "^1.13.0", 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 { diff --git a/yarn.lock b/yarn.lock index 92f734bc..677c9690 100644 --- a/yarn.lock +++ b/yarn.lock @@ -23,6 +23,10 @@ acorn@4.0.4: version "4.0.4" resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.4.tgz#17a8d6a7a6c4ef538b814ec9abac2779293bf30a" +acorn@^1.0.3: + version "1.2.2" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-1.2.2.tgz#c8ce27de0acc76d896d2b1fad3df588d9e82f014" + acorn@^3.0.0, acorn@^3.0.4, acorn@^3.1.0: version "3.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" @@ -193,11 +197,11 @@ ast-traverse@~0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/ast-traverse/-/ast-traverse-0.1.1.tgz#69cf2b8386f19dcda1bb1e05d68fe359d8897de6" -ast-types@0.8.12: +ast-types@0.8.12, ast-types@0.x.x: version "0.8.12" resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.8.12.tgz#a0d90e4351bb887716c83fd637ebf818af4adfcc" -ast-types@0.9.5, ast-types@0.x.x: +ast-types@0.9.5: version "0.9.5" resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.9.5.tgz#1a660a09945dbceb1f9c9cbb715002617424e04a" @@ -836,6 +840,10 @@ balanced-match@^0.4.1, balanced-match@^0.4.2: version "0.4.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838" +base62@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/base62/-/base62-0.1.1.tgz#7b4174c2f94449753b11c2651c083da841a7b084" + base64-arraybuffer@0.1.5: version "0.1.5" resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8" @@ -1815,6 +1823,14 @@ error-ex@^1.2.0: dependencies: is-arrayish "^0.2.1" +es3ify@^0.1.3: + version "0.1.4" + resolved "https://registry.yarnpkg.com/es3ify/-/es3ify-0.1.4.tgz#ad9fa5df1ae34f3f31e1211b5818b2d51078dfd1" + dependencies: + esprima-fb "~3001.0001.0000-dev-harmony-fb" + jstransform "~3.0.0" + through "~2.3.4" + es5-ext@^0.10.7, es5-ext@^0.10.8, es5-ext@~0.10.11, es5-ext@~0.10.2, es5-ext@~0.10.7: version "0.10.12" resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.12.tgz#aa84641d4db76b62abba5e45fd805ecbab140047" @@ -1978,6 +1994,10 @@ eslint@^3.7.1: text-table "~0.2.0" user-home "^2.0.0" +esmangle-evaluator@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/esmangle-evaluator/-/esmangle-evaluator-1.0.1.tgz#620d866ef4861b3311f75766d52a8572bb3c6336" + espree@^3.4.0: version "3.4.0" resolved "https://registry.yarnpkg.com/espree/-/espree-3.4.0.tgz#41656fa5628e042878025ef467e78f125cb86e1d" @@ -1989,6 +2009,10 @@ esprima-fb@~15001.1001.0-dev-harmony-fb: version "15001.1001.0-dev-harmony-fb" resolved "https://registry.yarnpkg.com/esprima-fb/-/esprima-fb-15001.1001.0-dev-harmony-fb.tgz#43beb57ec26e8cf237d3dd8b33e42533577f2659" +esprima-fb@~3001.0001.0000-dev-harmony-fb, esprima-fb@~3001.1.0-dev-harmony-fb: + version "3001.1.0-dev-harmony-fb" + resolved "https://registry.yarnpkg.com/esprima-fb/-/esprima-fb-3001.0001.0000-dev-harmony-fb.tgz#b77d37abcd38ea0b77426bb8bc2922ce6b426411" + esprima@2.7.x, esprima@^2.1.0, esprima@^2.6.0, esprima@^2.7.1: version "2.7.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" @@ -2136,6 +2160,15 @@ extsprintf@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550" +falafel@^1.0.1: + version "1.2.0" + resolved "https://registry.yarnpkg.com/falafel/-/falafel-1.2.0.tgz#c18d24ef5091174a497f318cd24b026a25cddab4" + dependencies: + acorn "^1.0.3" + foreach "^2.0.5" + isarray "0.0.1" + object-keys "^1.0.6" + fast-levenshtein@~2.0.4: version "2.0.6" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" @@ -2248,6 +2281,10 @@ for-own@^0.1.4: dependencies: for-in "^0.1.5" +foreach@^2.0.5: + version "2.0.5" + resolved "https://registry.yarnpkg.com/foreach/-/foreach-2.0.5.tgz#0bee005018aeb260d0a3af3ae658dd0136ec1b99" + forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" @@ -2683,6 +2720,10 @@ ignore@^3.2.0: version "3.2.2" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.2.2.tgz#1c51e1ef53bab6ddc15db4d9ac4ec139eceb3410" +immediate@~3.0.5: + version "3.0.6" + resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" + imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" @@ -2730,6 +2771,13 @@ inject-loader@^2.0.1: dependencies: loader-utils "^0.2.3" +inline-process-browser@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/inline-process-browser/-/inline-process-browser-1.0.0.tgz#46a61b153dd3c9b1624b1a00626edb4f7f414f22" + dependencies: + falafel "^1.0.1" + through2 "^0.6.5" + inquirer@^0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-0.12.0.tgz#1ef2bfd63504df0bc75785fff8c2c41df12f077e" @@ -3092,6 +3140,14 @@ jsprim@^1.2.2: json-schema "0.2.3" verror "1.3.6" +jstransform@~3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/jstransform/-/jstransform-3.0.0.tgz#a2591ab6cee8d97bf3be830dbfa2313b87cd640b" + dependencies: + base62 "0.1.1" + esprima-fb "~3001.1.0-dev-harmony-fb" + source-map "0.1.31" + karma-coverage@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/karma-coverage/-/karma-coverage-1.1.1.tgz#5aff8b39cf6994dc22de4c84362c76001b637cf6" @@ -3214,6 +3270,15 @@ levn@^0.3.0, levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" +lie@3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/lie/-/lie-3.0.2.tgz#ffda21d7bba26f377cad865d3649b2fc8ce39fea" + dependencies: + es3ify "^0.1.3" + immediate "~3.0.5" + inline-process-browser "^1.0.0" + unreachable-branch-transform "^0.3.0" + load-json-file@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0" @@ -3233,6 +3298,12 @@ loader-utils@0.2.x, loader-utils@^0.2.10, loader-utils@^0.2.11, loader-utils@^0. json5 "^0.5.0" object-assign "^4.0.1" +localforage@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.5.0.tgz#6b994e19b56611fa85df3992df397ac4ab66e815" + dependencies: + lie "3.0.2" + lodash._arraycopy@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/lodash._arraycopy/-/lodash._arraycopy-3.0.0.tgz#76e7b7c1f1fb92547374878a562ed06a3e50f6e1" @@ -3549,10 +3620,6 @@ lru-cache@~2.6.5: version "2.6.5" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.6.5.tgz#e56d6354148ede8d7707b58d143220fd08df0fd5" -lz-string@^1.4.4: - version "1.4.4" - resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26" - macaddress@^0.2.8: version "0.2.8" resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12" @@ -3936,6 +4003,10 @@ object-hash@^1.1.4: version "1.1.5" resolved "https://registry.yarnpkg.com/object-hash/-/object-hash-1.1.5.tgz#bdd844e030d0861b692ca175c6cab6868ec233d7" +object-keys@^1.0.6: + version "1.0.11" + resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d" + object-path@^0.11.3: version "0.11.3" resolved "https://registry.yarnpkg.com/object-path/-/object-path-0.11.3.tgz#3e21a42ad07234d815429ae9e15c1c5f38050554" @@ -4597,7 +4668,7 @@ read-pkg@^1.0.0: normalize-package-data "^2.3.2" path-type "^1.0.0" -readable-stream@1.0, readable-stream@~1.0.2: +readable-stream@1.0, "readable-stream@>=1.0.33-1 <1.1.0-0", readable-stream@~1.0.2: version "1.0.34" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" dependencies: @@ -4667,7 +4738,7 @@ readline2@^1.0.1: is-fullwidth-code-point "^1.0.0" mute-stream "0.0.5" -recast@0.10.33: +recast@0.10.33, recast@^0.10.1: version "0.10.33" resolved "https://registry.yarnpkg.com/recast/-/recast-0.10.33.tgz#942808f7aa016f1fa7142c461d7e5704aaa8d697" dependencies: @@ -5143,6 +5214,12 @@ source-map-support@^0.4.2: dependencies: source-map "^0.5.3" +source-map@0.1.31: + version "0.1.31" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.1.31.tgz#9f704d0d69d9e138a81badf6ebb4fde33d151c61" + dependencies: + amdefine ">=0.0.4" + source-map@0.5.x, source-map@^0.5.0, source-map@^0.5.1, source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.0, source-map@~0.5.1, source-map@~0.5.3: version "0.5.6" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412" @@ -5374,7 +5451,14 @@ throttleit@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/throttleit/-/throttleit-1.0.0.tgz#9e785836daf46743145a5984b6268d828528ac6c" -through@^2.3.6, through@~2.3.8: +through2@^0.6.2, through2@^0.6.5: + version "0.6.5" + resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48" + dependencies: + readable-stream ">=1.0.33-1 <1.1.0-0" + xtend ">=4.0.0 <4.1.0-0" + +through@^2.3.6, through@~2.3.4, through@~2.3.8: version "2.3.8" resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" @@ -5516,6 +5600,14 @@ unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" +unreachable-branch-transform@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/unreachable-branch-transform/-/unreachable-branch-transform-0.3.0.tgz#d99cc4c6e746d264928845b611db54b0f3474caa" + dependencies: + esmangle-evaluator "^1.0.0" + recast "^0.10.1" + through2 "^0.6.2" + upper-case@^1.1.1: version "1.1.3" resolved "https://registry.yarnpkg.com/upper-case/-/upper-case-1.1.3.tgz#f6b4501c2ec4cdd26ba78be7222961de77621598" @@ -5812,7 +5904,7 @@ xregexp@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-2.0.0.tgz#52a63e56ca0b84a7f3a5f3d61872f126ad7a5943" -xtend@^4.0.0: +"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"