File

src/app/services/app-updater.service.ts

Index

Properties
Methods

Constructor

constructor(appRef: ApplicationRef, updates: SwUpdate, snackbar: MatSnackBar)
Parameters :
Name Type Optional
appRef ApplicationRef No
updates SwUpdate No
snackbar MatSnackBar No

Methods

askToUpdate
askToUpdate()

Asks to update. When an update is available, it displays a snackbar notifying the user about the update.

Returns : void
checkForUpdates
checkForUpdates()

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

Closes snackbar.

Returns : void
openSnackBar
openSnackBar()

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

Updates app whenever there is an active update available

Returns : void

Properties

checkUpdateInterval
Default value : 1000 * 60 * 60 * 2
isSnackbarloaded
Default value : false
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());
  }
}

result-matching ""

    No results matching ""