src/app/services/update.service.ts
Properties |
|
Methods |
Public
constructor(appRef: ApplicationRef, dialog: MatDialog, log: NGXLogger, swUpdate: SwUpdate)
|
|||||||||||||||
Defined in src/app/services/update.service.ts:16
|
|||||||||||||||
Parameters :
|
Public start |
start()
|
Defined in src/app/services/update.service.ts:25
|
Returns :
void
|
Public stop |
stop()
|
Defined in src/app/services/update.service.ts:58
|
Returns :
void
|
Private Optional checkForUpdateSubscription |
Type : Subscription
|
Defined in src/app/services/update.service.ts:14
|
Private Optional updateActivatedSubscription |
Type : Subscription
|
Defined in src/app/services/update.service.ts:16
|
Private Optional updateAvailableSubscription |
Type : Subscription
|
Defined in src/app/services/update.service.ts:15
|
import { ApplicationRef, Injectable } from '@angular/core';
import { MatLegacyDialog as MatDialog } from '@angular/material/legacy-dialog';
import { SwUpdate } from '@angular/service-worker';
import { NGXLogger } from 'ngx-logger';
import { concat, interval, Subscription } from 'rxjs';
import { first } from 'rxjs/operators';
import { UpdateAvailableDialog } from 'src/app/dialogs/update-available/update-available.dialog';
@Injectable({
providedIn: 'root',
})
export class UpdateService {
private checkForUpdateSubscription?: Subscription;
private updateAvailableSubscription?: Subscription;
private updateActivatedSubscription?: Subscription;
public constructor(
private readonly appRef: ApplicationRef,
private readonly dialog: MatDialog,
private readonly log: NGXLogger,
private readonly swUpdate: SwUpdate,
) {}
public start(): void {
if (!this.swUpdate.isEnabled) {
return;
}
if (this.checkForUpdateSubscription !== undefined || this.updateAvailableSubscription !== undefined || this.updateActivatedSubscription !== undefined) {
this.stop();
}
const appIsStable$ = this.appRef.isStable.pipe(first((isStable) => isStable));
const everyThirtyMinutes$ = interval(30 * 60 * 1000);
const everyThirtyMinutesOnceAppIsStable$ = concat(appIsStable$, everyThirtyMinutes$);
this.checkForUpdateSubscription = everyThirtyMinutesOnceAppIsStable$.subscribe(() => {
this.log.debug('Checking for updates');
return this.swUpdate.checkForUpdate();
});
this.updateAvailableSubscription = this.swUpdate.available.subscribe(() => {
this.log.debug('Update available');
this.dialog
.open(UpdateAvailableDialog)
.afterClosed()
.subscribe(() => {
this.log.debug('Updating');
return this.swUpdate.activateUpdate().then(() => document.location.reload());
});
});
this.updateActivatedSubscription = this.swUpdate.activated.subscribe(() => this.log.info('Updated'));
this.log.debug('UpdateService started');
}
public stop(): void {
this.checkForUpdateSubscription?.unsubscribe();
this.updateAvailableSubscription?.unsubscribe();
this.updateActivatedSubscription?.unsubscribe();
this.log.debug('UpdateService stopped');
}
}