automatic sync intervals

This commit is contained in:
Kyle Spearrin 2018-02-09 15:49:00 -05:00
parent ac647f3184
commit 3b3750734b
3 changed files with 47 additions and 3 deletions

View File

@ -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':
if (message.successfully) {
await this.load();
}
break;
default:
detectChanges = false;

View File

@ -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();

View File

@ -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);
}
}