-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor background task initialization
- Loading branch information
Serghei Paduret
committed
Jul 7, 2021
1 parent
c738245
commit 500f1ef
Showing
8 changed files
with
76 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 2 additions & 50 deletions
52
src/app/components/background-page/background-page.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,16 @@ | ||
import {Component, OnInit} from '@angular/core'; | ||
import {BackgroundService} from '../../services/background.service'; | ||
// @ts-ignore | ||
import Alarm = chrome.alarms.Alarm; | ||
import {DataService} from '../../services/data.service'; | ||
|
||
// @ts-ignore | ||
let chr: any = chrome; | ||
|
||
@Component({ | ||
selector: 'app-background-service', | ||
templateUrl: './background-page.component.html', | ||
styleUrls: ['./background-page.component.css'] | ||
}) | ||
export class BackgroundPageComponent implements OnInit { | ||
|
||
constructor(private service: BackgroundService, private dataService: DataService) { | ||
constructor(private service: BackgroundService) { | ||
} | ||
|
||
ngOnInit(): void { | ||
chr.alarms.clearAll((wasCleared: boolean) => { | ||
if (wasCleared) { | ||
console.debug('alarms cleared'); | ||
} | ||
|
||
if (chr.alarms.onAlarm.hasListeners()) { | ||
console.error('alarm listener is already defined'); | ||
this.showError('alarm listener is already defined'); | ||
} else { | ||
chr.alarms.create({periodInMinutes: 1}); | ||
console.debug('alarm created'); | ||
|
||
this.addAlarmListener(); | ||
|
||
if (chr.alarms.onAlarm.hasListeners()) { | ||
console.debug('alarm listener has been added'); | ||
} else { | ||
console.error('alarm listener was not added'); | ||
this.showError('alarm listener was not added'); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
addAlarmListener() { | ||
chr.alarms.onAlarm.addListener((alarm: Alarm) => { | ||
this.service.doWork(); | ||
|
||
const settings = this.dataService.getExtensionSettings(); | ||
const newInterval = settings.refreshIntervalInMinutes; | ||
if (settings.refreshIntervalInMinutes !== alarm.periodInMinutes) { | ||
console.log(`interval changed from ${alarm.periodInMinutes} to ${newInterval}, restarting alarm...`); | ||
|
||
chr.alarms.create({periodInMinutes: settings.refreshIntervalInMinutes}); | ||
console.debug('alarm created'); | ||
} | ||
}); | ||
} | ||
|
||
showError(message: string) { | ||
chr.browserAction.setBadgeBackgroundColor({color: 'red'}); | ||
chr.browserAction.setBadgeText({text: message}); | ||
this.service.setupAlarms(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { enableProdMode } from '@angular/core'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
import { environment } from './environments/environment'; | ||
import {enableProdMode} from '@angular/core'; | ||
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; | ||
import {AppModule} from './app/app.module'; | ||
import {environment} from './environments/environment'; | ||
|
||
if (environment.production) { | ||
enableProdMode(); | ||
} | ||
|
||
platformBrowserDynamic().bootstrapModule(AppModule) | ||
platformBrowserDynamic() | ||
.bootstrapModule(AppModule) | ||
.catch(err => console.error(err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters