🐛 Surface changes rejected by the server
The server now answers an invalid change per item instead of failing the whole batch, and the client dropped such a change silently: the round went through and nothing told the user that the edit never reached the server. A rejected change now sets the sync status to "<collection>: <error>", cut to 200 characters, so the sync button shows its warning state. The change is not retried, as before. The message survives the collections that still succeed in the same round, since each of them used to clear the error. The next round without a rejection clears it.
This commit is contained in:
@@ -32,7 +32,8 @@ type AnyCollection = Collection<AnyDoc, string>;
|
||||
interface PushResult {
|
||||
id: string;
|
||||
collection: string;
|
||||
status: 'ok' | 'stale' | 'forbidden' | 'readonly';
|
||||
status: 'ok' | 'stale' | 'forbidden' | 'readonly' | 'invalid';
|
||||
error?: unknown;
|
||||
record?: Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
@@ -151,6 +152,8 @@ const pendingCursor = new Map<string, string>();
|
||||
const staleBuffer = new Map<string, AnyDoc[]>();
|
||||
/** Change records the last push of a collection sent; the pull after it keeps newer local changes. */
|
||||
const pushedChanges = new Map<string, Set<string>>();
|
||||
/** Set when the server rejected a change of the current round; its message stays until a round succeeds. */
|
||||
let rejected = false;
|
||||
|
||||
/** Collections whose local state the sync is about to touch or is touching, by name and nesting depth. */
|
||||
const applying = new Map<string, number>();
|
||||
@@ -197,7 +200,7 @@ function onSynced(name: string) {
|
||||
pendingCursor.delete(name);
|
||||
}
|
||||
status.lastSync = Date.now();
|
||||
status.error = null;
|
||||
if (!rejected) status.error = null;
|
||||
}
|
||||
|
||||
class WotraSync extends SyncManager<Record<string, unknown>, AnyDoc, string> {
|
||||
@@ -324,6 +327,11 @@ async function doOpenDb(uid: string): Promise<void> {
|
||||
}
|
||||
});
|
||||
for (const r of res?.results ?? []) {
|
||||
if (r.status === 'invalid') {
|
||||
if (!rejected) status.error = `${name}: ${String(r.error ?? 'rejected')}`.slice(0, 200);
|
||||
rejected = true;
|
||||
continue;
|
||||
}
|
||||
if (r.status === 'ok' || !r.record || !isCollection(r.collection)) continue;
|
||||
const list = staleBuffer.get(r.collection) ?? [];
|
||||
list.push(toRecord(r.collection, r.record));
|
||||
@@ -377,6 +385,7 @@ export function syncNow(): Promise<void> {
|
||||
if (syncing) return syncing;
|
||||
const m = sm;
|
||||
if (!m || !status.online) return Promise.resolve();
|
||||
rejected = false;
|
||||
const run: Promise<void> = runBeforeSync()
|
||||
.then(() => m.syncAll())
|
||||
.catch((err: Error) => {
|
||||
|
||||
+43
-2
@@ -8,11 +8,15 @@ const mocks = vi.hoisted(() => {
|
||||
const server = new Map<string, Map<string, Record<string, unknown>>>();
|
||||
const pushes: { collection: string; id: string; data: Record<string, unknown>; client_updated_at: number; deleted: boolean }[] = [];
|
||||
const batches: number[] = [];
|
||||
const reject = { collection: '', error: '' };
|
||||
const slow = { name: '' };
|
||||
let tick = 0;
|
||||
return {
|
||||
server,
|
||||
pushes,
|
||||
batches,
|
||||
reject,
|
||||
slow,
|
||||
stamp: () => new Date(Date.UTC(2026, 0, 1) + tick++).toISOString().replace('T', ' ')
|
||||
};
|
||||
});
|
||||
@@ -22,7 +26,10 @@ vi.mock('$lib/pb', () => ({
|
||||
autoCancellation: () => undefined,
|
||||
filter: (tpl: string, params: Record<string, unknown>) => tpl.replace(/\{:(\w+)\}/g, (_m, key: string) => JSON.stringify(String(params[key]))),
|
||||
collection: (name: string) => ({
|
||||
getFullList: async () => [...(mocks.server.get(name)?.values() ?? [])],
|
||||
getFullList: async () => {
|
||||
if (mocks.slow.name === name) await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
return [...(mocks.server.get(name)?.values() ?? [])];
|
||||
},
|
||||
subscribe: async () => async () => undefined
|
||||
}),
|
||||
send: async (path: string, options: { method?: string; body?: unknown }) => {
|
||||
@@ -30,6 +37,8 @@ vi.mock('$lib/pb', () => ({
|
||||
const body = options.body as { changes: (typeof mocks.pushes)[number][] };
|
||||
mocks.batches.push(body.changes.length);
|
||||
const results = body.changes.map((c) => {
|
||||
mocks.pushes.push(c);
|
||||
if (mocks.reject.collection === c.collection) return { id: c.id, collection: c.collection, status: 'invalid', error: mocks.reject.error };
|
||||
const record = { id: c.id, ...c.data, client_updated_at: c.client_updated_at, rev: 1, deleted: c.deleted, updated: mocks.stamp() };
|
||||
let store = mocks.server.get(c.collection);
|
||||
if (!store) {
|
||||
@@ -37,7 +46,6 @@ vi.mock('$lib/pb', () => ({
|
||||
mocks.server.set(c.collection, store);
|
||||
}
|
||||
store.set(c.id, record);
|
||||
mocks.pushes.push(c);
|
||||
return { id: c.id, collection: c.collection, status: 'ok', record };
|
||||
});
|
||||
return { results };
|
||||
@@ -218,4 +226,37 @@ describe('sync boundary', () => {
|
||||
expect(Math.max(...mocks.batches)).toBeLessThanOrEqual(200);
|
||||
expect(mocks.batches.reduce((sum, size) => sum + size, 0)).toBe(250);
|
||||
}, 20000);
|
||||
|
||||
it('a rejected change is dropped and reported, and a later success of the round keeps the message', async () => {
|
||||
status.online = true;
|
||||
mocks.reject.collection = 'settings_history';
|
||||
mocks.reject.error = 'hours_per_week must be at most 168';
|
||||
mocks.slow.name = 'balance_adjustments';
|
||||
try {
|
||||
const id = await save('settings_history', null, settingsRow);
|
||||
await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 });
|
||||
await syncNow();
|
||||
expect(status.error).toBe('settings_history: hours_per_week must be at most 168');
|
||||
mocks.reject.collection = '';
|
||||
await syncNow();
|
||||
expect(status.error).toBeNull();
|
||||
expect(mocks.pushes.filter((c) => c.id === id)).toHaveLength(1);
|
||||
} finally {
|
||||
mocks.reject.collection = '';
|
||||
mocks.slow.name = '';
|
||||
}
|
||||
});
|
||||
|
||||
it('a long rejection is cut to 200 characters', async () => {
|
||||
status.online = true;
|
||||
mocks.reject.collection = 'balance_adjustments';
|
||||
mocks.reject.error = 'x'.repeat(300);
|
||||
try {
|
||||
await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 });
|
||||
await syncNow();
|
||||
expect(status.error).toBe(`balance_adjustments: ${'x'.repeat(200 - 'balance_adjustments: '.length)}`);
|
||||
} finally {
|
||||
mocks.reject.collection = '';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user