From 209e8614b09ab7f24262d86de0924bd2e0dd96ec Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 18 Feb 2017 20:56:03 +0100 Subject: Don't add notifications twice + persiste them. --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.js') diff --git a/src/main.js b/src/main.js index 4b367db9..841e61a7 100644 --- a/src/main.js +++ b/src/main.js @@ -29,7 +29,7 @@ Vue.use(VueTimeago, { }) const persistedStateOptions = { - paths: ['users.users'] + paths: ['users.users', 'statuses.notifications'] } const store = new Vuex.Store({ -- cgit v1.2.3-70-g09d2 From 9cf438f1b2d35b9282861128f2267d54d97d7588 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 19 Feb 2017 14:25:31 +0100 Subject: Remove state persistence for now. Makes chrome really slow and adds problems with tab synchronization. --- src/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main.js') diff --git a/src/main.js b/src/main.js index 841e61a7..fd03e519 100644 --- a/src/main.js +++ b/src/main.js @@ -17,7 +17,7 @@ import configModule from './modules/config.js' import VueTimeago from 'vue-timeago' -import createPersistedState from 'vuex-persistedstate' +// import createPersistedState from 'vuex-persistedstate' Vue.use(Vuex) Vue.use(VueRouter) @@ -28,9 +28,9 @@ Vue.use(VueTimeago, { } }) -const persistedStateOptions = { - paths: ['users.users', 'statuses.notifications'] -} +// const persistedStateOptions = { +// paths: ['users.users', 'statuses.notifications'] +// } const store = new Vuex.Store({ modules: { @@ -39,7 +39,7 @@ const store = new Vuex.Store({ api: apiModule, config: configModule }, - plugins: [createPersistedState(persistedStateOptions)], + // plugins: [createPersistedState(persistedStateOptions)], strict: process.env.NODE_ENV !== 'production' }) -- cgit v1.2.3-70-g09d2 From 3fd0d0d1cba11f469d8f00b9bf00bea0dfdf1494 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Feb 2017 17:49:43 +0100 Subject: Revert "Remove state persistence for now." This reverts commit 9cf438f1b2d35b9282861128f2267d54d97d7588. --- src/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main.js') diff --git a/src/main.js b/src/main.js index fd03e519..841e61a7 100644 --- a/src/main.js +++ b/src/main.js @@ -17,7 +17,7 @@ import configModule from './modules/config.js' import VueTimeago from 'vue-timeago' -// import createPersistedState from 'vuex-persistedstate' +import createPersistedState from 'vuex-persistedstate' Vue.use(Vuex) Vue.use(VueRouter) @@ -28,9 +28,9 @@ Vue.use(VueTimeago, { } }) -// const persistedStateOptions = { -// paths: ['users.users', 'statuses.notifications'] -// } +const persistedStateOptions = { + paths: ['users.users', 'statuses.notifications'] +} const store = new Vuex.Store({ modules: { @@ -39,7 +39,7 @@ const store = new Vuex.Store({ api: apiModule, config: configModule }, - // plugins: [createPersistedState(persistedStateOptions)], + plugins: [createPersistedState(persistedStateOptions)], strict: process.env.NODE_ENV !== 'production' }) -- cgit v1.2.3-70-g09d2 From 8429de22e8a45b8cb35f7026c61ebd581b1ef159 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Feb 2017 18:02:50 +0100 Subject: Remove notification persistence for now. Seems to blow things up. --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.js') diff --git a/src/main.js b/src/main.js index 841e61a7..4b367db9 100644 --- a/src/main.js +++ b/src/main.js @@ -29,7 +29,7 @@ Vue.use(VueTimeago, { }) const persistedStateOptions = { - paths: ['users.users', 'statuses.notifications'] + paths: ['users.users'] } const store = new Vuex.Store({ -- cgit v1.2.3-70-g09d2 From ebf9fe6a987842966d32f881902f233a67787d2f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Feb 2017 18:25:19 +0100 Subject: Pull in persistence plugin, don't blow up on full storage. --- src/lib/persisted_state.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.js | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/lib/persisted_state.js (limited to 'src/main.js') diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js new file mode 100644 index 00000000..67ecc1ea --- /dev/null +++ b/src/lib/persisted_state.js @@ -0,0 +1,65 @@ +import merge from 'lodash.merge' +import objectPath from 'object-path' + +const defaultReducer = (state, paths) => ( + paths.length === 0 ? state : paths.reduce((substate, path) => { + objectPath.set(substate, path, objectPath.get(state, path)) + return substate + }, {}) +) + +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() +})() + +export default function createPersistedState ({ + key = 'vuex', + paths = [], + getState = (key, storage) => { + const value = storage.getItem(key) + return value && value !== 'undefined' ? JSON.parse(value) : undefined + }, + setState = (key, state, storage) => storage.setItem(key, JSON.stringify(state)), + reducer = defaultReducer, + storage = defaultStorage, + subscriber = store => handler => store.subscribe(handler) +} = {}) { + return store => { + const savedState = getState(key, storage) + if (typeof savedState === 'object') { + store.replaceState( + merge({}, store.state, savedState) + ) + } + + subscriber(store)((mutation, state) => { + try { + setState(key, reducer(state, paths), storage) + } catch (e) { + console.log("Couldn't persist state:") + console.log(e) + } + }) + } +} diff --git a/src/main.js b/src/main.js index 4b367db9..d982d668 100644 --- a/src/main.js +++ b/src/main.js @@ -17,7 +17,7 @@ import configModule from './modules/config.js' import VueTimeago from 'vue-timeago' -import createPersistedState from 'vuex-persistedstate' +import createPersistedState from './lib/persisted_state.js' Vue.use(Vuex) Vue.use(VueRouter) -- cgit v1.2.3-70-g09d2 From 73afa8e075cd8e97b09526e2393b0aafc9c8e4e5 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Feb 2017 18:54:30 +0100 Subject: Save notifications again. --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.js') diff --git a/src/main.js b/src/main.js index d982d668..97156b64 100644 --- a/src/main.js +++ b/src/main.js @@ -29,7 +29,7 @@ Vue.use(VueTimeago, { }) const persistedStateOptions = { - paths: ['users.users'] + paths: ['users.users', 'statuses.notifications'] } const store = new Vuex.Store({ -- cgit v1.2.3-70-g09d2