@memrosetta/sync-client 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +38 -26
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -247,6 +247,7 @@ function applyInboxOps(db, ops) {
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
// src/sync-client.ts
|
|
250
|
+
var MAX_OPS_PER_PUSH = 400;
|
|
250
251
|
var SyncClient = class {
|
|
251
252
|
db;
|
|
252
253
|
config;
|
|
@@ -293,36 +294,47 @@ var SyncClient = class {
|
|
|
293
294
|
this.setState("last_push_success_at", now);
|
|
294
295
|
return { pushed: 0, results: [], highWatermark: 0 };
|
|
295
296
|
}
|
|
296
|
-
const baseCursor = this.getCursor();
|
|
297
|
-
const wireOps = pending.map((op) => ({
|
|
298
|
-
...op,
|
|
299
|
-
payload: typeof op.payload === "string" ? JSON.parse(op.payload) : op.payload
|
|
300
|
-
}));
|
|
301
297
|
const url = `${this.config.serverUrl}/sync/push`;
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
298
|
+
const aggregatedResults = [];
|
|
299
|
+
let totalPushed = 0;
|
|
300
|
+
let highWatermark = 0;
|
|
301
|
+
for (let start = 0; start < pending.length; start += MAX_OPS_PER_PUSH) {
|
|
302
|
+
const chunk = pending.slice(start, start + MAX_OPS_PER_PUSH);
|
|
303
|
+
const baseCursor = this.getCursor();
|
|
304
|
+
const wireOps = chunk.map((op) => ({
|
|
305
|
+
...op,
|
|
306
|
+
payload: typeof op.payload === "string" ? JSON.parse(op.payload) : op.payload
|
|
307
|
+
}));
|
|
308
|
+
const response = await fetch(url, {
|
|
309
|
+
method: "POST",
|
|
310
|
+
headers: {
|
|
311
|
+
"Content-Type": "application/json",
|
|
312
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
313
|
+
},
|
|
314
|
+
body: JSON.stringify({
|
|
315
|
+
deviceId: this.config.deviceId,
|
|
316
|
+
baseCursor,
|
|
317
|
+
ops: wireOps
|
|
318
|
+
})
|
|
319
|
+
});
|
|
320
|
+
if (!response.ok) {
|
|
321
|
+
throw new Error(
|
|
322
|
+
`Push failed: ${response.status} ${response.statusText}`
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
const body = await response.json();
|
|
326
|
+
const { results, highWatermark: batchHigh } = body.data;
|
|
327
|
+
const pushedIds = results.filter((r) => r.status === "accepted" || r.status === "duplicate").map((r) => r.opId);
|
|
328
|
+
this.outbox.markPushed(pushedIds);
|
|
329
|
+
this.setCursor(batchHigh);
|
|
330
|
+
aggregatedResults.push(...results);
|
|
331
|
+
totalPushed += pushedIds.length;
|
|
332
|
+
highWatermark = batchHigh;
|
|
316
333
|
}
|
|
317
|
-
const body = await response.json();
|
|
318
|
-
const { results, highWatermark } = body.data;
|
|
319
|
-
const pushedIds = results.filter((r) => r.status === "accepted" || r.status === "duplicate").map((r) => r.opId);
|
|
320
|
-
this.outbox.markPushed(pushedIds);
|
|
321
|
-
this.setCursor(highWatermark);
|
|
322
334
|
this.setState("last_push_success_at", (/* @__PURE__ */ new Date()).toISOString());
|
|
323
335
|
return {
|
|
324
|
-
pushed:
|
|
325
|
-
results,
|
|
336
|
+
pushed: totalPushed,
|
|
337
|
+
results: aggregatedResults,
|
|
326
338
|
highWatermark
|
|
327
339
|
};
|
|
328
340
|
}
|