Revert domain store service (#392)

This commit is contained in:
Rinat Arsaev 2024-09-23 14:52:49 +05:00 committed by GitHub
parent 8da6c5ab04
commit 0f2441a130
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 40 deletions

View File

@ -1,27 +1,23 @@
import { Injectable, DestroyRef } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DomainObject, Reference } from '@vality/domain-proto/domain';
import { Domain, DomainObject, Reference } from '@vality/domain-proto/domain';
import { Commit, Snapshot, Version } from '@vality/domain-proto/domain_config';
import { NotifyLogService, handleError, inProgressFrom, progressTo } from '@vality/ng-core';
import { getUnionKey } from '@vality/ng-thrift';
import isEqual from 'lodash-es/isEqual';
import { BehaviorSubject, defer, Observable, of, ReplaySubject, filter, combineLatest } from 'rxjs';
import { map, shareReplay, startWith, switchMap, take, tap } from 'rxjs/operators';
import { DomainSecretService } from '../../../shared/services';
import { RepositoryService } from '../repository.service';
import { createObjectHash } from '../utils/create-object-hash';
@Injectable({
providedIn: 'root',
})
export class DomainStoreService {
domain$ = defer(() => this.rawDomain$).pipe(
map((d) => this.domainSecretService.reduceDomain(d)),
shareReplay({ refCount: true, bufferSize: 1 }),
);
version$ = defer(() => this.loadedSnapshot$).pipe(
version$ = combineLatest([defer(() => this.snapshot$), defer(() => this.progress$)]).pipe(
filter(([, p]) => !p),
map(([s]) => s.version),
shareReplay({ refCount: true, bufferSize: 1 }),
);
isLoading$ = inProgressFrom(
() => this.progress$,
@ -38,31 +34,8 @@ export class DomainStoreService {
takeUntilDestroyed(this.destroyRef),
shareReplay(1),
);
private loadedSnapshot$ = combineLatest([
defer(() => this.snapshot$),
defer(() => this.progress$),
]).pipe(
filter(([, p]) => !p),
shareReplay({ refCount: true, bufferSize: 1 }),
);
private reload$ = new ReplaySubject<void>(1);
private progress$ = new BehaviorSubject(0);
private rawDomain$ = this.loadedSnapshot$.pipe(
map(([s]) => s?.domain),
shareReplay({ refCount: true, bufferSize: 1 }),
);
private objects$ = combineLatest([this.rawDomain$, this.domain$]).pipe(
map(
([rawDomain, domain]) =>
new Map(
Array.from(rawDomain).map(([ref, raw]) => [
createObjectHash(ref),
{ raw, reduced: domain.get(ref) },
]),
),
),
shareReplay({ refCount: true, bufferSize: 1 }),
);
constructor(
private repositoryService: RepositoryService,
@ -75,9 +48,17 @@ export class DomainStoreService {
this.reload$.next();
}
getDomain(raw = false): Observable<Domain> {
return combineLatest([defer(() => this.snapshot$), defer(() => this.progress$)]).pipe(
filter(([, p]) => !p),
map(([s]) => s?.domain),
map((d) => (raw ? d : this.domainSecretService.reduceDomain(d))),
);
}
getObject(ref: Reference, raw = false): Observable<DomainObject> {
return this.objects$.pipe(
map((objects) => objects.get(createObjectHash(ref))?.[raw ? 'raw' : 'reduced']),
return this.getDomain(raw).pipe(
map((domain) => Array.from(domain).find(([r]) => isEqual(ref, r))?.[1]),
);
}
@ -88,7 +69,7 @@ export class DomainStoreService {
getObjectsRefs<T extends keyof DomainObject>(
objectType: T,
): Observable<[Reference, DomainObject][]> {
return this.domain$.pipe(
return this.getDomain().pipe(
map((d) => Array.from(d).filter(([, o]) => getUnionKey(o) === objectType)),
);
}

View File

@ -92,10 +92,8 @@
Change status
</button>
<button mat-raised-button (click)="createWallet()">Add wallet modification</button>
<button mat-raised-button (click)="createShop(false)">Add shop modifications</button>
<button color="accent" mat-raised-button (click)="createShop(true)">
Add shop modifications without payout
</button>
<!-- <button mat-raised-button (click)="createShop(false)">Add shop modifications</button>-->
<button mat-raised-button (click)="createShop(true)">Add shop modifications</button>
<button color="primary" mat-raised-button (click)="addModification()">
Add modification
</button>

View File

@ -59,7 +59,7 @@ export class DomainObjectsTableComponent implements OnInit {
(this.qp.params.types as (keyof DomainObject)[]) || [],
);
objects$: Observable<DomainObjectData[]> = combineLatest([
this.domainStoreService.domain$,
this.domainStoreService.getDomain(),
this.typesControl.valueChanges.pipe(startWith(this.typesControl.value)),
]).pipe(
switchMap(([objects, types]) =>

View File

@ -34,7 +34,7 @@ export class DomainObjectCardComponent implements OnChanges {
ref$ = new ReplaySubject<Reference>(1);
progress$ = this.domainStoreService.isLoading$;
domainObject$ = combineLatest([this.domainStoreService.domain$, this.ref$]).pipe(
domainObject$ = combineLatest([this.domainStoreService.getDomain(), this.ref$]).pipe(
map(([domain, ref]) =>
domain.get(Array.from(domain.keys()).find((k) => isEqualThrift(k, ref))),
),