Skip to content

Instantly share code, notes, and snippets.

@grostarin
Last active May 4, 2020 13:19
Show Gist options
  • Save grostarin/3594225f58963f99e62dfbb10e7e4ded to your computer and use it in GitHub Desktop.
Save grostarin/3594225f58963f99e62dfbb10e7e4ded to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { interval } from 'rxjs';
import { MatSnackBar } from '@angular/material';
import { environment } from '@environments/environment';
@Injectable({ providedIn: 'root' })
export class ThisSoftwareUpdateService {
constructor(
private http: HttpClient,
private _snackBar: MatSnackBar) { }
// this will be replaced by actual timestamp post-build.js
private currentTimestamp = '{{POST_BUILD_ENTERS_TIMESTAMP_HERE}}'
private appJsonFile = 'software.json'
public initApplicationUpdating() {
console.debug("SoftwareUpdateService.initApplicationUpdating()")
let frequency = environment.applicationVersionCheckDelayMs
console.debug("SoftwareUpdateService.initApplicationUpdating()", frequency)
let frequencyNumber = Number(frequency);
if (isNaN(frequencyNumber) || 0 >= frequencyNumber) {
console.warn("SoftwareUpdateService.initApplicationUpdating() no frequency : runApplicationUpdating is skipped")
} else {
// on application init, force reload if necessary
this.runApplicationCheck(true)
// check will be done to reload on user validation
interval(frequencyNumber).subscribe(val => this.runApplicationCheck(false))
}
}
/**
* Will get new application timestamp and launch application update if needed
* @param forceReload
*/
private runApplicationCheck(forceReload: boolean = false): void {
console.debug("SoftwareUpdateService.runApplicationCheck()", forceReload)
let url = environment.applicationUrl + '/' + this.appJsonFile
// get file plus timestamp in request to invalidate caches
this.http.get(url + '?t=' + new Date().getTime())
.subscribe(
(response: any) => {
// get timestamp in json
const newTimestamp = response.timestamp;
const applicationNeedsToBeUpdated = this.isApplicationHasBeenUpdatedOnServer(newTimestamp);
// If application needs to be updated
if (applicationNeedsToBeUpdated) {
this.updateApplication(forceReload);
} else {
console.debug("SoftwareUpdateService.runApplicationCheck() application does not need to be updated")
}
},
(err) => {
console.error(err, `SoftwareUpdateService.runApplicationCheck() : could not get ${this.appJsonFile}`);
}
);
}
/**
* Reload application, user may be asked for validation
* if forceReload is false then user will be asked for application refresh
* if foceTeload is true then application will be refreshed
* @param forceReload
*/
private updateApplication(forceReload: boolean): void {
console.debug("SoftwareUpdateService.updateApplication() application has to be updated")
if (forceReload) {
// Force reload
console.debug("SoftwareUpdateService.updateApplication() refresh forced")
window.location.reload();
} else {
// ask for user's validation to reload
let snackBarRef = this._snackBar.open("Application has been updated ! OK to get refreshed ?", "Update application", {});
snackBarRef.onAction().subscribe(() => {
// Refresh application when user asks for...
console.debug("SoftwareUpdateService.updateApplication() refresh asked")
window.location.reload()
});
}
}
/**
* Checks if application timestamp has changed
* @param newTimestamp
* @returns {boolean}
*/
private isApplicationHasBeenUpdatedOnServer(newTimestamp: string): boolean {
console.debug("SoftwareUpdateService.isApplicationHasBeenUpdatedOnServer()", this.currentTimestamp, newTimestamp)
if (!this.currentTimestamp || !newTimestamp) {
console.debug("SoftwareUpdateService.isApplicationHasBeenUpdatedOnServer() : missing input : false")
return false
}
let result = this.currentTimestamp !== newTimestamp
console.debug(`SoftwareUpdateService.isApplicationHasBeenUpdatedOnServer() : ${result}`)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment