disconnect and connect on idle states

This commit is contained in:
Kyle Spearrin 2018-08-22 21:10:23 -04:00
parent cde6697721
commit 22c991f655
3 changed files with 39 additions and 8 deletions

2
jslib

@ -1 +1 @@
Subproject commit a67ea2422f082c6b884a3c7187e17a318048f7f5
Subproject commit d37fa836da523ae69413881f4af929c6808a688b

View File

@ -1,18 +1,21 @@
import MainBackground from './main.background';
import { ConstantsService } from 'jslib/services';
import { ConstantsService } from 'jslib/services/constants.service';
import {
LockService,
StorageService,
} from 'jslib/abstractions';
import { NotificationsService } from 'jslib/abstractions/notifications.service';
const IdleInterval = 60 * 5; // 5 minutes
export default class IdleBackground {
private idle: any;
private idleTimer: number = null;
private idleState = 'active';
constructor(private main: MainBackground, private lockService: LockService,
private storageService: StorageService) {
this.idle = chrome.idle;
constructor(private lockService: LockService, private storageService: StorageService,
private notificationsService: NotificationsService) {
this.idle = chrome.idle || browser.idle;
}
async init() {
@ -20,6 +23,20 @@ export default class IdleBackground {
return;
}
const idleHandler = (newState: string) => {
if (newState === 'active') {
this.notificationsService.reconnectFromActivity();
} else {
this.notificationsService.disconnectFromInactivity();
}
};
if (this.idle.onStateChanged && this.idle.setDetectionInterval) {
this.idle.setDetectionInterval(IdleInterval);
this.idle.onStateChanged.addListener(idleHandler);
} else {
this.pollIdle(idleHandler);
}
if (this.idle.onStateChanged) {
this.idle.onStateChanged.addListener(async (newState: string) => {
if (newState === 'locked') {
@ -31,4 +48,18 @@ export default class IdleBackground {
});
}
}
private pollIdle(handler: (newState: string) => void) {
if (this.idleTimer != null) {
window.clearTimeout(this.idleTimer);
this.idleTimer = null;
}
this.idle.queryState(IdleInterval, (state: string) => {
if (state !== this.idleState) {
this.idleState = state;
handler(state);
}
this.idleTimer = window.setTimeout(() => this.pollIdle(handler), 5000);
});
}
}

View File

@ -180,7 +180,7 @@ export default class MainBackground {
if (!this.isSafari) {
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
this.passwordGenerationService, this.analytics, this.platformUtilsService);
this.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
this.idleBackground = new IdleBackground(this.lockService, this.storageService, this.notificationsService);
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
this.windowsBackground = new WindowsBackground(this);
}