Skip to content
Snippets Groups Projects
Commit 15124319 authored by HJ's avatar HJ :fire:
Browse files

fix leaky journal by running uniq on addToCollection entries

parent da94c94f
No related branches found
No related tags found
2 merge requests!1711Update stable - 2.5.0 release,!1687fix leaky journal by running uniq on addToCollection entries
Pipeline #42672 passed
import { toRaw } from 'vue'
import { isEqual, cloneDeep, set, get, clamp, flatten, groupBy, findLastIndex, takeRight } from 'lodash'
import { isEqual, cloneDeep, set, get, clamp, flatten, groupBy, findLastIndex, takeRight, uniqWith } from 'lodash'
import { CURRENT_UPDATE_COUNTER } from 'src/components/update_notification/update_notification.js'
export const VERSION = 1
......@@ -149,12 +149,21 @@ const _mergeJournal = (...journals) => {
if (path.startsWith('collections')) {
const lastRemoveIndex = findLastIndex(journal, ({ operation }) => operation === 'removeFromCollection')
// everything before last remove is unimportant
let remainder
if (lastRemoveIndex > 0) {
return journal.slice(lastRemoveIndex)
remainder = journal.slice(lastRemoveIndex)
} else {
// everything else doesn't need trimming
return journal
remainder = journal
}
return uniqWith(remainder, (a, b) => {
if (a.path !== b.path) { return false }
if (a.operation !== b.operation) { return false }
if (a.operation === 'addToCollection') {
return a.args[0] === b.args[0]
}
return false
})
} else if (path.startsWith('simple')) {
// Only the last record is important
return takeRight(journal)
......
......@@ -148,6 +148,18 @@ describe('The serverSideStorage module', () => {
timestamp: state.prefsStorage._journal[1].timestamp
})
})
it('should remove duplicate entries from journal', () => {
const state = cloneDeep(defaultState)
setPreference(state, { path: 'simple.testing', value: 1 })
setPreference(state, { path: 'simple.testing', value: 1 })
addCollectionPreference(state, { path: 'collections.testing', value: 2 })
addCollectionPreference(state, { path: 'collections.testing', value: 2 })
updateCache(state, { username: 'test' })
expect(state.prefsStorage.simple.testing).to.eql(1)
expect(state.prefsStorage.collections.testing).to.eql([2])
expect(state.prefsStorage._journal.length).to.eql(2)
})
})
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment