fix unsubscribes on logout, better connection monitoring, and better message for lost connection

This commit is contained in:
Matt Hill
2021-08-23 15:06:24 -06:00
committed by Aiden McClelland
parent 056452faca
commit 6e82ef48ca
10 changed files with 115 additions and 97 deletions

View File

@@ -2,9 +2,9 @@ import { Injectable } from '@angular/core'
import { BehaviorSubject, combineLatest, fromEvent, merge, Subscription } from 'rxjs'
import { PatchConnection, PatchDbService } from './patch-db/patch-db.service'
import { HttpService, Method } from './http.service'
import { distinctUntilChanged, takeWhile } from 'rxjs/operators'
import { distinctUntilChanged } from 'rxjs/operators'
import { ConfigService } from './config.service'
import { AuthState } from './auth.service'
import { pauseFor } from '../util/misc.util'
@Injectable({
providedIn: 'root',
@@ -23,16 +23,13 @@ export class ConnectionService {
return this.connectionFailure$.asObservable()
}
start (auth: AuthState) {
merge(fromEvent(window, 'online'), fromEvent(window, 'offline'))
.pipe(
takeWhile(() => auth === AuthState.VERIFIED),
)
start (): Subscription[] {
const sub1 = merge(fromEvent(window, 'online'), fromEvent(window, 'offline'))
.subscribe(event => {
this.networkState$.next(event.type === 'online')
}),
})
combineLatest([
const sub2 = combineLatest([
// 1
this.networkState$
.pipe(
@@ -46,7 +43,6 @@ export class ConnectionService {
// 3
this.patch.watch$('server-info', 'connection-addresses')
.pipe(
takeWhile(() => auth === AuthState.VERIFIED),
distinctUntilChanged(),
),
])
@@ -76,6 +72,7 @@ export class ConnectionService {
}
}
})
return [sub1, sub2]
}
private async testAddrs (addrs: string[]): Promise<boolean> {

View File

@@ -2,6 +2,7 @@ import { Inject, Injectable, InjectionToken } from '@angular/core'
import { Bootstrapper, PatchDB, Source, Store } from 'patch-db-client'
import { BehaviorSubject, Observable, of, Subscription } from 'rxjs'
import { catchError, debounceTime, finalize, map, tap } from 'rxjs/operators'
import { pauseFor } from 'src/app/util/misc.util'
import { ApiService } from '../api/embassy-api.service'
import { DataModel } from './data-model'
@@ -40,7 +41,11 @@ export class PatchDbService {
start (): void {
// make sure everything is stopped before initializing
this.stop()
if (this.patchSub) {
this.patchSub.unsubscribe()
this.patchSub = undefined
}
console.log('Retrying')
try {
this.patchSub = this.patchDb.sync$()
.pipe(debounceTime(500))
@@ -49,10 +54,12 @@ export class PatchDbService {
this.patchConnection$.next(PatchConnection.Connected)
this.bootstrapper.update(cache)
},
error: e => {
error: async e => {
console.error('patch-db-sync sub ERROR', e)
this.patchConnection$.next(PatchConnection.Disconnected)
// this.start()
console.log('Erroring out')
await pauseFor(4000)
this.start()
},
complete: () => {
console.warn('patch-db-sync sub COMPLETE')
@@ -64,7 +71,9 @@ export class PatchDbService {
}
stop (): void {
console.log('STOPPING PATCH DB')
this.patchConnection$.next(PatchConnection.Initializing)
this.patchDb.store.reset()
if (this.patchSub) {
this.patchSub.unsubscribe()
this.patchSub = undefined

View File

@@ -13,6 +13,7 @@ import { PatchDbService } from './patch-db/patch-db.service'
import { filter, take } from 'rxjs/operators'
import { isEmptyObject } from '../util/misc.util'
import { ApiService } from './api/embassy-api.service'
import { Subscription } from 'rxjs'
@Injectable({
providedIn: 'root',
@@ -60,8 +61,8 @@ export class StartupAlertsService {
// Then, the reduce fires, quickly iterating through yielding a promise (previousDisplay) to the next element
// Each promise fires more or less concurrently, so each c.check(server) is run concurrently
// Then, since we await previousDisplay before c.display(res), each promise executing gets hung awaiting the display of the previous run
async runChecks (): Promise<void> {
this.patch.watch$()
runChecks (): Subscription {
return this.patch.watch$()
.pipe(
filter(data => !isEmptyObject(data)),
take(1),