diff --git a/src/app/core/cache/object-cache.service.ts b/src/app/core/cache/object-cache.service.ts index 384df23c22..b4c37df736 100644 --- a/src/app/core/cache/object-cache.service.ts +++ b/src/app/core/cache/object-cache.service.ts @@ -65,18 +65,10 @@ export class ObjectCacheService { /** * Get an observable of the object with the specified UUID * - * The type needs to be specified as well, in order to turn - * the cached plain javascript object in to an instance of - * a class. - * - * e.g. getByUUID('c96588c6-72d3-425d-9d47-fa896255a695', Item) - * * @param uuid * The UUID of the object to get - * @param type - * The type of the object to get - * @return Observable - * An observable of the requested object + * @return Observable> + * An observable of the requested object in normalized form */ getObjectByUUID(uuid: string): Observable> { return this.store.pipe( @@ -86,6 +78,14 @@ export class ObjectCacheService { ) } + /** + * Get an observable of the object with the specified selfLink + * + * @param selfLink + * The selfLink of the object to get + * @return Observable> + * An observable of the requested object in normalized form + */ getObjectBySelfLink(selfLink: string): Observable> { return this.getBySelfLink(selfLink).pipe( map((entry: ObjectCacheEntry) => { @@ -105,6 +105,14 @@ export class ObjectCacheService { ); } + /** + * Get an observable of the object cache entry with the specified selfLink + * + * @param selfLink + * The selfLink of the object to get + * @return Observable + * An observable of the requested object cache entry + */ getBySelfLink(selfLink: string): Observable { return this.store.pipe( select(entryFromSelfLinkSelector(selfLink)), @@ -113,12 +121,28 @@ export class ObjectCacheService { ); } + /** + * Get an observable of the request's uuid with the specified selfLink + * + * @param selfLink + * The selfLink of the object to get + * @return Observable + * An observable of the request's uuid + */ getRequestUUIDBySelfLink(selfLink: string): Observable { return this.getBySelfLink(selfLink).pipe( map((entry: ObjectCacheEntry) => entry.requestUUID), distinctUntilChanged()); } + /** + * Get an observable of the request's uuid with the specified uuid + * + * @param uuid + * The uuid of the object to get + * @return Observable + * An observable of the request's uuid + */ getRequestUUIDByObjectUUID(uuid: string): Observable { return this.store.pipe( select(selfLinkFromUuidSelector(uuid)), diff --git a/src/app/core/data/request.models.ts b/src/app/core/data/request.models.ts index 951dbacff6..11ead7b53c 100644 --- a/src/app/core/data/request.models.ts +++ b/src/app/core/data/request.models.ts @@ -39,7 +39,7 @@ export abstract class RestRequest { } export class GetRequest extends RestRequest { - public responseMsToLive = 60 * 15 * 1000; + public responseMsToLive = 10000; constructor( public uuid: string, diff --git a/src/app/core/data/request.service.spec.ts b/src/app/core/data/request.service.spec.ts index 1ff55f9871..b3d9436d30 100644 --- a/src/app/core/data/request.service.spec.ts +++ b/src/app/core/data/request.service.spec.ts @@ -1,5 +1,5 @@ import { cold, getTestScheduler, hot } from 'jasmine-marbles'; -import { of as observableOf } from 'rxjs'; +import { of as observableOf, EMPTY } from 'rxjs'; import { getMockObjectCacheService } from '../../shared/mocks/mock-object-cache.service'; import { defaultUUID, getMockUUIDService } from '../../shared/mocks/mock-uuid.service'; import { ObjectCacheService } from '../cache/object-cache.service'; @@ -7,6 +7,7 @@ import { CoreState } from '../core.reducers'; import { UUIDService } from '../shared/uuid.service'; import { RequestConfigureAction, RequestExecuteAction } from './request.actions'; import * as ngrx from '@ngrx/store'; +import { ActionsSubject, Store } from '@ngrx/store'; import { DeleteRequest, GetRequest, @@ -18,11 +19,8 @@ import { RestRequest } from './request.models'; import { RequestService } from './request.service'; -import { ActionsSubject, Store } from '@ngrx/store'; import { TestScheduler } from 'rxjs/testing'; import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject'; -import { MockStore } from '../../shared/testing/mock-store'; -import { IndexState } from '../index/index.reducer'; describe('RequestService', () => { let scheduler: TestScheduler; @@ -431,7 +429,7 @@ describe('RequestService', () => { describe('when the given entry has a value, but the request is not completed', () => { let valid; - const requestEntry = { completed: false } ; + const requestEntry = { completed: false }; beforeEach(() => { spyOn(service, 'getByUUID').and.returnValue(observableOf(requestEntry)); valid = serviceAsAny.isValid(requestEntry); @@ -503,8 +501,42 @@ describe('RequestService', () => { }); it('return an observable emitting true', () => { - expect(valid).toBe(true); + expect(valid).toBe(true); }) }) - }) + }); + + describe('hasByHref', () => { + describe('when nothing is returned by getByHref', () => { + beforeEach(() => { + spyOn(service, 'getByHref').and.returnValue(EMPTY); + }); + it('hasByHref should return false', () => { + const result = service.hasByHref(''); + expect(result).toBe(false); + }); + }); + + describe('when isValid returns false', () => { + beforeEach(() => { + spyOn(service, 'getByHref').and.returnValue(observableOf(undefined)); + spyOn(service as any, 'isValid').and.returnValue(false); + }); + it('hasByHref should return false', () => { + const result = service.hasByHref(''); + expect(result).toBe(false); + }); + }); + + describe('when isValid returns true', () => { + beforeEach(() => { + spyOn(service, 'getByHref').and.returnValue(observableOf(undefined)); + spyOn(service as any, 'isValid').and.returnValue(true); + }); + it('hasByHref should return true', () => { + const result = service.hasByHref(''); + expect(result).toBe(true); + }); + }); + }); }); diff --git a/src/app/core/data/request.service.ts b/src/app/core/data/request.service.ts index 4fd7b84196..f129f8becd 100644 --- a/src/app/core/data/request.service.ts +++ b/src/app/core/data/request.service.ts @@ -80,6 +80,9 @@ export class RequestService { return `client/${this.uuidService.generate()}`; } + /** + * Check if a request is currently pending + */ isPending(request: GetRequest): boolean { // first check requests that haven't made it to the store yet if (this.requestsOnTheirWayToTheStore.includes(request.href)) { @@ -96,6 +99,9 @@ export class RequestService { return isPending; } + /** + * Retrieve a RequestEntry based on their uuid + */ getByUUID(uuid: string): Observable { return observableRace( this.store.pipe(select(this.entryFromUUIDSelector(uuid))), @@ -108,6 +114,9 @@ export class RequestService { ); } + /** + * Retrieve a RequestEntry based on their href + */ getByHref(href: string): Observable { return this.store.pipe( select(this.uuidFromHrefSelector(href)), @@ -247,7 +256,6 @@ export class RequestService { this.getByHref(href).pipe( take(1) ).subscribe((requestEntry: RequestEntry) => result = this.isValid(requestEntry)); - return result; }