Created MyDSpaceRequest to avoid use of forceBypassCache flag

This commit is contained in:
Giuseppe Digilio
2019-04-19 17:40:51 +02:00
parent 4b7b61d0c2
commit ca7e8798a0
3 changed files with 31 additions and 22 deletions

View File

@@ -20,6 +20,7 @@ import { RoleType } from '../core/roles/role-types';
import { SearchConfigurationService } from '../+search-page/search-service/search-configuration.service'; import { SearchConfigurationService } from '../+search-page/search-service/search-configuration.service';
import { MyDSpaceConfigurationService } from './my-dspace-configuration.service'; import { MyDSpaceConfigurationService } from './my-dspace-configuration.service';
import { ViewMode } from '../core/shared/view-mode.model'; import { ViewMode } from '../core/shared/view-mode.model';
import { MyDSpaceRequest } from '../core/data/request.models';
export const MYDSPACE_ROUTE = '/mydspace'; export const MYDSPACE_ROUTE = '/mydspace';
export const SEARCH_CONFIG_SERVICE: InjectionToken<SearchConfigurationService> = new InjectionToken<SearchConfigurationService>('searchConfigurationService'); export const SEARCH_CONFIG_SERVICE: InjectionToken<SearchConfigurationService> = new InjectionToken<SearchConfigurationService>('searchConfigurationService');
@@ -87,7 +88,7 @@ export class MyDSpacePageComponent implements OnInit {
private windowService: HostWindowService, private windowService: HostWindowService,
@Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: MyDSpaceConfigurationService) { @Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: MyDSpaceConfigurationService) {
this.isXsOrSm$ = this.windowService.isXsOrSm(); this.isXsOrSm$ = this.windowService.isXsOrSm();
this.service.setServiceOptions(MyDSpaceResponseParsingService, true); this.service.setServiceOptions(MyDSpaceResponseParsingService, MyDSpaceRequest);
} }
/** /**

View File

@@ -1,6 +1,6 @@
import { combineLatest as observableCombineLatest, Observable, of as observableOf } from 'rxjs'; import { combineLatest as observableCombineLatest, Observable, of as observableOf } from 'rxjs';
import { Injectable, OnDestroy } from '@angular/core'; import { Injectable, OnDestroy } from '@angular/core';
import { ActivatedRoute, NavigationExtras, PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router'; import { NavigationExtras, PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router';
import { first, map, switchMap } from 'rxjs/operators'; import { first, map, switchMap } from 'rxjs/operators';
import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service';
import { import {
@@ -17,7 +17,8 @@ import { DSpaceObject } from '../../core/shared/dspace-object.model';
import { GenericConstructor } from '../../core/shared/generic-constructor'; import { GenericConstructor } from '../../core/shared/generic-constructor';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
import { import {
configureRequest, filterSuccessfulResponses, configureRequest,
filterSuccessfulResponses,
getResponseFromEntry, getResponseFromEntry,
getSucceededRemoteData getSucceededRemoteData
} from '../../core/shared/operators'; } from '../../core/shared/operators';
@@ -58,16 +59,16 @@ export class SearchService implements OnDestroy {
*/ */
private facetLinkPathPrefix = 'discover/facets/'; private facetLinkPathPrefix = 'discover/facets/';
/**
* When true, a new search request is always dispatched
*/
private forceBypassCache = false;
/** /**
* The ResponseParsingService constructor name * The ResponseParsingService constructor name
*/ */
private parser: GenericConstructor<ResponseParsingService> = SearchResponseParsingService; private parser: GenericConstructor<ResponseParsingService> = SearchResponseParsingService;
/**
* The RestRequest constructor name
*/
private request: GenericConstructor<RestRequest> = GetRequest;
/** /**
* Subscription to unsubscribe from * Subscription to unsubscribe from
*/ */
@@ -85,15 +86,16 @@ export class SearchService implements OnDestroy {
/** /**
* Method to set service options * Method to set service options
* @param {GenericConstructor<ResponseParsingService>} parser The configuration necessary to perform this search * @param {GenericConstructor<ResponseParsingService>} parser The ResponseParsingService constructor name
* @param {boolean} forceBypassCache When true, a new search request is always dispatched * @param {boolean} request The RestRequest constructor name
* @returns {Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>>} Emits a paginated list with all search results found
*/ */
setServiceOptions(parser: GenericConstructor<ResponseParsingService>, forceBypassCache: boolean) { setServiceOptions(parser: GenericConstructor<ResponseParsingService>, request: GenericConstructor<RestRequest>) {
if (parser) { if (parser) {
this.parser = parser; this.parser = parser;
} }
this.forceBypassCache = forceBypassCache; if (request) {
this.request = request;
}
} }
/** /**
@@ -107,7 +109,8 @@ export class SearchService implements OnDestroy {
if (hasValue(searchOptions)) { if (hasValue(searchOptions)) {
url = (searchOptions as PaginatedSearchOptions).toRestUrl(url); url = (searchOptions as PaginatedSearchOptions).toRestUrl(url);
} }
const request = new GetRequest(this.requestService.generateRequestId(), url); const request = new this.request(this.requestService.generateRequestId(), url);
const getResponseParserFn: () => GenericConstructor<ResponseParsingService> = () => { const getResponseParserFn: () => GenericConstructor<ResponseParsingService> = () => {
return this.parser; return this.parser;
}; };
@@ -116,7 +119,7 @@ export class SearchService implements OnDestroy {
getResponseParser: getResponseParserFn getResponseParser: getResponseParserFn
}); });
}), }),
configureRequest(this.requestService, this.forceBypassCache), configureRequest(this.requestService),
); );
const requestEntryObs = requestObs.pipe( const requestEntryObs = requestObs.pipe(
switchMap((request: RestRequest) => this.requestService.getByHref(request.href)) switchMap((request: RestRequest) => this.requestService.getByHref(request.href))
@@ -176,9 +179,10 @@ export class SearchService implements OnDestroy {
/** /**
* Request the filter configuration for a given scope or the whole repository * Request the filter configuration for a given scope or the whole repository
* @param {string} scope UUID of the object for which config the filter config is requested, when no scope is provided the configuration for the whole repository is loaded * @param {string} scope UUID of the object for which config the filter config is requested, when no scope is provided the configuration for the whole repository is loaded
* @param {string} configurationName the name of the configuration
* @returns {Observable<RemoteData<SearchFilterConfig[]>>} The found filter configuration * @returns {Observable<RemoteData<SearchFilterConfig[]>>} The found filter configuration
*/ */
getConfig(scope?: string, configuration?: string): Observable<RemoteData<SearchFilterConfig[]>> { getConfig(scope?: string, configurationName?: string): Observable<RemoteData<SearchFilterConfig[]>> {
const requestObs = this.halService.getEndpoint(this.facetLinkPathPrefix).pipe( const requestObs = this.halService.getEndpoint(this.facetLinkPathPrefix).pipe(
map((url: string) => { map((url: string) => {
const args: string[] = []; const args: string[] = [];
@@ -187,22 +191,22 @@ export class SearchService implements OnDestroy {
args.push(`scope=${scope}`); args.push(`scope=${scope}`);
} }
if (isNotEmpty(configuration)) { if (isNotEmpty(configurationName)) {
args.push(`configuration=${configuration}`); args.push(`configuration=${configurationName}`);
} }
if (isNotEmpty(args)) { if (isNotEmpty(args)) {
url = new URLCombiner(url, `?${args.join('&')}`).toString(); url = new URLCombiner(url, `?${args.join('&')}`).toString();
} }
const request = new GetRequest(this.requestService.generateRequestId(), url); const request = new this.request(this.requestService.generateRequestId(), url);
return Object.assign(request, { return Object.assign(request, {
getResponseParser(): GenericConstructor<ResponseParsingService> { getResponseParser(): GenericConstructor<ResponseParsingService> {
return FacetConfigResponseParsingService; return FacetConfigResponseParsingService;
} }
}); });
}), }),
configureRequest(this.requestService, this.forceBypassCache) configureRequest(this.requestService)
); );
const requestEntryObs = requestObs.pipe( const requestEntryObs = requestObs.pipe(
@@ -238,14 +242,14 @@ export class SearchService implements OnDestroy {
url = searchOptions.toRestUrl(url, args); url = searchOptions.toRestUrl(url, args);
} }
const request = new GetRequest(this.requestService.generateRequestId(), url); const request = new this.request(this.requestService.generateRequestId(), url);
return Object.assign(request, { return Object.assign(request, {
getResponseParser(): GenericConstructor<ResponseParsingService> { getResponseParser(): GenericConstructor<ResponseParsingService> {
return FacetValueResponseParsingService; return FacetValueResponseParsingService;
} }
}); });
}), }),
configureRequest(this.requestService, this.forceBypassCache), configureRequest(this.requestService),
first() first()
); );

View File

@@ -412,6 +412,10 @@ export class TaskDeleteRequest extends DeleteRequest {
} }
} }
export class MyDSpaceRequest extends GetRequest {
public responseMsToLive = 0;
}
export class RequestError extends Error { export class RequestError extends Error {
statusCode: number; statusCode: number;
statusText: string; statusText: string;