ui: remove interval checking

This commit is contained in:
Aaron Greenspan
2021-01-19 14:25:42 -07:00
committed by Aiden McClelland
parent b7821576bb
commit e74ab3ce26
2 changed files with 12 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ import { NavController } from '@ionic/angular'
import { BehaviorSubject, combineLatest, forkJoin, interval, NextObserver, Observable, Observer, of } from 'rxjs' import { BehaviorSubject, combineLatest, forkJoin, interval, NextObserver, Observable, Observer, of } from 'rxjs'
import { catchError, concatMap, distinctUntilChanged, filter, map, take, tap } from 'rxjs/operators' import { catchError, concatMap, distinctUntilChanged, filter, map, take, tap } from 'rxjs/operators'
import { ServerModel, ServerStatus } from '../models/server-model' import { ServerModel, ServerStatus } from '../models/server-model'
import { exists } from '../util/misc.util'
import { ApiService } from './api/api.service' import { ApiService } from './api/api.service'
import { Emver } from './emver.service' import { Emver } from './emver.service'
@@ -13,39 +14,26 @@ export class OsUpdateService {
// holds version latest if update available, undefined if not. // holds version latest if update available, undefined if not.
private readonly $updateAvailable$ = new BehaviorSubject<string>(undefined) 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> { watchForUpdateAvailable$ (): Observable<undefined | string> {
return this.$updateAvailable$.asObservable().pipe(distinctUntilChanged()) return this.$updateAvailable$.asObservable().pipe(distinctUntilChanged())
} }
watchForAutoCheckUpdateAvailable$ (): Observable<undefined | string> {
return this.$updateAvailableFromAutoCheck$.asObservable().pipe(distinctUntilChanged())
}
constructor ( constructor (
private readonly emver: Emver, private readonly emver: Emver,
private readonly serverModel: ServerModel, private readonly serverModel: ServerModel,
private readonly apiService: ApiService, private readonly apiService: ApiService,
private readonly navCtrl: NavController, private readonly navCtrl: NavController,
) { ) {
// watch auto check flag and versionLatest for possible update
this.autoCheck$().subscribe(this.$updateAvailableFromAutoCheck$)
// if update is available from auto check, then it's available (not vice versa)
this.$updateAvailableFromAutoCheck$.subscribe(this.$updateAvailable$)
} }
// emits everytime autoCheckUpdates becomes (or is) true
private autoCheck$ (): Observable<string> { autoCheck$ (): Observable<string> {
const { autoCheckUpdates } = this.serverModel.watch() return this.serverModel.watch().autoCheckUpdates.pipe(
return combineLatest([autoCheckUpdates, interval(5000)]).pipe( distinctUntilChanged(),
filter( ([check, _]) => check), filter(check => check),
concatMap(() => this.apiService.getVersionLatest()), concatMap(() => this.apiService.getVersionLatest()),
filter( ({ canUpdate }) => canUpdate), map(({ canUpdate, versionLatest }) => canUpdate ? versionLatest : undefined),
map(({ versionLatest }) => versionLatest), tap(vl => this.$updateAvailable$.next(vl)),
) )
} }
@@ -61,7 +49,7 @@ export class OsUpdateService {
return of(undefined) return of(undefined)
}), }),
// cache the result for components to learn update available without having to have called this method // cache the result for components to learn update available without having to have called this method
tap(updateAvailable => this.$updateAvailable$.next(updateAvailable)), tap(vl => this.$updateAvailable$.next(vl)),
).toPromise() ).toPromise()
} }

View File

@@ -35,7 +35,7 @@ export class GlobalAlertsNotifier {
const { welcomeAck, versionInstalled } = this.server.watch() const { welcomeAck, versionInstalled } = this.server.watch()
return combineLatest([ welcomeAck, versionInstalled ]).pipe( return combineLatest([ welcomeAck, versionInstalled ]).pipe(
filter( ([wa, vi]) => !!vi && !wa), filter( ([wa, vi]) => vi && !wa),
take(1), // we will check and show welcome message at most once per app instance take(1), // we will check and show welcome message at most once per app instance
concatMap(([_, vi]) => iif( concatMap(([_, vi]) => iif(
() => vi === this.config.version, () => vi === this.config.version,
@@ -47,9 +47,9 @@ export class GlobalAlertsNotifier {
private autoUpdateCheck$ (): Observable<void> { private autoUpdateCheck$ (): Observable<void> {
// this emits iff autoCheck is on and update available // this emits iff autoCheck is on and update available
return this.osUpdateService.watchForAutoCheckUpdateAvailable$().pipe( return this.osUpdateService.autoCheck$().pipe(
filter(exists), filter(exists),
concatMap(async (vl) => { concatMap(async vl => {
const { update } = await this.presentAlertNewOS(vl) const { update } = await this.presentAlertNewOS(vl)
if (update) { if (update) {
return this.loader.displayDuringP( return this.loader.displayDuringP(