refactor(patch-db): use PatchDB class declaratively (#1562)

* refactor(patch-db): use PatchDB class declaratively

* chore: remove initial source before init

* chore: show spinner

* fix: show Connecting to Embassy spinner until first connection

* fix: switching marketplaces

* allow for subscription to end with take when installing a package

* update patchdb

Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com>
This commit is contained in:
Alex Inkin
2022-06-23 01:09:14 +03:00
committed by GitHub
parent a8749f574a
commit 53ca9b0420
19 changed files with 142 additions and 105 deletions

View File

@@ -14,16 +14,17 @@ import {
filter,
finalize,
mergeMap,
shareReplay,
switchMap,
take,
tap,
withLatestFrom,
} from 'rxjs/operators'
import { isEmptyObject, pauseFor } from '@start9labs/shared'
import { pauseFor } from '@start9labs/shared'
import { DataModel } from './data-model'
import { ApiService } from '../api/embassy-api.service'
import { AuthService } from '../auth.service'
import { BOOTSTRAPPER, PATCH_SOURCE } from './patch-db.factory'
import { BOOTSTRAPPER, PATCH_SOURCE, PATCH_SOURCE$ } from './patch-db.factory'
export enum PatchConnection {
Initializing = 'initializing',
@@ -39,49 +40,40 @@ export class PatchDbService {
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.sources[0],
])
data: DataModel
readonly connected$ = this.watchPatchConnection$().pipe(
filter(status => status === PatchConnection.Connected),
take(1),
shareReplay(),
)
errors = 0
getData() {
return this.patchDb.store.cache.data
}
// TODO: Refactor to use `Observable` so that we can react to PatchDb becoming loaded
get loaded(): boolean {
return (
this.patchDb?.store?.cache?.data &&
!isEmptyObject(this.patchDb.store.cache.data)
)
}
constructor(
// [wsSources, pollSources]
@Inject(PATCH_SOURCE) private readonly sources: Source<DataModel>[],
@Inject(BOOTSTRAPPER)
private readonly bootstrapper: Bootstrapper<DataModel>,
@Inject(PATCH_SOURCE$)
private readonly sources$: BehaviorSubject<Source<DataModel>[]>,
private readonly http: ApiService,
private readonly auth: AuthService,
private readonly storage: Storage,
private readonly patchDb: PatchDB<DataModel>,
) {}
async init(): Promise<void> {
const cache = await this.bootstrapper.init()
init() {
this.sources$.next([this.sources[0], this.http])
this.patchDb = new PatchDB(this.sources$, this.http, cache)
this.patchConnection$.next(PatchConnection.Initializing)
this.data = this.patchDb.store.cache.data
}
async start(): Promise<void> {
await this.init()
this.init()
this.subs.push(
// Connection Error
@@ -165,11 +157,9 @@ export class PatchDbService {
}
stop(): void {
if (this.patchDb) {
console.log('patchDB: STOPPING')
this.patchConnection$.next(PatchConnection.Initializing)
this.patchDb.store.reset()
}
console.log('patchDB: STOPPING')
this.patchConnection$.next(PatchConnection.Initializing)
this.patchDb.store.reset()
this.subs.forEach(x => x.unsubscribe())
this.subs = []
}