reduce number of unnecessary requests from HalEndpointService and RemoteDataBuildService

This commit is contained in:
Art Lowel
2019-10-28 18:01:05 +01:00
parent 82367ea778
commit e26961322f
4 changed files with 24 additions and 10 deletions

View File

@@ -161,7 +161,10 @@ export class RemoteDataBuildService {
const objectList = normalized[relationship].page || normalized[relationship]; const objectList = normalized[relationship].page || normalized[relationship];
if (typeof objectList !== 'string') { if (typeof objectList !== 'string') {
objectList.forEach((href: string) => { objectList.forEach((href: string) => {
this.requestService.configure(new GetRequest(this.requestService.generateRequestId(), href)) const request = new GetRequest(this.requestService.generateRequestId(), href);
if (!this.requestService.isCachedOrPending(request)) {
this.requestService.configure(request)
}
}); });
const rdArr = []; const rdArr = [];
@@ -175,7 +178,10 @@ export class RemoteDataBuildService {
result = rdArr[0]; result = rdArr[0];
} }
} else { } else {
this.requestService.configure(new GetRequest(this.requestService.generateRequestId(), objectList)); const request = new GetRequest(this.requestService.generateRequestId(), objectList);
if (!this.requestService.isCachedOrPending(request)) {
this.requestService.configure(request)
}
// The rest API can return a single URL to represent a list of resources (e.g. /items/:id/bitstreams) // The rest API can return a single URL to represent a list of resources (e.g. /items/:id/bitstreams)
// in that case only 1 href will be stored in the normalized obj (so the isArray above fails), // in that case only 1 href will be stored in the normalized obj (so the isArray above fails),

View File

@@ -151,6 +151,8 @@ export class FindAllRequest extends GetRequest {
} }
export class EndpointMapRequest extends GetRequest { export class EndpointMapRequest extends GetRequest {
public responseMsToLive = Number.MAX_SAFE_INTEGER;
constructor( constructor(
uuid: string, uuid: string,
href: string, href: string,

View File

@@ -222,7 +222,7 @@ export class RequestService {
* @param {GetRequest} request The request to check * @param {GetRequest} request The request to check
* @returns {boolean} True if the request is cached or still pending * @returns {boolean} True if the request is cached or still pending
*/ */
private isCachedOrPending(request: GetRequest): boolean { public isCachedOrPending(request: GetRequest): boolean {
const inReqCache = this.hasByHref(request.href); const inReqCache = this.hasByHref(request.href);
const inObjCache = this.objectCache.hasBySelfLink(request.href); const inObjCache = this.objectCache.hasBySelfLink(request.href);
const isCached = inReqCache || inObjCache; const isCached = inReqCache || inObjCache;

View File

@@ -4,13 +4,14 @@ import {
map, map,
mergeMap, mergeMap,
startWith, startWith,
switchMap, switchMap, take,
tap tap
} from 'rxjs/operators'; } from 'rxjs/operators';
import { RequestEntry } from '../data/request.reducer';
import { RequestService } from '../data/request.service'; import { RequestService } from '../data/request.service';
import { GlobalConfig } from '../../../config/global-config.interface'; import { GlobalConfig } from '../../../config/global-config.interface';
import { EndpointMapRequest } from '../data/request.models'; import { EndpointMapRequest } from '../data/request.models';
import { hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util'; import { hasNoValue, hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util';
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner'; import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
import { Inject, Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { GLOBAL_CONFIG } from '../../../config'; import { GLOBAL_CONFIG } from '../../../config';
@@ -36,15 +37,19 @@ export class HALEndpointService {
private getEndpointMapAt(href): Observable<EndpointMap> { private getEndpointMapAt(href): Observable<EndpointMap> {
const request = new EndpointMapRequest(this.requestService.generateRequestId(), href); const request = new EndpointMapRequest(this.requestService.generateRequestId(), href);
if (!this.requestService.isCachedOrPending(request)) {
// don't bother configuring the request if it's already cached or pending.
this.requestService.configure(request); this.requestService.configure(request);
}
return this.requestService.getByHref(request.href).pipe( return this.requestService.getByHref(request.href).pipe(
getResponseFromEntry(), getResponseFromEntry(),
map((response: EndpointMapSuccessResponse) => response.endpointMap), map((response: EndpointMapSuccessResponse) => response.endpointMap),
); );
} }
public getEndpoint(linkPath: string): Observable<string> { public getEndpoint(linkPath: string, startHref?: string): Observable<string> {
return this.getEndpointAt(this.getRootHref(), ...linkPath.split('/')); return this.getEndpointAt(startHref || this.getRootHref(), ...linkPath.split('/')).pipe(take(1));
} }
/** /**
@@ -71,10 +76,11 @@ export class HALEndpointService {
) as Observable<string>; ) as Observable<string>;
if (halNames.length === 1) { if (halNames.length === 1) {
return nextHref$; return nextHref$.pipe(take(1));
} else { } else {
return nextHref$.pipe( return nextHref$.pipe(
switchMap((nextHref) => this.getEndpointAt(nextHref, ...halNames.slice(1))) switchMap((nextHref) => this.getEndpointAt(nextHref, ...halNames.slice(1))),
take(1)
); );
} }
} }