Skip to content

Commit 86ea3b7

Browse files
Add wrapper
1 parent 8c3f158 commit 86ea3b7

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

packages/firestore/src/remote/remote_store.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ export class RemoteStore implements TargetMetadataProvider {
484484
}
485485
}
486486

487+
/**
488+
* Executes `op`. If `op` fails, takes the network offline until `op`
489+
* succeeds. Returns after the first attempt.
490+
*/
491+
private executeWithRecovery(op: () => Promise<void>): Promise<void> {
492+
return op().catch(e => this.disableNetworkUntilRecovery(e, op));
493+
}
494+
487495
/**
488496
* Takes a batch of changes from the Datastore, repackages them as a
489497
* RemoteEvent, and passes that on to the listener, which is typically the
@@ -678,13 +686,10 @@ export class RemoteStore implements TargetMetadataProvider {
678686
);
679687
const batch = this.writePipeline.shift()!;
680688
const success = MutationBatchResult.from(batch, commitVersion, results);
681-
try {
682-
await this.syncEngine.applySuccessfulWrite(success);
683-
} catch (e) {
684-
await this.disableNetworkUntilRecovery(e, () =>
685-
this.syncEngine.applySuccessfulWrite(success)
686-
);
687-
}
689+
690+
await this.executeWithRecovery(() =>
691+
this.syncEngine.applySuccessfulWrite(success)
692+
);
688693

689694
// It's possible that with the completion of this mutation another
690695
// slot has freed up.
@@ -728,13 +733,9 @@ export class RemoteStore implements TargetMetadataProvider {
728733
// restart.
729734
this.writeStream.inhibitBackoff();
730735

731-
try {
732-
await this.syncEngine.rejectFailedWrite(batch.batchId, error);
733-
} catch (e) {
734-
await this.disableNetworkUntilRecovery(e, () =>
735-
this.syncEngine.rejectFailedWrite(batch.batchId, error)
736-
);
737-
}
736+
await this.executeWithRecovery(() =>
737+
this.syncEngine.rejectFailedWrite(batch.batchId, error)
738+
);
738739

739740
// It's possible that with the completion of this mutation
740741
// another slot has freed up.

0 commit comments

Comments
 (0)