diff --git a/src/app/core/data/request.service.spec.ts b/src/app/core/data/request.service.spec.ts index 017721fdf9..cfb3611fc6 100644 --- a/src/app/core/data/request.service.spec.ts +++ b/src/app/core/data/request.service.spec.ts @@ -22,6 +22,7 @@ import { } from './request.models'; import { RequestEntry } from './request.reducer'; import { RequestService } from './request.service'; +import { parseJsonSchemaToCommandDescription } from '@angular/cli/utilities/json-schema'; describe('RequestService', () => { let scheduler: TestScheduler; @@ -139,13 +140,21 @@ describe('RequestService', () => { describe('getByUUID', () => { describe('if the request with the specified UUID exists in the store', () => { beforeEach(() => { + let callCounter = 0; + const responses = [ + cold('a', { // A direct hit in the request cache + a: { + completed: true + } + }), + cold('b', { b: undefined }), // No hit in the index + cold('c', { c: undefined }) // So no mapped hit in the request cache + ]; selectSpy.and.callFake(() => { return () => { - return () => hot('a', { - a: { - completed: true - } - }); + const response = responses[callCounter]; + callCounter++; + return () => response; }; }); }); @@ -162,11 +171,19 @@ describe('RequestService', () => { }); }); - describe('if the request with the specified UUID doesn\'t exist in the store', () => { + describe(`if the request with the specified UUID doesn't exist in the store `, () => { beforeEach(() => { + let callCounter = 0; + const responses = [ + cold('a', { a: undefined }), // No direct hit in the request cache + cold('b', { b: undefined }), // No hit in the index + cold('c', { c: undefined }), // So no mapped hit in the request cache + ]; selectSpy.and.callFake(() => { return () => { - return () => hot('a', { a: undefined }); + const response = responses[callCounter]; + callCounter++; + return () => response; }; }); }); @@ -174,11 +191,43 @@ describe('RequestService', () => { it('should return an Observable of undefined', () => { const result = service.getByUUID(testUUID); - scheduler.expectObservable(result).toBe('b', { b: undefined }); + scheduler.expectObservable(result).toBe('a', { a: undefined }); }); }); - }); + describe(`if the request with the specified UUID wasn't sent, because it was already cached`, () => { + beforeEach(() => { + let callCounter = 0; + const responses = [ + cold('a', { a: undefined }), // No direct hit in the request cache with that UUID + cold('b', { b: 'otherRequestUUID' }), // A hit in the index, which returns the uuid of the cached request + cold('c', { // the call to retrieve the cached request using the UUID from the index + c: { + completed: true + } + }) + ]; + selectSpy.and.callFake(() => { + return () => { + const response = responses[callCounter]; + callCounter++; + return () => response; + }; + }); + }); + + it(`it should return the cached request`, () => { + const result = service.getByUUID(testUUID); + + scheduler.expectObservable(result).toBe('c', { + c: { + completed: true + } + }); + }); + }); + + }); describe('getByHref', () => { describe('when the request with the specified href exists in the store', () => { diff --git a/src/app/core/data/request.service.ts b/src/app/core/data/request.service.ts index 810b0721ae..105d84cf4a 100644 --- a/src/app/core/data/request.service.ts +++ b/src/app/core/data/request.service.ts @@ -2,10 +2,10 @@ import { Injectable } from '@angular/core'; import { HttpHeaders } from '@angular/common/http'; import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store'; -import { Observable, race as observableRace } from 'rxjs'; -import { filter, map, mergeMap, take } from 'rxjs/operators'; +import { Observable, combineLatest as observableCombineLatest } from 'rxjs'; +import { filter, map, mergeMap, take, switchMap, startWith } from 'rxjs/operators'; import { cloneDeep, remove } from 'lodash'; -import { hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util'; +import { hasValue, isEmpty, isNotEmpty, hasValueOperator } from '../../shared/empty.util'; import { CacheableObject } from '../cache/object-cache.reducer'; import { ObjectCacheService } from '../cache/object-cache.service'; import { CoreState } from '../core.reducers'; @@ -110,15 +110,19 @@ export class RequestService { * Retrieve a RequestEntry based on their uuid */ getByUUID(uuid: string): Observable { - return observableRace( - this.store.pipe(select(entryFromUUIDSelector(uuid))), + return observableCombineLatest([ + this.store.pipe( + select(entryFromUUIDSelector(uuid)) + ), this.store.pipe( select(originalRequestUUIDFromRequestUUIDSelector(uuid)), - mergeMap((originalUUID) => { - return this.store.pipe(select(entryFromUUIDSelector(originalUUID))) + switchMap((originalUUID) => { + return this.store.pipe(select(entryFromUUIDSelector(originalUUID))) }, - )) - ).pipe( + ), + ), + ]).pipe( + map((entries: RequestEntry[]) => entries.find((entry: RequestEntry) => hasValue(entry))), map((entry: RequestEntry) => { // Headers break after being retrieved from the store (because of lazy initialization) // Combining them with a new object fixes this issue @@ -137,7 +141,13 @@ export class RequestService { getByHref(href: string): Observable { return this.store.pipe( select(uuidFromHrefSelector(href)), - mergeMap((uuid: string) => this.getByUUID(uuid)) + mergeMap((uuid: string) => { + if (isNotEmpty(uuid)) { + return this.getByUUID(uuid); + } else { + return [undefined]; + } + }) ); }