mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-17 23:13:04 +00:00
fixed an issue where certain GET requests would be performed more often then needed and added tests for the requestservice (not everything tested yet)
This commit is contained in:
@@ -14,24 +14,10 @@ import { IndexName } from '../index/index.reducer';
|
||||
import { pathSelector } from '../shared/selectors';
|
||||
import { UUIDService } from '../shared/uuid.service';
|
||||
import { RequestConfigureAction, RequestExecuteAction } from './request.actions';
|
||||
import { RestRequest, RestRequestMethod } from './request.models';
|
||||
import { GetRequest, RestRequest, RestRequestMethod } from './request.models';
|
||||
|
||||
import { RequestEntry, RequestState } from './request.reducer';
|
||||
|
||||
function entryFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, RequestEntry> {
|
||||
return pathSelector<CoreState, RequestEntry>(coreSelector, 'data/request', uuid);
|
||||
}
|
||||
|
||||
function uuidFromHrefSelector(href: string): MemoizedSelector<CoreState, string> {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.REQUEST, href);
|
||||
}
|
||||
|
||||
export function requestStateSelector(): MemoizedSelector<CoreState, RequestState> {
|
||||
return createSelector(coreSelector, (state: CoreState) => {
|
||||
return state['data/request'] as RequestState;
|
||||
});
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RequestService {
|
||||
private requestsOnTheirWayToTheStore: string[] = [];
|
||||
@@ -42,19 +28,27 @@ export class RequestService {
|
||||
private store: Store<CoreState>) {
|
||||
}
|
||||
|
||||
private entryFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, RequestEntry> {
|
||||
return pathSelector<CoreState, RequestEntry>(coreSelector, 'data/request', uuid);
|
||||
}
|
||||
|
||||
private uuidFromHrefSelector(href: string): MemoizedSelector<CoreState, string> {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.REQUEST, href);
|
||||
}
|
||||
|
||||
generateRequestId(): string {
|
||||
return `client/${this.uuidService.generate()}`;
|
||||
}
|
||||
|
||||
isPending(uuid: string): boolean {
|
||||
isPending(request: GetRequest): boolean {
|
||||
// first check requests that haven't made it to the store yet
|
||||
if (this.requestsOnTheirWayToTheStore.includes(uuid)) {
|
||||
if (this.requestsOnTheirWayToTheStore.includes(request.href)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// then check the store
|
||||
let isPending = false;
|
||||
this.store.select(entryFromUUIDSelector(uuid))
|
||||
this.getByHref(request.href)
|
||||
.take(1)
|
||||
.subscribe((re: RequestEntry) => {
|
||||
isPending = (hasValue(re) && !re.completed)
|
||||
@@ -64,11 +58,11 @@ export class RequestService {
|
||||
}
|
||||
|
||||
getByUUID(uuid: string): Observable<RequestEntry> {
|
||||
return this.store.select(entryFromUUIDSelector(uuid));
|
||||
return this.store.select(this.entryFromUUIDSelector(uuid));
|
||||
}
|
||||
|
||||
getByHref(href: string): Observable<RequestEntry> {
|
||||
return this.store.select(uuidFromHrefSelector(href))
|
||||
return this.store.select(this.uuidFromHrefSelector(href))
|
||||
.flatMap((uuid: string) => this.getByUUID(uuid));
|
||||
}
|
||||
|
||||
@@ -78,7 +72,7 @@ export class RequestService {
|
||||
}
|
||||
}
|
||||
|
||||
private isCachedOrPending(request: RestRequest) {
|
||||
private isCachedOrPending(request: GetRequest) {
|
||||
let isCached = this.objectCache.hasBySelfLink(request.href);
|
||||
if (!isCached && this.responseCache.has(request.href)) {
|
||||
const [successResponse, errorResponse] = this.responseCache.get(request.href)
|
||||
@@ -102,7 +96,7 @@ export class RequestService {
|
||||
).subscribe((c) => isCached = c);
|
||||
}
|
||||
|
||||
const isPending = this.isPending(request.uuid);
|
||||
const isPending = this.isPending(request);
|
||||
|
||||
return isCached || isPending;
|
||||
}
|
||||
@@ -110,23 +104,25 @@ export class RequestService {
|
||||
private dispatchRequest(request: RestRequest) {
|
||||
this.store.dispatch(new RequestConfigureAction(request));
|
||||
this.store.dispatch(new RequestExecuteAction(request.uuid));
|
||||
this.trackRequestsOnTheirWayToTheStore(request.uuid);
|
||||
if (request.method === RestRequestMethod.Get) {
|
||||
this.trackRequestsOnTheirWayToTheStore(request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ngrx action dispatches are asynchronous. But this.isPending needs to return true as soon as the
|
||||
* configure method for a request has been executed, otherwise certain requests will happen multiple times.
|
||||
* configure method for a GET request has been executed, otherwise certain requests will happen multiple times.
|
||||
*
|
||||
* This method will store the href of every request that gets configured in a local variable, and
|
||||
* This method will store the href of every GET request that gets configured in a local variable, and
|
||||
* remove it as soon as it can be found in the store.
|
||||
*/
|
||||
private trackRequestsOnTheirWayToTheStore(uuid: string) {
|
||||
this.requestsOnTheirWayToTheStore = [...this.requestsOnTheirWayToTheStore, uuid];
|
||||
this.store.select(entryFromUUIDSelector(uuid))
|
||||
private trackRequestsOnTheirWayToTheStore(request: GetRequest) {
|
||||
this.requestsOnTheirWayToTheStore = [...this.requestsOnTheirWayToTheStore, request.href];
|
||||
this.store.select(this.entryFromUUIDSelector(request.href))
|
||||
.filter((re: RequestEntry) => hasValue(re))
|
||||
.take(1)
|
||||
.subscribe((re: RequestEntry) => {
|
||||
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((pendingUUID: string) => pendingUUID !== uuid)
|
||||
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((pendingHref: string) => pendingHref !== request.href)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user