From db1a632c652cffe78346c4f8dbc6f361dbc9ee1d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 23 Aug 2018 08:56:23 -0400 Subject: [PATCH] sync on reconnect --- src/abstractions/notifications.service.ts | 2 +- src/services/notifications.service.ts | 34 ++++++++++++----------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/abstractions/notifications.service.ts b/src/abstractions/notifications.service.ts index e89896550d..91db76bdd6 100644 --- a/src/abstractions/notifications.service.ts +++ b/src/abstractions/notifications.service.ts @@ -2,7 +2,7 @@ import { EnvironmentService } from './environment.service'; export abstract class NotificationsService { init: (environmentService: EnvironmentService) => Promise; - updateConnection: () => Promise; + updateConnection: (sync?: boolean) => Promise; reconnectFromActivity: () => Promise; disconnectFromInactivity: () => Promise; } diff --git a/src/services/notifications.service.ts b/src/services/notifications.service.ts index 896e7b9d3a..5071b94ccd 100644 --- a/src/services/notifications.service.ts +++ b/src/services/notifications.service.ts @@ -55,18 +55,18 @@ export class NotificationsService implements NotificationsServiceAbstraction { (data: any) => this.processNotification(new NotificationResponse(data))); this.signalrConnection.onclose(() => { this.connected = false; - this.reconnect(); + this.reconnect(true); }); this.inited = true; if (await this.isAuthedAndUnlocked()) { - await this.connect(); + await this.reconnect(false); } } - async updateConnection(): Promise { + async updateConnection(sync = false): Promise { try { if (await this.isAuthedAndUnlocked()) { - await this.connect(); + await this.reconnect(sync); } else { await this.signalrConnection.stop(); } @@ -77,16 +77,19 @@ export class NotificationsService implements NotificationsServiceAbstraction { } async reconnectFromActivity(): Promise { + if (!this.inited) { + return; + } this.inactive = false; if (!this.connected) { - if (await this.isAuthedAndUnlocked()) { - await this.reconnect(); - await this.syncService.fullSync(false); - } + await this.reconnect(true); } } async disconnectFromInactivity(): Promise { + if (!this.inited) { + return; + } this.inactive = true; if (this.connected) { await this.signalrConnection.stop(); @@ -133,12 +136,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { } } - private async connect() { - await this.signalrConnection.start(); - this.connected = true; - } - - private async reconnect() { + private async reconnect(sync: boolean) { if (this.reconnectTimer != null) { clearTimeout(this.reconnectTimer); this.reconnectTimer = null; @@ -152,11 +150,15 @@ export class NotificationsService implements NotificationsServiceAbstraction { } try { - await this.connect(); + await this.signalrConnection.start(); + this.connected = true; + if (sync) { + await this.syncService.fullSync(false); + } } catch { } if (!this.connected) { - this.reconnectTimer = setTimeout(() => this.reconnect(), 120000); + this.reconnectTimer = setTimeout(() => this.reconnect(sync), 120000); } }