src/app/services/app-updater.service.ts
Properties |
Methods |
constructor(appRef: ApplicationRef, updates: SwUpdate, snackbar: MatSnackBar)
|
||||||||||||
Defined in src/app/services/app-updater.service.ts:14
|
||||||||||||
Parameters :
|
askToUpdate |
askToUpdate()
|
Defined in src/app/services/app-updater.service.ts:38
|
Asks to update. When an update is available, it displays a snackbar notifying the user about the update.
Returns :
void
|
checkForUpdates |
checkForUpdates()
|
Defined in src/app/services/app-updater.service.ts:27
|
Checks for updates. creates an observable that emits when the application is stable and checkUpdateInterval is completed. Subsription calls the checkForUpdates function to poll if there's an update in the build.
Returns :
void
|
closeSnackbar |
closeSnackbar()
|
Defined in src/app/services/app-updater.service.ts:63
|
Closes snackbar.
Returns :
void
|
openSnackBar |
openSnackBar()
|
Defined in src/app/services/app-updater.service.ts:53
|
Opens snack bar. Uses angular material snackbar service to open the snackbar. When 'REFRESH' button is pressed, updateApp function is called.
Returns :
void
|
updateApp |
updateApp()
|
Defined in src/app/services/app-updater.service.ts:71
|
Updates app whenever there is an active update available
Returns :
void
|
checkUpdateInterval |
Default value : 1000 * 60 * 60 * 2
|
Defined in src/app/services/app-updater.service.ts:14
|
isSnackbarloaded |
Default value : false
|
Defined in src/app/services/app-updater.service.ts:13
|
import { ApplicationRef, Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SwUpdate } from '@angular/service-worker';
import { concat, interval } from 'rxjs';
import { first } from 'rxjs/operators';
import { AppUpdateNotificationComponent } from '../app-update-notification/app-update-notification.component';
@Injectable({
providedIn: 'root'
})
export class AppUpdaterService {
isSnackbarloaded = false;
checkUpdateInterval = 1000 * 60 * 60 * 2; // 2 hours
constructor(
private readonly appRef: ApplicationRef,
private readonly updates: SwUpdate,
private readonly snackbar: MatSnackBar
) { }
/**
* Checks for updates.
* creates an observable that emits when the application is stable and checkUpdateInterval is completed.
* Subsription calls the checkForUpdates function to poll if there's an update in the build.
*/
checkForUpdates() {
const appIsStable = this.appRef.isStable.pipe(first(isStable => isStable === true));
const checkInterval = interval(this.checkUpdateInterval);
const checkUpdateObservable = concat(appIsStable, checkInterval);
checkUpdateObservable.subscribe(() => this.updates.checkForUpdate());
}
/**
* Asks to update.
* When an update is available, it displays a snackbar notifying the user about the update.
*/
askToUpdate() {
this.updates.available.subscribe(event => {
if (!this.isSnackbarloaded) {
this.openSnackBar();
this.isSnackbarloaded = true;
}
});
}
/**
* Opens snack bar.
* Uses angular material snackbar service to open the snackbar.
* When 'REFRESH' button is pressed, updateApp function is called.
*/
openSnackBar() {
this.snackbar.openFromComponent(AppUpdateNotificationComponent, {
horizontalPosition: 'left',
verticalPosition: 'bottom'
});
}
/**
* Closes snackbar.
*/
closeSnackbar() {
this.isSnackbarloaded = false;
this.snackbar.dismiss();
}
/**
* Updates app whenever there is an active update available
*/
updateApp() {
this.updates.activateUpdate().then(() => document.location.reload());
}
}