fixed objectCollection not leaving a journal entry on removal

This commit is contained in:
Henry Jameson 2026-01-28 21:31:41 +02:00
commit 6877008017
2 changed files with 36 additions and 10 deletions

View file

@ -139,7 +139,7 @@ export const _getRecentData = (cache, live, isTest) => {
result.stale = live result.stale = live
} else { } else {
console.debug( console.debug(
'Different timestamp, figuring out which one is more recent', 'Different timestamp or version, figuring out which one is more recent',
) )
if (live._timestamp < cache._timestamp) { if (live._timestamp < cache._timestamp) {
result.recent = cache result.recent = cache
@ -522,6 +522,7 @@ export const useSyncConfigStore = defineStore('sync_config', {
const collection = new Set(get(this.prefsStorage, path)) const collection = new Set(get(this.prefsStorage, path))
collection.delete(value) collection.delete(value)
set(this.prefsStorage, path, [...collection]) set(this.prefsStorage, path, [...collection])
this.prefsStorage._journal = [ this.prefsStorage._journal = [
...this.prefsStorage._journal, ...this.prefsStorage._journal,
{ {
@ -535,8 +536,19 @@ export const useSyncConfigStore = defineStore('sync_config', {
} else if (path.startsWith('objectCollection')) { } else if (path.startsWith('objectCollection')) {
const collection = new Set(get(this.prefsStorage, path + '.index')) const collection = new Set(get(this.prefsStorage, path + '.index'))
collection.delete(_key) collection.delete(_key)
set(this.prefsStorage, path + '.index', [...collection])
const data = get(this.prefsStorage, path + '.data') const data = get(this.prefsStorage, path + '.data')
delete data[_key] delete data[_key]
this.prefsStorage._journal = [
...this.prefsStorage._journal,
{
operation: 'removeFromCollection',
path,
args: [{ _key }],
timestamp: Date.now(),
},
]
} }
}, },
reorderCollectionPreference({ path, value, movement }) { reorderCollectionPreference({ path, value, movement }) {
@ -579,14 +591,13 @@ export const useSyncConfigStore = defineStore('sync_config', {
const live = userData.storage const live = userData.storage
this.raw = live this.raw = live
let cache = this.cache let cache = this.cache
if (cache && cache._user !== userData.fqn) { if (cache?._user !== userData.fqn) {
console.warn( console.warn(
'Cache belongs to another user! reinitializing local cache!', 'Cache belongs to another user! reinitializing local cache!',
) )
cache = null cache = null
} }
console.log(cache, live)
cache = _doMigrations(cache, live)
let { recent, stale, needUpload } = _getRecentData(cache, live) let { recent, stale, needUpload } = _getRecentData(cache, live)
@ -604,6 +615,9 @@ export const useSyncConfigStore = defineStore('sync_config', {
}) })
} }
recent = recent && _doMigrations(recent)
stale = stale && _doMigrations(stale)
if (!needUpload && recent && stale) { if (!needUpload && recent && stale) {
console.debug('Checking if data needs merging...') console.debug('Checking if data needs merging...')
// discarding timestamps and versions // discarding timestamps and versions

View file

@ -16,7 +16,7 @@ import {
VERSION, VERSION,
} from 'src/stores/sync_config.js' } from 'src/stores/sync_config.js'
describe.skip('The SyncConfig module', () => { describe('The SyncConfig module', () => {
beforeEach(() => { beforeEach(() => {
setActivePinia(createPinia()) setActivePinia(createPinia())
}) })
@ -30,7 +30,7 @@ describe.skip('The SyncConfig module', () => {
it('should initialize storage if none present', () => { it('should initialize storage if none present', () => {
const store = useSyncConfigStore() const store = useSyncConfigStore()
store.setSyncConfig(store, user) store.setSyncConfig({ ...user })
expect(store.cache._version).to.eql(VERSION) expect(store.cache._version).to.eql(VERSION)
expect(store.cache._timestamp).to.be.a('number') expect(store.cache._timestamp).to.be.a('number')
expect(store.cache.flagStorage).to.eql(defaultState.flagStorage) expect(store.cache.flagStorage).to.eql(defaultState.flagStorage)
@ -95,7 +95,7 @@ describe.skip('The SyncConfig module', () => {
expect(store.cache.flagStorage.updateCounter).to.eql(999) expect(store.cache.flagStorage.updateCounter).to.eql(999)
}) })
it('should remote version if local missing', () => { it('should use remote version if local missing', () => {
const store = useSyncConfigStore() const store = useSyncConfigStore()
store.setSyncConfig(store, user) store.setSyncConfig(store, user)
expect(store.cache._version).to.eql(VERSION) expect(store.cache._version).to.eql(VERSION)
@ -127,14 +127,26 @@ describe.skip('The SyncConfig module', () => {
path: 'objectCollections.testing', path: 'objectCollections.testing',
value: { _key: 'a', foo: 1 }, value: { _key: 'a', foo: 1 },
}) })
expect(store.prefsStorage.objectCollections.testing).to.eql({
data: { a: { _key: 'a', foo: 1 } },
index: ['a'],
})
store.removeCollectionPreference({ store.removeCollectionPreference({
path: 'collections.testing', path: 'collections.testing',
value: 2, value: 2,
}) })
store.removeCollectionPreference({
path: 'objectCollections.testing',
value: { _key: 'a' },
})
store.updateCache({ username: 'test' }) store.updateCache({ username: 'test' })
expect(store.prefsStorage.simple.testing).to.eql(2) expect(store.prefsStorage.simple.testing).to.eql(2)
expect(store.prefsStorage.collections.testing).to.eql([]) expect(store.prefsStorage.collections.testing).to.eql([])
expect(store.prefsStorage._journal.length).to.eql(2) expect(store.prefsStorage.objectCollections.testing).to.eql({
data: {},
index: [],
})
expect(store.prefsStorage._journal.length).to.eql(3)
expect(store.prefsStorage._journal[0]).to.eql({ expect(store.prefsStorage._journal[0]).to.eql({
path: 'simple.testing', path: 'simple.testing',
operation: 'set', operation: 'set',
@ -152,7 +164,7 @@ describe.skip('The SyncConfig module', () => {
expect(store.prefsStorage._journal[2]).to.eql({ expect(store.prefsStorage._journal[2]).to.eql({
path: 'objectCollections.testing', path: 'objectCollections.testing',
operation: 'removeFromCollection', operation: 'removeFromCollection',
args: [{ _key: 'a', foo: 1 }], args: [{ _key: 'a' }],
// should have A timestamp, we don't really care what it is // should have A timestamp, we don't really care what it is
timestamp: store.prefsStorage._journal[2].timestamp, timestamp: store.prefsStorage._journal[2].timestamp,
}) })
@ -179,7 +191,7 @@ describe.skip('The SyncConfig module', () => {
data: { a: { _key: 'a', foo: 1 } }, data: { a: { _key: 'a', foo: 1 } },
index: ['a'], index: ['a'],
}) })
expect(store.prefsStorage._journal.length).to.eql(3) expect(store.prefsStorage._journal.length).to.eql(4)
}) })
it('should remove depth = 3 set/unset entries from journal', () => { it('should remove depth = 3 set/unset entries from journal', () => {