2017-08-23 19:06:55 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
import {Http} from "@angular/http";
|
|
|
|
|
import {Observable} from "rxjs/Observable";
|
2017-08-23 19:18:36 +02:00
|
|
|
import {Location} from "@angular/common";
|
2017-08-23 19:06:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SelfUpdaterService {
|
|
|
|
|
|
|
|
|
|
private appRevision: number = 0;
|
|
|
|
|
private initFailed: boolean = false;
|
|
|
|
|
|
2017-08-23 19:18:36 +02:00
|
|
|
constructor(
|
|
|
|
|
private httpService: Http,
|
|
|
|
|
private locationService: Location,
|
|
|
|
|
) {
|
2017-08-23 19:06:55 +02:00
|
|
|
this.getDeployedRevision().subscribe(
|
|
|
|
|
result => this.appRevision = result,
|
|
|
|
|
() => {
|
|
|
|
|
console.log(
|
|
|
|
|
"%c Couldn't load initial revision data from server. Self update disabled.",
|
|
|
|
|
"background: #222; color: #bada55;"
|
|
|
|
|
);
|
|
|
|
|
this.initFailed = true;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getDeployedRevision(): Observable<number> {
|
2017-08-23 19:18:36 +02:00
|
|
|
return this.httpService.get(this.locationService.prepareExternalUrl("/revision.json")).map(result => result.json());
|
2017-08-23 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public checkAndReloadIfNecessary() {
|
|
|
|
|
if (!this.initFailed) {
|
|
|
|
|
this.getDeployedRevision().subscribe(
|
|
|
|
|
result => {
|
|
|
|
|
if (result > this.appRevision) {
|
|
|
|
|
document.location.reload(true);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
() => {}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|