mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-13 13:03:04 +00:00
Merge remote-tracking branch 'remotes/origin/master' into submission
# Conflicts: # package.json # src/app/+item-page/edit-item-page/item-delete/item-delete.component.spec.ts # src/app/+item-page/edit-item-page/item-private/item-private.component.spec.ts # src/app/+item-page/edit-item-page/item-public/item-public.component.spec.ts # src/app/+item-page/edit-item-page/item-reinstate/item-reinstate.component.spec.ts # src/app/+item-page/edit-item-page/item-withdraw/item-withdraw.component.spec.ts # src/app/+item-page/edit-item-page/simple-item-action/abstract-simple-item-action.component.spec.ts # src/app/+search-page/search-service/search.service.spec.ts # src/app/core/auth/auth-response-parsing.service.spec.ts # src/app/core/auth/auth-response-parsing.service.ts # src/app/core/cache/builders/remote-data-build.service.ts # src/app/core/cache/response-cache.reducer.spec.ts # src/app/core/cache/response-cache.service.spec.ts # src/app/core/cache/response.models.ts # src/app/core/config/config-response-parsing.service.ts # src/app/core/core.effects.ts # src/app/core/core.module.ts # src/app/core/core.reducers.ts # src/app/core/data/base-response-parsing.service.ts # src/app/core/data/data.service.ts # src/app/core/data/item-data.service.spec.ts # src/app/core/data/request.models.ts # src/app/core/data/request.service.spec.ts # src/app/core/data/request.service.ts # src/app/core/integration/integration-response-parsing.service.spec.ts # src/app/core/integration/integration-response-parsing.service.ts # src/app/core/integration/integration.service.ts # src/app/core/metadata/metadata.service.spec.ts # src/app/core/registry/registry.service.spec.ts # src/app/core/shared/hal-endpoint.service.ts # src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts # src/app/shared/mocks/mock-response-cache.service.ts # src/app/shared/shared.module.ts
This commit is contained in:
@@ -1,31 +1,32 @@
|
||||
import { merge as observableMerge, Observable, of as observableOf, race as observableRace } from 'rxjs';
|
||||
import { filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
import { remove } from 'lodash';
|
||||
import { merge as observableMerge, Observable } from 'rxjs';
|
||||
import { filter, map, mergeMap, take } from 'rxjs/operators';
|
||||
import { MemoizedSelector, select, Store } from '@ngrx/store';
|
||||
|
||||
import { hasValue } from '../../shared/empty.util';
|
||||
import { hasNoValue, hasValue } from '../../shared/empty.util';
|
||||
import { CacheableObject } from '../cache/object-cache.reducer';
|
||||
import { ObjectCacheService } from '../cache/object-cache.service';
|
||||
import { DSOSuccessResponse } from '../cache/response-cache.models';
|
||||
import { ResponseCacheEntry } from '../cache/response-cache.reducer';
|
||||
import { ResponseCacheService } from '../cache/response-cache.service';
|
||||
import { DSOSuccessResponse, RestResponse } from '../cache/response.models';
|
||||
import { coreSelector, CoreState } from '../core.reducers';
|
||||
import { IndexName } from '../index/index.reducer';
|
||||
import { pathSelector } from '../shared/selectors';
|
||||
import { UUIDService } from '../shared/uuid.service';
|
||||
import { RequestConfigureAction, RequestExecuteAction } from './request.actions';
|
||||
import { GetRequest, RestRequest, RestRequestMethod } from './request.models';
|
||||
import { GetRequest, RestRequest } from './request.models';
|
||||
|
||||
import { RequestEntry } from './request.reducer';
|
||||
import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
|
||||
import { RestRequestMethod } from './rest-request-method';
|
||||
import { getResponseFromEntry } from '../shared/operators';
|
||||
import { AddToIndexAction } from '../index/index.actions';
|
||||
|
||||
@Injectable()
|
||||
export class RequestService {
|
||||
private requestsOnTheirWayToTheStore: string[] = [];
|
||||
|
||||
constructor(private objectCache: ObjectCacheService,
|
||||
private responseCache: ResponseCacheService,
|
||||
private uuidService: UUIDService,
|
||||
private store: Store<CoreState>) {
|
||||
}
|
||||
@@ -38,6 +39,10 @@ export class RequestService {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.REQUEST, href);
|
||||
}
|
||||
|
||||
private originalUUIDFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, string> {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid);
|
||||
}
|
||||
|
||||
generateRequestId(): string {
|
||||
return `client/${this.uuidService.generate()}`;
|
||||
}
|
||||
@@ -60,7 +65,15 @@ export class RequestService {
|
||||
}
|
||||
|
||||
getByUUID(uuid: string): Observable<RequestEntry> {
|
||||
return this.store.pipe(select(this.entryFromUUIDSelector(uuid)));
|
||||
return observableRace(
|
||||
this.store.pipe(select(this.entryFromUUIDSelector(uuid))),
|
||||
this.store.pipe(
|
||||
select(this.originalUUIDFromUUIDSelector(uuid)),
|
||||
mergeMap((originalUUID) => {
|
||||
return this.store.pipe(select(this.entryFromUUIDSelector(originalUUID)))
|
||||
},
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
getByHref(href: string): Observable<RequestEntry> {
|
||||
@@ -83,9 +96,15 @@ export class RequestService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a certain request
|
||||
* Used to make sure a request is in the cache
|
||||
* @param {RestRequest} request The request to send out
|
||||
* @param {boolean} forceBypassCache When true, a new request is always dispatched
|
||||
*/
|
||||
// TODO to review "forceBypassCache" param when https://github.com/DSpace/dspace-angular/issues/217 will be fixed
|
||||
configure<T extends CacheableObject>(request: RestRequest, forceBypassCache: boolean = false): void {
|
||||
const isGetRequest = request.method === RestRequestMethod.Get;
|
||||
const isGetRequest = request.method === RestRequestMethod.GET;
|
||||
if (forceBypassCache) {
|
||||
this.clearRequestsOnTheirWayToTheStore(request.href);
|
||||
}
|
||||
@@ -94,16 +113,34 @@ export class RequestService {
|
||||
if (isGetRequest) {
|
||||
this.trackRequestsOnTheirWayToTheStore(request);
|
||||
}
|
||||
} else {
|
||||
this.getByHref(request.href).pipe(
|
||||
filter((entry) => hasValue(entry)),
|
||||
take(1)
|
||||
).subscribe((entry) => {
|
||||
return this.store.dispatch(new AddToIndexAction(IndexName.UUID_MAPPING, request.uuid, entry.request.uuid))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a request is in the cache or if it's still pending
|
||||
* @param {GetRequest} request The request to check
|
||||
* @returns {boolean} True if the request is cached or still pending
|
||||
*/
|
||||
private isCachedOrPending(request: GetRequest) {
|
||||
let isCached = this.objectCache.hasBySelfLink(request.href);
|
||||
if (!isCached && this.responseCache.has(request.href)) {
|
||||
const responses = this.responseCache.get(request.href).pipe(
|
||||
take(1),
|
||||
map((entry: ResponseCacheEntry) => entry.response)
|
||||
);
|
||||
if (isCached) {
|
||||
const responses: Observable<RestResponse> = this.isReusable(request.uuid).pipe(
|
||||
filter((reusable: boolean) => reusable),
|
||||
switchMap(() => {
|
||||
return this.getByHref(request.href).pipe(
|
||||
getResponseFromEntry(),
|
||||
take(1)
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
const errorResponses = responses.pipe(filter((response) => !response.isSuccessful), map(() => true)); // TODO add a configurable number of retries in case of an error.
|
||||
const dsoSuccessResponses = responses.pipe(
|
||||
@@ -112,6 +149,7 @@ export class RequestService {
|
||||
map((resourceSelfLinks: string[]) => resourceSelfLinks
|
||||
.every((selfLink) => this.objectCache.hasBySelfLink(selfLink))
|
||||
));
|
||||
|
||||
const otherSuccessResponses = responses.pipe(filter((response) => response.isSuccessful && !hasValue((response as DSOSuccessResponse).resourceSelfLinks)), map(() => true));
|
||||
|
||||
observableMerge(errorResponses, otherSuccessResponses, dsoSuccessResponses).subscribe((c) => isCached = c);
|
||||
@@ -120,6 +158,10 @@ export class RequestService {
|
||||
return isCached || isPending;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure and execute the request
|
||||
* @param {RestRequest} request to dispatch
|
||||
*/
|
||||
private dispatchRequest(request: RestRequest) {
|
||||
this.store.dispatch(new RequestConfigureAction(request));
|
||||
this.store.dispatch(new RequestExecuteAction(request.uuid));
|
||||
@@ -141,4 +183,41 @@ export class RequestService {
|
||||
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((pendingHref: string) => pendingHref !== request.href)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch commit action to send all changes (for a certain method) to the server (buffer)
|
||||
* @param {RestRequestMethod} method RestRequestMethod for which the changes should be committed
|
||||
*/
|
||||
commit(method?: RestRequestMethod) {
|
||||
this.store.dispatch(new CommitSSBAction(method))
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a Response should still be cached
|
||||
*
|
||||
* @param uuid
|
||||
* the uuid of the entry to check
|
||||
* @return boolean
|
||||
* false if the uuid has no value, no entry could be found, the response was nog successful or its time to
|
||||
* live has exceeded, true otherwise
|
||||
*/
|
||||
private isReusable(uuid: string): Observable<boolean> {
|
||||
if (hasNoValue(uuid)) {
|
||||
return observableOf(false);
|
||||
} else {
|
||||
const requestEntry$ = this.getByUUID(uuid);
|
||||
return requestEntry$.pipe(
|
||||
filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
|
||||
map((entry: RequestEntry) => {
|
||||
if (hasValue(entry) && entry.response.isSuccessful) {
|
||||
const timeOutdated = entry.response.timeAdded + entry.request.responseMsToLive;
|
||||
const isOutDated = new Date().getTime() > timeOutdated;
|
||||
return !isOutDated;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user