Compress saved state with zlib.

This commit is contained in:
Roger Braun 2017-02-20 18:54:09 +01:00
parent de986b81d5
commit 6e388c0126
3 changed files with 13 additions and 2 deletions

View File

@ -19,6 +19,7 @@
"karma-mocha-reporter": "^2.2.1",
"node-sass": "^3.10.1",
"object-path": "^0.11.3",
"pako": "^1.0.4",
"sanitize-html": "^1.13.0",
"sass-loader": "^4.0.2",
"tributejs": "^2.1.0",

View File

@ -1,6 +1,7 @@
import merge from 'lodash.merge'
import objectPath from 'object-path'
import { throttle } from 'lodash'
import { inflate, deflate } from 'pako'
const defaultReducer = (state, paths) => (
paths.length === 0 ? state : paths.reduce((substate, path) => {
@ -35,14 +36,19 @@ const defaultStorage = (() => {
})()
const defaultSetState = (key, state, storage) => {
return storage.setItem(key, JSON.stringify(state))
return storage.setItem(key, deflate(JSON.stringify(state), { to: 'string' }))
}
export default function createPersistedState ({
key = 'vuex',
paths = [],
getState = (key, storage) => {
const value = storage.getItem(key)
let value = storage.getItem(key)
try {
value = inflate(value, { to: 'string' })
} catch (e) {
console.log("Couldn't inflate value... Maybe upgrading")
}
return value && value !== 'undefined' ? JSON.parse(value) : undefined
},
setState = throttle(defaultSetState, 5000),

View File

@ -4041,6 +4041,10 @@ pac-resolver@~1.2.1:
regenerator "~0.8.13"
thunkify "~2.1.1"
pako@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.4.tgz#412cc97c3b7ff06dc6c2557fd4f03d06f5e708d4"
pako@~0.2.0:
version "0.2.9"
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"