🐛 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:
+20
-17
@@ -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<CollectionName, string[]> = {
|
||||
entries: ['start_time', 'end_time'],
|
||||
@@ -217,25 +219,26 @@ async function doOpenDb(uid: string): Promise<void> {
|
||||
},
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user