From 40b607bdeae35d7da533aae0b1371770615c2eb6 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 9 Apr 2017 22:15:35 +0200 Subject: [PATCH] Set state to loaded if it crashes while loading. --- src/lib/persisted_state.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js index 26a9d0e4..02349e13 100644 --- a/src/lib/persisted_state.js +++ b/src/lib/persisted_state.js @@ -38,19 +38,24 @@ export default function createPersistedState ({ } = {}) { return store => { getState(key, storage).then((savedState) => { - if (typeof savedState === 'object') { - // build user cache - const usersState = savedState.users || {} - usersState.usersObject = {} - const users = usersState.users || [] - each(users, (user) => { usersState.usersObject[user.id] = user }) - savedState.users = usersState + try { + if (typeof savedState === 'object') { + // build user cache + const usersState = savedState.users || {} + usersState.usersObject = {} + const users = usersState.users || [] + each(users, (user) => { usersState.usersObject[user.id] = user }) + savedState.users = usersState - store.replaceState( - merge({}, store.state, savedState) - ) + store.replaceState( + merge({}, store.state, savedState) + ) + } + loaded = true + } catch (e) { + console.log("Couldn't load state") + loaded = true } - loaded = true }) subscriber(store)((mutation, state) => {