mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
refactor: isolate network toast and login redirect to separate services (#1412)
* refactor: isolate network toast and login redirect to separate services * chore: remove accidentally committed sketch of a service * chore: tidying things up * feat: add `GlobalModule` encapsulating all global subscription services * remove angular build cache when building deps * chore: fix more issues found while testing * chore: fix issues reported by testing * chore: fix template error * chore: fix server-info * chore: fix server-info * fix: switch to Observable to fix race conditions * fix embassy name display on load * update patchdb * clean up patch data watch Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com>
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { Inject, Injectable, InjectionToken } from '@angular/core'
|
||||
import { Inject, Injectable } from '@angular/core'
|
||||
import { Storage } from '@ionic/storage-angular'
|
||||
import { Bootstrapper, PatchDB, Source, Store } from 'patch-db-client'
|
||||
import { BehaviorSubject, Observable, of, Subscription } from 'rxjs'
|
||||
import {
|
||||
BehaviorSubject,
|
||||
Observable,
|
||||
of,
|
||||
ReplaySubject,
|
||||
Subscription,
|
||||
} from 'rxjs'
|
||||
import {
|
||||
catchError,
|
||||
debounceTime,
|
||||
filter,
|
||||
finalize,
|
||||
mergeMap,
|
||||
skip,
|
||||
switchMap,
|
||||
take,
|
||||
tap,
|
||||
@@ -17,13 +23,7 @@ import { isEmptyObject, pauseFor } from '@start9labs/shared'
|
||||
import { DataModel } from './data-model'
|
||||
import { ApiService } from '../api/embassy-api.service'
|
||||
import { AuthService } from '../auth.service'
|
||||
import { patch } from '@start9labs/emver'
|
||||
|
||||
export const PATCH_HTTP = new InjectionToken<Source<DataModel>>('')
|
||||
export const PATCH_SOURCE = new InjectionToken<Source<DataModel>>('')
|
||||
export const BOOTSTRAPPER = new InjectionToken<Bootstrapper<DataModel>>('')
|
||||
export const AUTH = new InjectionToken<AuthService>('')
|
||||
export const STORAGE = new InjectionToken<Storage>('')
|
||||
import { BOOTSTRAPPER, PATCH_SOURCE } from './patch-db.factory'
|
||||
|
||||
export enum PatchConnection {
|
||||
Initializing = 'initializing',
|
||||
@@ -36,13 +36,13 @@ export enum PatchConnection {
|
||||
})
|
||||
export class PatchDbService {
|
||||
private readonly WS_SUCCESS = 'wsSuccess'
|
||||
private patchConnection$ = new BehaviorSubject(PatchConnection.Initializing)
|
||||
private patchConnection$ = new ReplaySubject<PatchConnection>(1)
|
||||
private wsSuccess$ = new BehaviorSubject(false)
|
||||
private polling$ = new BehaviorSubject(false)
|
||||
private patchDb: PatchDB<DataModel>
|
||||
private subs: Subscription[] = []
|
||||
private sources$: BehaviorSubject<Source<DataModel>[]> = new BehaviorSubject([
|
||||
this.wsSource,
|
||||
this.sources[0],
|
||||
])
|
||||
|
||||
data: DataModel
|
||||
@@ -61,18 +61,18 @@ export class PatchDbService {
|
||||
}
|
||||
|
||||
constructor(
|
||||
@Inject(PATCH_SOURCE) private readonly wsSource: Source<DataModel>,
|
||||
@Inject(PATCH_SOURCE) private readonly pollSource: Source<DataModel>,
|
||||
@Inject(PATCH_HTTP) private readonly http: ApiService,
|
||||
// [wsSources, pollSources]
|
||||
@Inject(PATCH_SOURCE) private readonly sources: Source<DataModel>[],
|
||||
@Inject(BOOTSTRAPPER)
|
||||
private readonly bootstrapper: Bootstrapper<DataModel>,
|
||||
@Inject(AUTH) private readonly auth: AuthService,
|
||||
@Inject(STORAGE) private readonly storage: Storage,
|
||||
private readonly http: ApiService,
|
||||
private readonly auth: AuthService,
|
||||
private readonly storage: Storage,
|
||||
) {}
|
||||
|
||||
async init(): Promise<void> {
|
||||
const cache = await this.bootstrapper.init()
|
||||
this.sources$.next([this.wsSource, this.http])
|
||||
this.sources$.next([this.sources[0], this.http])
|
||||
|
||||
this.patchDb = new PatchDB(this.sources$, this.http, cache)
|
||||
|
||||
@@ -94,13 +94,13 @@ export class PatchDbService {
|
||||
console.log('patchDB: POLLING FAILED', e)
|
||||
this.patchConnection$.next(PatchConnection.Disconnected)
|
||||
await pauseFor(2000)
|
||||
this.sources$.next([this.pollSource, this.http])
|
||||
this.sources$.next([this.sources[1], this.http])
|
||||
return
|
||||
}
|
||||
|
||||
console.log('patchDB: WEBSOCKET FAILED', e)
|
||||
this.polling$.next(true)
|
||||
this.sources$.next([this.pollSource, this.http])
|
||||
this.sources$.next([this.sources[1], this.http])
|
||||
}),
|
||||
)
|
||||
.subscribe({
|
||||
@@ -152,7 +152,7 @@ export class PatchDbService {
|
||||
console.log('patchDB: SWITCHING BACK TO WEBSOCKETS')
|
||||
this.patchConnection$.next(PatchConnection.Initializing)
|
||||
this.polling$.next(false)
|
||||
this.sources$.next([this.wsSource, this.http])
|
||||
this.sources$.next([this.sources[0], this.http])
|
||||
}
|
||||
}),
|
||||
)
|
||||
@@ -180,19 +180,14 @@ export class PatchDbService {
|
||||
|
||||
// prettier-ignore
|
||||
watch$: Store<DataModel>['watch$'] = (...args: (string | number)[]): Observable<DataModel> => {
|
||||
// TODO: refactor with a better solution to race condition
|
||||
const argsString = '/' + args.join('/')
|
||||
const source$ =
|
||||
this.patchDb?.store.watch$(...(args as [])) ||
|
||||
this.patchConnection$.pipe(
|
||||
skip(1),
|
||||
take(1),
|
||||
switchMap(() => this.patchDb.store.watch$(...(args as []))),
|
||||
)
|
||||
|
||||
console.log('patchDB: WATCHING ', argsString)
|
||||
|
||||
return source$.pipe(
|
||||
return this.patchConnection$.pipe(
|
||||
filter(status => status === PatchConnection.Connected),
|
||||
take(1),
|
||||
switchMap(() => this.patchDb.store.watch$(...(args as []))),
|
||||
tap(data => console.log('patchDB: NEW VALUE', argsString, data)),
|
||||
catchError(e => {
|
||||
console.error('patchDB: WATCH ERROR', e)
|
||||
|
||||
Reference in New Issue
Block a user