Skip to content

Commit a70251f

Browse files
committed
fix(sync): skip sync requests while offline
1 parent 68a063a commit a70251f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/app/core/database/sync.service.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ import { LoginStateSubject, SyncStateSubject } from "../session/session-type";
77
import { LoginState } from "../session/session-states/login-state.enum";
88
import { KeycloakAuthService } from "../session/auth/keycloak/keycloak-auth.service";
99
import { Subject } from "rxjs";
10+
import { NAVIGATOR_TOKEN } from "../../utils/di-tokens";
1011

1112
describe("SyncService", () => {
1213
let service: SyncService;
1314
let loginState: LoginStateSubject;
1415
let mockAuthService: jasmine.SpyObj<KeycloakAuthService>;
16+
let mockNavigator;
1517

1618
beforeEach(() => {
1719
mockAuthService = jasmine.createSpyObj(["login", "addAuthHeader"]);
20+
mockNavigator = { onLine: true };
21+
1822
TestBed.configureTestingModule({
1923
providers: [
2024
{ provide: KeycloakAuthService, useValue: mockAuthService },
2125
{ provide: Database, useClass: PouchDatabase },
2226
LoginStateSubject,
2327
SyncStateSubject,
28+
{ provide: NAVIGATOR_TOKEN, useValue: mockNavigator },
2429
],
2530
});
2631
service = TestBed.inject(SyncService);
@@ -94,4 +99,24 @@ describe("SyncService", () => {
9499

95100
stopPeriodicTimer();
96101
}));
102+
103+
it("should skip sync calls when offline", fakeAsync(() => {
104+
const mockLocalDb = jasmine.createSpyObj(["sync"]);
105+
mockLocalDb.sync.and.resolveTo({});
106+
const db = TestBed.inject(Database) as PouchDatabase;
107+
spyOn(db, "getPouchDB").and.returnValue(mockLocalDb);
108+
109+
mockNavigator.onLine = false;
110+
111+
service.startSync();
112+
113+
tick(1000);
114+
expect(mockLocalDb.sync).not.toHaveBeenCalled();
115+
116+
mockNavigator.onLine = true;
117+
tick(SyncService.SYNC_INTERVAL);
118+
expect(mockLocalDb.sync).toHaveBeenCalled();
119+
120+
stopPeriodicTimer();
121+
}));
97122
});

src/app/core/database/sync.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from "@angular/core";
1+
import { Inject, Injectable } from "@angular/core";
22
import { Database } from "./database";
33
import { PouchDatabase } from "./pouch-database";
44
import { Logging } from "../logging/logging.service";
@@ -16,6 +16,7 @@ import { Config } from "../config/config";
1616
import { Entity } from "../entity/model/entity";
1717
import { from, interval, merge, of } from "rxjs";
1818
import { environment } from "../../../environments/environment";
19+
import { NAVIGATOR_TOKEN } from "../../utils/di-tokens";
1920

2021
/**
2122
* This service initializes the remote DB and manages the sync between the local and remote DB.
@@ -36,6 +37,7 @@ export class SyncService {
3637
private database: Database,
3738
private authService: KeycloakAuthService,
3839
private syncStateSubject: SyncStateSubject,
40+
@Inject(NAVIGATOR_TOKEN) private navigator: Navigator,
3941
) {
4042
this.remoteDatabase = new PouchDatabase(this.authService);
4143

@@ -89,6 +91,12 @@ export class SyncService {
8991
* Execute a (one-time) sync between the local and server database.
9092
*/
9193
sync(): Promise<SyncResult> {
94+
if (!this.navigator.onLine) {
95+
Logging.debug("Not syncing because offline");
96+
this.syncStateSubject.next(SyncState.UNSYNCED);
97+
return Promise.resolve({});
98+
}
99+
92100
this.syncStateSubject.next(SyncState.STARTED);
93101

94102
return this.localDB

0 commit comments

Comments
 (0)