diff --git a/src/stores/sync_config.js b/src/stores/sync_config.js index 674f84f9a..244489074 100644 --- a/src/stores/sync_config.js +++ b/src/stores/sync_config.js @@ -413,7 +413,7 @@ export const _resetPrefs = ( return totalPrefs } -export const _doMigrations = async (data) => { +export const _doMigrations = async (data, setPreference) => { console.log('TEST', data._version) if (data._version < VERSION) { @@ -425,7 +425,7 @@ export const _doMigrations = async (data) => { // Migrate old config to sync config const vuexState = await storage.getItem('vuex-lz') defaultStateKeys.forEach(key => { - this.setPreference({ path: `simple.${key}`, value: vuexState.config[key] }) + setPreference({ path: `simple.${key}`, value: vuexState.config[key] }) }) } } @@ -636,8 +636,8 @@ export const useSyncConfigStore = defineStore('sync_config', { }) } - recent = recent && await _doMigrations(recent) - stale = stale && await _doMigrations(stale) + recent = recent && await _doMigrations(recent, this.setPreference) + stale = stale && await _doMigrations(stale, this.setPreference) if (!needUpload && recent && stale) { console.debug('Checking if data needs merging...') diff --git a/src/stores/user_highlight.js b/src/stores/user_highlight.js index 78bb3c4dc..14431b8b4 100644 --- a/src/stores/user_highlight.js +++ b/src/stores/user_highlight.js @@ -51,7 +51,7 @@ const _wrapData = (data, userName) => { const _checkValidity = (data) => data._timestamp > 0 -const _verifyPrefs = (state) => { +const _verifyHighlights = (state) => { state.highlight = state.highlight || {} // Simple @@ -148,7 +148,7 @@ const _mergeJournal = (...journals) => { ) } -export const _mergePrefs = (recent, stale) => { +export const _mergeHighlights = (recent, stale) => { if (!stale) return recent if (!recent) return stale const { _journal: recentJournal, ...recentData } = recent @@ -163,8 +163,8 @@ export const _mergePrefs = (recent, stale) => { * to requested one. Intended only to be used with simple preferences (boolean, number) */ const resultOutput = { ...recentData } - const totalJournal = _mergeJournal(staleJournal, recentJournal) - totalJournal.forEach(({ user, operation, args }) => { + const Journal = _mergeJournal(staleJournal, recentJournal) + Journal.forEach(({ user, operation, args }) => { if (user.startsWith('_')) { throw new Error( `journal contains entry to edit internal (starts with _) field '${user}', something is incorrect here, ignoring.`, @@ -172,16 +172,16 @@ export const _mergePrefs = (recent, stale) => { } switch (operation) { case 'set': - resultOutput.highlight[user] = args[0] + resultOutput[user] = args[0] break case 'unset': - delete resultOutput.highlight[user] + delete resultOutput[user] break default: return console.error(`Unknown journal operation: '${operation}'`) } }) - return { ...resultOutput, _journal: totalJournal } + return { ...resultOutput, _journal: Journal } } export const useUserHighlightStore = defineStore('user_highlight', { @@ -272,20 +272,21 @@ export const useUserHighlightStore = defineStore('user_highlight', { let { recent, stale, needUpload } = _getRecentData(cache, live) + const userNew = userData.created_at > NEW_USER_DATE + let dirty = false + const vuexState = await storage.getItem('vuex-lz') const { highlight } = vuexState.config Object.entries(highlight).forEach(([user, value]) => { - if ((highlight[user]._migrated || 0) < 2) { + if ((highlight[user]._migrated || 0) < 1) { + dirty = true this.set({ user, value: clone(value) }) - // vuexState.config.highlight[user]._migrated = 1 + vuexState.config.highlight[user]._migrated = 1 console.log(user, value, this.highlight[user]) } }) storage.setItem('vuex-lz', vuexState) - const userNew = userData.created_at > NEW_USER_DATE - let dirty = false - if (recent === null) { console.debug( `Data is empty, initializing for ${userNew ? 'new' : 'existing'} user`, @@ -304,17 +305,17 @@ export const useUserHighlightStore = defineStore('user_highlight', { console.debug(`Data ${dirty ? 'needs' : "doesn't need"} merging`) } - let totalPrefs + let highlights if (dirty) { console.debug('Merging the data...') - _verifyPrefs(recent) - _verifyPrefs(stale) - totalPrefs = _mergePrefs(recent.highlight, stale.highlight) + _verifyHighlights(recent) + _verifyHighlights(stale) + highlights = _mergeHighlights(recent.highlight, stale.highlight) } else { - totalPrefs = recent.highlight + highlights = recent.highlight } - recent.highlight = { ...defaultState.highlight, ...totalPrefs } + recent.highlight = { ...defaultState.highlight, ...highlights } this.dirty = dirty || needUpload this.cache = recent