diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index c93efac..c2d81eb 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -28,6 +28,8 @@ interface PushResult { const SYNC_ID = 'wotra'; const SCHEMA_VERSION = 1; +/** Changes per push request; the server rejects more than 500. */ +const PUSH_BATCH = 200; const NUMBER_FIELDS: Record = { entries: ['start_time', 'end_time'], @@ -217,25 +219,26 @@ async function doOpenDb(uid: string): Promise { }, push: async ({ name }, { changes }) => { const items = [...changes.added, ...changes.modified]; - if (items.length === 0) return; const n = name as CollectionName; - const res = await pb.send<{ results: PushResult[] }>('/api/sync/push', { - method: 'POST', - body: { - changes: items.map((i) => ({ - collection: name, - id: i.id, - data: toServerData(n, i), - client_updated_at: i.client_updated_at, - deleted: i.deleted - })) + for (let i = 0; i < items.length; i += PUSH_BATCH) { + const res = await pb.send<{ results: PushResult[] }>('/api/sync/push', { + method: 'POST', + body: { + changes: items.slice(i, i + PUSH_BATCH).map((it) => ({ + collection: name, + id: it.id, + data: toServerData(n, it), + client_updated_at: it.client_updated_at, + deleted: it.deleted + })) + } + }); + for (const r of res?.results ?? []) { + if (r.status === 'ok' || !r.record || !isCollection(r.collection)) continue; + const list = staleBuffer.get(r.collection) ?? []; + list.push(toRecord(r.collection, r.record)); + staleBuffer.set(r.collection, list); } - }); - for (const r of res?.results ?? []) { - if (r.status === 'ok' || !r.record || !isCollection(r.collection)) continue; - const list = staleBuffer.get(r.collection) ?? []; - list.push(toRecord(r.collection, r.record)); - staleBuffer.set(r.collection, list); } }, registerRemoteChange: async ({ name }, onChange) => {