Skip to content

Commit

Permalink
fix(core): retry subscription to changes feed upon errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Jul 2, 2024
1 parent 2306101 commit 5148e8a
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/app/core/database/pouch-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,29 +283,34 @@ export class PouchDatabase extends Database {
changes(prefix: string): Observable<any> {
if (!this.changesFeed) {
this.changesFeed = new Subject();
this.getPouchDBOnceReady()
.then((pouchDB) =>
pouchDB
.changes({
live: true,
since: "now",
include_docs: true,
})
.addListener("change", (change) =>
this.changesFeed.next(change.doc),
),
)
.catch((err) => {
if (err.statusCode === HttpStatusCode.Unauthorized) {
this.loggingService.warn(err);
} else {
throw err;
}
});
this.subscribeChanges();
}
return this.changesFeed.pipe(filter((doc) => doc._id.startsWith(prefix)));
}

private async subscribeChanges() {
(await this.getPouchDBOnceReady())
.changes({
live: true,
since: "now",
include_docs: true,
})
.addListener("change", (change) => this.changesFeed.next(change.doc))
.catch((err) => {
if (
err.statusCode === HttpStatusCode.Unauthorized ||
err.statusCode === HttpStatusCode.GatewayTimeout
) {
this.loggingService.warn(err);
} else {
this.loggingService.error(err);
}

// retry
setTimeout(() => this.subscribeChanges(), 10000);
});
}

/**
* Destroy the database and all saved data
*/
Expand Down

0 comments on commit 5148e8a

Please sign in to comment.