diff options
| author | Henry Jameson <me@hjkos.com> | 2022-08-16 19:24:20 +0300 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2022-08-16 19:24:45 +0300 |
| commit | 840ce063971d68063d380fc5f3aacc0564363b50 (patch) | |
| tree | c3cf81569689285657e76192af2f010b1e341d62 /src/modules/serverSideStorage.js | |
| parent | 8d6e5c1e69b32bc6c13558dc4346407662849486 (diff) | |
proper journal trimming + remove some old workaround to my local bad data
Diffstat (limited to 'src/modules/serverSideStorage.js')
| -rw-r--r-- | src/modules/serverSideStorage.js | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/src/modules/serverSideStorage.js b/src/modules/serverSideStorage.js index eb089be6..08b11b0b 100644 --- a/src/modules/serverSideStorage.js +++ b/src/modules/serverSideStorage.js @@ -1,5 +1,5 @@ import { toRaw } from 'vue' -import { isEqual, uniqWith, cloneDeep, set, get, clamp } from 'lodash' +import { isEqual, cloneDeep, set, get, clamp, flatten, groupBy, findLastIndex, takeRight } from 'lodash' import { CURRENT_UPDATE_COUNTER } from 'src/components/update_notification/update_notification.js' export const VERSION = 1 @@ -131,25 +131,32 @@ export const _mergeFlags = (recent, stale, allFlagKeys) => { })) } -const _mergeJournal = (a, b) => uniqWith( - // TODO use groupBy to group by path, then trim them depending on operations, - // i.e. if field got removed in the end - no need to sort it beforehand, if field - // got re-added no need to remove it and add it etc. - [ - ...(Array.isArray(a) ? a : []), - ...(Array.isArray(b) ? b : []) - ].sort((a, b) => a.timestamp > b.timestamp ? -1 : 1), - (a, b) => { - if (a.operation !== b.operation) return false - switch (a.operation) { - case 'set': - case 'arrangeSet': - return a.path === b.path - default: - return a.path === b.path && a.timestamp === b.timestamp +const _mergeJournal = (...journals) => { + const allJournals = flatten(journals.map(j => Array.isArray(j) ? j : [])) + const grouped = groupBy(allJournals, 'path') + const trimmedGrouped = Object.entries(grouped).map(([path, journal]) => { + // side effect + journal.sort((a, b) => a.timestamp > b.timestamp ? 1 : -1) + + if (path.startsWith('collections')) { + const lastRemoveIndex = findLastIndex(journal, ({ operation }) => operation === 'removeFromCollection') + // everything before last remove is unimportant + if (lastRemoveIndex > 0) { + return journal.slice(lastRemoveIndex) + } else { + // everything else doesn't need trimming + return journal + } + } else if (path.startsWith('simple')) { + // Only the last record is important + return takeRight(journal) + } else { + return journal } - } -).reverse() + }) + return flatten(trimmedGrouped) + .sort((a, b) => a.timestamp > b.timestamp ? 1 : -1) +} export const _mergePrefs = (recent, stale, allFlagKeys) => { if (!stale) return recent @@ -169,7 +176,6 @@ export const _mergePrefs = (recent, stale, allFlagKeys) => { const resultOutput = { ...recentData } const totalJournal = _mergeJournal(staleJournal, recentJournal) totalJournal.forEach(({ path, timestamp, operation, command, args }) => { - operation = operation || command if (path.startsWith('_')) { console.error(`journal contains entry to edit internal (starts with _) field '${path}', something is incorrect here, ignoring.`) return |
