import {Injectable} from '@angular/core'; import {Http} from "@angular/http"; import {Observable} from "rxjs/Observable"; import {Location} from "@angular/common"; @Injectable() export class SelfUpdaterService { private appRevision: number = 0; private initFailed: boolean = false; constructor( private httpService: Http, private locationService: Location, ) { 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 { return this.httpService.get(this.locationService.prepareExternalUrl("/revision.json")).map(result => result.json()); } public checkAndReloadIfNecessary() { if (!this.initFailed) { this.getDeployedRevision().subscribe( result => { if (result > this.appRevision) { document.location.reload(true); } }, () => {} ); } } }