🐛 Merge entry edits after pending sync data has landed
update() read the record before save() waited for the sync to apply server state, and the record finally written was that stale read plus the patch with a fresh client_updated_at: an edit silently reverted what another device had changed in between, and the server took it as the newer version. update() and remove() now patch the record after the wait, so every field they do not touch keeps the value the pull just applied. The test holds an update while a round applies a remote change to the same record and fails on the old code with the reverted field.
This commit is contained in:
@@ -104,7 +104,7 @@ function toServerData(name: CollectionName, item: AnyDoc): Record<string, unknow
|
||||
return data;
|
||||
}
|
||||
|
||||
function pick<K extends CollectionName>(name: K, data: PayloadMap[K]): Record<string, unknown> {
|
||||
function pick<K extends CollectionName>(name: K, data: Partial<PayloadMap[K]>): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [f, v] of Object.entries(data)) {
|
||||
if (DATA_FIELDS[name].includes(f)) out[f] = v;
|
||||
@@ -538,14 +538,26 @@ export async function save<K extends CollectionName>(name: K, id: string | null,
|
||||
return rid;
|
||||
}
|
||||
|
||||
/** Applies a patch once the sync applied the server state it was waiting for, so the fields the patch leaves out keep a newer remote value. */
|
||||
async function merge<K extends CollectionName>(name: K, id: string, patch: Partial<PayloadMap[K]>, deleted?: boolean): Promise<void> {
|
||||
if (!cols) throw new Error('database is not open');
|
||||
await waitForApply(name);
|
||||
const c = cols?.[name];
|
||||
if (!c) throw new Error('database is not open');
|
||||
const prev = readOnce(c, { id }, (cursor) => cursor.fetch()[0]);
|
||||
if (!prev) return;
|
||||
await c.replaceOne({ id }, {
|
||||
...prev,
|
||||
...pick(name, patch),
|
||||
client_updated_at: Math.max(Date.now(), prev.client_updated_at + 1),
|
||||
deleted: deleted ?? prev.deleted
|
||||
});
|
||||
}
|
||||
|
||||
export async function update<K extends CollectionName>(name: K, id: string, patch: Partial<PayloadMap[K]>): Promise<void> {
|
||||
const cur = get(name, id);
|
||||
if (!cur) return;
|
||||
await save(name, id, { ...cur, ...patch } as PayloadMap[K], cur.deleted);
|
||||
await merge(name, id, patch);
|
||||
}
|
||||
|
||||
export async function remove<K extends CollectionName>(name: K, id: string): Promise<void> {
|
||||
const cur = get(name, id);
|
||||
if (!cur) return;
|
||||
await save(name, id, cur, true);
|
||||
await merge(name, id, {}, true);
|
||||
}
|
||||
|
||||
@@ -364,3 +364,31 @@ describe('session expiry', () => {
|
||||
expect(mocks.pushes).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('merge after apply', () => {
|
||||
it('an update waiting for the sync keeps the remote fields the patch does not touch', async () => {
|
||||
status.online = true;
|
||||
const id = await save('entries', null, { start_time: 1000, end_time: 2000, auto_stopped: false, note: 'mine', day_key: '2026-01-01' });
|
||||
await syncNow();
|
||||
const server = mocks.server.get('entries');
|
||||
if (!server?.get(id)) throw new Error('server record missing');
|
||||
server.set(id, { ...server.get(id)!, start_time: 3000, updated: mocks.stamp() });
|
||||
const snapshots = Collection.getCollections().find((c) => c.name === 'wotra-snapshots');
|
||||
if (!snapshots) throw new Error('snapshots collection missing');
|
||||
let waiting: Promise<void> | undefined;
|
||||
const onFind = (selector: unknown) => {
|
||||
if ((selector as { collectionName?: unknown } | null)?.collectionName !== 'entries' || waiting) return;
|
||||
waiting = update('entries', id, { note: 'edited here' });
|
||||
};
|
||||
snapshots.on('find', onFind);
|
||||
try {
|
||||
await syncNow();
|
||||
} finally {
|
||||
snapshots.off('find', onFind);
|
||||
}
|
||||
expect(waiting).toBeDefined();
|
||||
await waiting;
|
||||
expect(get('entries', id)).toMatchObject({ start_time: 3000, end_time: 2000, note: 'edited here', auto_stopped: false });
|
||||
expect(await update('entries', 'missing', { note: 'ignored' })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user