ui: factors auto checks

This commit is contained in:
Aaron Greenspan
2021-01-19 13:53:51 -07:00
committed by Aiden McClelland
parent b717853759
commit b7821576bb
7 changed files with 187 additions and 94 deletions

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core'
import { NavController } from '@ionic/angular'
import { BehaviorSubject, forkJoin, Observable, of } from 'rxjs'
import { catchError, map, take, tap } from 'rxjs/operators'
import { BehaviorSubject, combineLatest, forkJoin, interval, NextObserver, Observable, Observer, of } from 'rxjs'
import { catchError, concatMap, distinctUntilChanged, filter, map, take, tap } from 'rxjs/operators'
import { ServerModel, ServerStatus } from '../models/server-model'
import { ApiService } from './api/api.service'
import { Emver } from './emver.service'
@@ -10,26 +10,52 @@ import { Emver } from './emver.service'
// call checkForUpdates in marketplace pages, can subscribe globally however
@Injectable({ providedIn: 'root' })
export class OsUpdateService {
private readonly $updateAvailable$ = new BehaviorSubject(undefined)
// holds version latest if update available, undefined if not.
private readonly $updateAvailable$ = new BehaviorSubject<string>(undefined)
// same as above, but we only update this as a result of auto check
// this is because we only pop update alert when it comes from an auto check, not the checkForUpdates() call
private readonly $updateAvailableFromAutoCheck$ = new BehaviorSubject<string>(undefined)
watchForUpdateAvailable$ (): Observable<undefined | string> {
return this.$updateAvailable$.asObservable().pipe(distinctUntilChanged())
}
watchForAutoCheckUpdateAvailable$ (): Observable<undefined | string> {
return this.$updateAvailableFromAutoCheck$.asObservable().pipe(distinctUntilChanged())
}
constructor (
private readonly emver: Emver,
private readonly serverModel: ServerModel,
private readonly apiService: ApiService,
private readonly navCtrl: NavController,
) { }
) {
// watch auto check flag and versionLatest for possible update
this.autoCheck$().subscribe(this.$updateAvailableFromAutoCheck$)
watchForUpdateAvailable (): Observable<undefined | string> {
return this.$updateAvailable$.asObservable()
// if update is available from auto check, then it's available (not vice versa)
this.$updateAvailableFromAutoCheck$.subscribe(this.$updateAvailable$)
}
// undefined when no update available, string for the versionLatest if there is
private autoCheck$ (): Observable<string> {
const { autoCheckUpdates } = this.serverModel.watch()
return combineLatest([autoCheckUpdates, interval(5000)]).pipe(
filter( ([check, _]) => check),
concatMap(() => this.apiService.getVersionLatest()),
filter( ({ canUpdate }) => canUpdate),
map(({ versionLatest }) => versionLatest),
)
}
// can call this imperatively and take the return value as gospel, or watch the $updateAvailable$ subject for the same info.
checkForUpdates (): Promise<undefined | string> {
return forkJoin([
this.apiService.getVersionLatest(),
this.serverModel.watch().versionInstalled.pipe(take(1)),
this.apiService.getVersionLatest(),
]).pipe(
map(([vl, vi]) => this.emver.compare(vi, vl.versionLatest) === -1 ? vl.versionLatest : undefined),
map(([vi, vl]) => updateIsAvailable(this.emver, vi, vl.versionLatest)),
catchError(e => {
console.error(`OsUpdateService Error: ${e}`)
return of(undefined)
@@ -39,10 +65,20 @@ export class OsUpdateService {
).toPromise()
}
async checkForAppsUpdate (): Promise<boolean> {
const availableApps = await this.apiService.getAvailableApps()
return !!availableApps.find(app => this.emver.compare(app.versionInstalled, app.versionLatest) === -1)
}
async updateEmbassyOS (versionLatest: string): Promise<void> {
await this.apiService.updateAgent(versionLatest)
this.serverModel.update({ status: ServerStatus.UPDATING })
this.$updateAvailable$.next(undefined)
await this.navCtrl.navigateRoot('/embassy')
}
}
function updateIsAvailable (e: Emver, vi: string, vl: string): string | undefined {
if (!vi || !vl) return undefined
return e.compare(vi, vl) === -1 ? vl : undefined
}