🐛 Push sync changes in batches of 200

The server rejects more than 500 changes per request, so one large
offline batch would otherwise stay pending forever.
This commit is contained in:
2026-11-29 20:16:45 +00:00
parent 98f3e8048c
commit 29f3cb9926
+9 -6
View File
@@ -28,6 +28,8 @@ interface PushResult {
const SYNC_ID = 'wotra'; const SYNC_ID = 'wotra';
const SCHEMA_VERSION = 1; const SCHEMA_VERSION = 1;
/** Changes per push request; the server rejects more than 500. */
const PUSH_BATCH = 200;
const NUMBER_FIELDS: Record<CollectionName, string[]> = { const NUMBER_FIELDS: Record<CollectionName, string[]> = {
entries: ['start_time', 'end_time'], entries: ['start_time', 'end_time'],
@@ -217,17 +219,17 @@ async function doOpenDb(uid: string): Promise<void> {
}, },
push: async ({ name }, { changes }) => { push: async ({ name }, { changes }) => {
const items = [...changes.added, ...changes.modified]; const items = [...changes.added, ...changes.modified];
if (items.length === 0) return;
const n = name as CollectionName; const n = name as CollectionName;
for (let i = 0; i < items.length; i += PUSH_BATCH) {
const res = await pb.send<{ results: PushResult[] }>('/api/sync/push', { const res = await pb.send<{ results: PushResult[] }>('/api/sync/push', {
method: 'POST', method: 'POST',
body: { body: {
changes: items.map((i) => ({ changes: items.slice(i, i + PUSH_BATCH).map((it) => ({
collection: name, collection: name,
id: i.id, id: it.id,
data: toServerData(n, i), data: toServerData(n, it),
client_updated_at: i.client_updated_at, client_updated_at: it.client_updated_at,
deleted: i.deleted deleted: it.deleted
})) }))
} }
}); });
@@ -237,6 +239,7 @@ async function doOpenDb(uid: string): Promise<void> {
list.push(toRecord(r.collection, r.record)); list.push(toRecord(r.collection, r.record));
staleBuffer.set(r.collection, list); staleBuffer.set(r.collection, list);
} }
}
}, },
registerRemoteChange: async ({ name }, onChange) => { registerRemoteChange: async ({ name }, onChange) => {
try { try {