Skip to content

Commit 91db686

Browse files
Catch closed errors for hold requests. Use crud throttle time for crud retries.
1 parent 0fc0d73 commit 91db686

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ The next upload iteration will be delayed.`);
429429
uploadError: ex
430430
}
431431
});
432-
await this.delayRetry(controller.signal);
432+
await this.delayRetry(controller.signal, this.options.crudUploadThrottleMs);
433433
if (!this.isConnected) {
434434
// Exit the upload loop if the sync stream is no longer connected
435435
break;
@@ -1216,15 +1216,14 @@ The next upload iteration will be delayed.`);
12161216
this.iterateListeners((cb) => cb.statusUpdated?.(options));
12171217
}
12181218

1219-
private async delayRetry(signal?: AbortSignal): Promise<void> {
1219+
private async delayRetry(signal?: AbortSignal, delayMs?: number): Promise<void> {
12201220
return new Promise((resolve) => {
12211221
if (signal?.aborted) {
12221222
// If the signal is already aborted, resolve immediately
12231223
resolve();
12241224
return;
12251225
}
1226-
1227-
const { retryDelayMs } = this.options;
1226+
const delay = delayMs ?? this.options.retryDelayMs;
12281227

12291228
let timeoutId: ReturnType<typeof setTimeout> | undefined;
12301229

@@ -1238,7 +1237,7 @@ The next upload iteration will be delayed.`);
12381237
};
12391238

12401239
signal?.addEventListener('abort', endDelay, { once: true });
1241-
timeoutId = setTimeout(endDelay, retryDelayMs);
1240+
timeoutId = setTimeout(endDelay, delay);
12421241
});
12431242
}
12441243

packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,16 @@ export class LockedAsyncDatabaseAdapter
251251
if (timeoutId) {
252252
clearTimeout(timeoutId);
253253
}
254-
const holdId = this.requiresHolds ? await this.baseDB.markHold() : null;
254+
let holdId: string | null = null;
255255
try {
256256
if (this.requiresReOpen) {
257+
this.logger.debug('Re-opening database');
257258
await this.openInternalDB();
259+
this.logger.debug('Database re-opened');
258260
this.requiresReOpen = false;
259261
}
262+
263+
holdId = this.requiresHolds ? await this.baseDB.markHold() : null;
260264
return await callback();
261265
} catch (ex) {
262266
if (ex instanceof WorkerConnectionClosedError) {

packages/web/tests/multiple_instances.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,12 @@ describe('Multiple Instances', { sequential: true }, () => {
284284
await stream2.dispose();
285285
});
286286

287-
it('should trigger uploads from last connected clients', async () => {
287+
it('should trigger uploads from last connected clients', { timeout: Infinity }, async () => {
288288
// Generate the first streaming sync implementation
289289
const connector1 = new TestConnector();
290290
const spy1 = vi.spyOn(connector1, 'uploadData');
291291

292+
await new Promise((resolve) => setTimeout(resolve, 5000));
292293
const db = openDatabase();
293294
await db.init();
294295
// They need to use the same identifier to use the same shared worker.
@@ -380,8 +381,6 @@ describe('Multiple Instances', { sequential: true }, () => {
380381
// Close the second client, leaving only the first one
381382
await stream2.dispose();
382383

383-
// The dispose above will disconnect, but we need to wait for the sync stream to be created before we can update the status
384-
await vi.waitFor(() => expect(stream1.syncStatus.connecting).true);
385384
// Hack, set the status to connected in order to trigger the upload
386385
await (stream1 as any)['_testUpdateStatus'](new SyncStatus({ connected: true }));
387386
stream1.triggerCrudUpload();

0 commit comments

Comments
 (0)