import { provideSingleton , inject , Fetch } from '../utils/IOC' ; import { observable } from 'mobx/lib/mobx' ; import { UiStore } from './UiStore' ; import { SettingsStore } from './SettingsStore' ; declare var __VERSION__ ; interface GithubRelease { id : number ; name : string ; } @ provideSingleton ( ReleaseStore ) export class ReleaseStore { private intervalHolder : any = null ; @ inject ( UiStore ) private uiStore : UiStore ; @ inject ( SettingsStore ) private settingsStore : SettingsStore ; @ inject ( Fetch ) private fetch : typeof window . fetch ; @ observable newVersion : string ; currentVersion : string = __VERSION__ ; constructor ( ) { setTimeout ( ( ) => this . checkForUpdate ( ) , 5000 ) ; this . intervalHolder = setInterval ( ( ) => this . checkForUpdate ( ) , 60 * 50 * 1000 ) ; } async checkForUpdate ( ) { if ( ! this . settingsStore . showUpdateNotifications ) { return ; } const finishTask = this . uiStore . startAsyncTask ( ) ; let releases = [ ] ; try { releases = await ( await this . fetch ( 'https://api.github.com/repos/Mercateo/dwatch/releases' ) ) . json < Array < GithubRelease > > ( ) ; } catch ( e ) { } releases . sort ( ( a , b ) => a . id - b . id ) . reverse ( ) ; let latestRelease = releases [ 0 ] && releases [ 0 ] . name ? releases [ 0 ] . name . substr ( 1 ) : null ; if ( latestRelease && latestRelease !== this . currentVersion ) { this . newVersion = latestRelease ; if ( this . intervalHolder != null ) { clearInterval ( this . intervalHolder ) ; } } finishTask ( ) ; } }