diff --git a/src/app/vault/vault.component.ts b/src/app/vault/vault.component.ts index 201110e077..aea06cb4f7 100644 --- a/src/app/vault/vault.component.ts +++ b/src/app/vault/vault.component.ts @@ -36,8 +36,11 @@ import { CollectionView } from 'jslib/models/view/collectionView'; import { FolderView } from 'jslib/models/view/folderView'; import { I18nService } from 'jslib/abstractions/i18n.service'; +import { MessagingService } from 'jslib/abstractions/messaging.service'; import { SyncService } from 'jslib/abstractions/sync.service'; +const SyncInterval = 6 * 60 * 60 * 1000; // 6 hours + @Component({ selector: 'app-vault', template: template, @@ -64,7 +67,7 @@ export class VaultComponent implements OnInit { private componentFactoryResolver: ComponentFactoryResolver, private i18nService: I18nService, private broadcasterService: BroadcasterService, private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone, private syncService: SyncService, private analytics: Angulartics2, - private toasterService: ToasterService) { + private toasterService: ToasterService, private messagingService: MessagingService) { } async ngOnInit() { @@ -104,8 +107,25 @@ export class VaultComponent implements OnInit { this.toasterService.popAsync('error', null, this.i18nService.t('syncingFailed')); } break; + case 'checkSyncVault': + try { + const lastSync = await this.syncService.getLastSync(); + let lastSyncAgo = SyncInterval + 1; + if (lastSync != null) { + lastSyncAgo = new Date().getTime() - lastSync.getTime(); + } + + if (lastSyncAgo >= SyncInterval) { + await this.syncService.fullSync(false); + } + } catch { } + + this.messagingService.send('scheduleNextSync'); + break; case 'syncCompleted': - await this.load(); + if (message.successfully) { + await this.load(); + } break; default: detectChanges = false; diff --git a/src/main.ts b/src/main.ts index 548662feae..adf86bf9c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,7 @@ if (watch) { const i18nService = new I18nService('en', './locales/'); const windowMain = new WindowMain(dev); -const messagingMain = new MessagingMain(); +const messagingMain = new MessagingMain(windowMain); const menuMain = new MenuMain(windowMain, i18nService); windowMain.init(); diff --git a/src/main/messaging.main.ts b/src/main/messaging.main.ts index 03a81a6fc8..129c3d7488 100644 --- a/src/main/messaging.main.ts +++ b/src/main/messaging.main.ts @@ -1,10 +1,19 @@ import { app, ipcMain } from 'electron'; // import { getPassword, setPassword, deletePassword } from 'keytar'; +import { WindowMain } from './window.main'; + const KeytarService = 'bitwarden'; +const SyncInterval = 5 * 60 * 1000; // 5 minutes export class MessagingMain { + private syncTimeout: NodeJS.Timer; + + constructor(private windowMain: WindowMain) { } + init() { + this.scheduleNextSync(); + ipcMain.on('messagingService', async (event: any, message: any) => { switch (message.command) { case 'loggedIn': @@ -13,6 +22,9 @@ export class MessagingMain { break; case 'syncCompleted': break; + case 'scheduleNextSync': + this.scheduleNextSync(); + break; default: break; } @@ -40,4 +52,16 @@ export class MessagingMain { }); */ } + + private scheduleNextSync() { + if (this.syncTimeout) { + global.clearTimeout(this.syncTimeout); + } + + this.syncTimeout = global.setTimeout(() => { + this.windowMain.win.webContents.send('messagingService', { + command: 'checkSyncVault', + }); + }, SyncInterval); + } }