Used shareReplay instead cache service (#44)

This commit is contained in:
Ildar Galeev 2019-01-25 14:58:58 +03:00 committed by Alexandra Usacheva
parent 38bcf89fb6
commit 4f92507385
3 changed files with 6 additions and 42 deletions

View File

@ -1,29 +0,0 @@
import { Injectable } from '@angular/core';
import { Subject, Observable, BehaviorSubject } from 'rxjs';
import { DomainService } from './domain.service';
import { toGenReference } from './converters';
import { Snapshot } from '../gen-damsel/domain_config';
@Injectable()
export class CheckoutCacheService {
private cache: Snapshot;
private isLoading = false;
private snapshot$: Subject<Snapshot> = new BehaviorSubject(null);
constructor(private dmtService: DomainService) {}
checkout(): Observable<Snapshot> {
if (this.cache) {
this.snapshot$.next(this.cache);
} else if (!this.isLoading) {
this.isLoading = true;
this.dmtService.checkout(toGenReference()).subscribe(s => {
this.cache = s;
this.isLoading = false;
this.snapshot$.next(s);
});
}
return this.snapshot$;
}
}

View File

@ -1,7 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/internal/operators';
import { filter } from 'rxjs/operators';
import { shareReplay, map, switchMap } from 'rxjs/operators';
import {
Domain,
@ -16,7 +15,6 @@ import { findDomainObject, findDomainObjects } from '../claim/domain-typed-manag
import { createShopTerminal } from '../claim/domain-typed-manager/create-shop-terminal';
import { toGenReference } from './converters';
import { DomainService } from './domain.service';
import { CheckoutCacheService } from './checkout-cache.service';
const findBusinessScheduleObjects = (domain: Domain): BusinessScheduleObject[] =>
findDomainObjects(domain, 'business_schedule');
@ -49,13 +47,10 @@ const filterByTerminalSelector = (
export class DomainTypedManager {
private domain: Observable<Domain>;
constructor(
private dmtService: DomainService,
private checkoutCacheService: CheckoutCacheService
) {
this.domain = this.checkoutCacheService.checkout().pipe(
filter(s => s !== null),
map(s => s.domain)
constructor(private dmtService: DomainService) {
this.domain = this.dmtService.checkout(toGenReference()).pipe(
map(s => s.domain),
shareReplay(1)
);
}

View File

@ -4,15 +4,13 @@ import { DomainService } from './domain.service';
import { PaymentProcessingService } from './payment-processing.service';
import { MerchantStatisticsService } from './merchant-statistics.service';
import { DomainTypedManager } from './domain-typed-manager';
import { CheckoutCacheService } from './checkout-cache.service';
@NgModule({
providers: [
DomainService,
DomainTypedManager,
PaymentProcessingService,
MerchantStatisticsService,
CheckoutCacheService
MerchantStatisticsService
]
})
export class ThriftModule {}