Skip to content
Snippets Groups Projects
Commit 4307cb0f authored by Kristof De Langhe's avatar Kristof De Langhe
Browse files

55946: Clear data/request attached to the hrefs deleted from index

parent e275fe59
Branches
Tags
No related merge requests found
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store'; import { MemoizedSelector, select, Store } from '@ngrx/store';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { NormalizedCollection } from '../cache/models/normalized-collection.model'; import { NormalizedCollection } from '../cache/models/normalized-collection.model';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { CoreState } from '../core.reducers'; import { coreSelector, CoreState } from '../core.reducers';
import { Collection } from '../shared/collection.model'; import { Collection } from '../shared/collection.model';
import { ComColDataService } from './comcol-data.service'; import { ComColDataService } from './comcol-data.service';
import { CommunityDataService } from './community-data.service'; import { CommunityDataService } from './community-data.service';
...@@ -12,7 +12,7 @@ import { HALEndpointService } from '../shared/hal-endpoint.service'; ...@@ -12,7 +12,7 @@ import { HALEndpointService } from '../shared/hal-endpoint.service';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { RemoteData } from './remote-data'; import { RemoteData } from './remote-data';
import { PaginatedList } from './paginated-list'; import { PaginatedList } from './paginated-list';
import { distinctUntilChanged, map, take } from 'rxjs/operators'; import { distinctUntilChanged, map, mergeMap, switchMap, take } from 'rxjs/operators';
import { hasValue, isNotEmptyOperator } from '../../shared/empty.util'; import { hasValue, isNotEmptyOperator } from '../../shared/empty.util';
import { GetRequest } from './request.models'; import { GetRequest } from './request.models';
import { configureRequest } from '../shared/operators'; import { configureRequest } from '../shared/operators';
...@@ -23,6 +23,8 @@ import { DSpaceObject } from '../shared/dspace-object.model'; ...@@ -23,6 +23,8 @@ import { DSpaceObject } from '../shared/dspace-object.model';
import { DSOResponseParsingService } from './dso-response-parsing.service'; import { DSOResponseParsingService } from './dso-response-parsing.service';
import { IndexName, IndexState } from '../index/index.reducer'; import { IndexName, IndexState } from '../index/index.reducer';
import { RemoveFromIndexBySubstringAction } from '../index/index.actions'; import { RemoveFromIndexBySubstringAction } from '../index/index.actions';
import { pathSelector } from '../shared/selectors';
import { RequestState } from './request.reducer';
@Injectable() @Injectable()
export class CollectionDataService extends ComColDataService<NormalizedCollection, Collection> { export class CollectionDataService extends ComColDataService<NormalizedCollection, Collection> {
...@@ -71,6 +73,7 @@ export class CollectionDataService extends ComColDataService<NormalizedCollectio ...@@ -71,6 +73,7 @@ export class CollectionDataService extends ComColDataService<NormalizedCollectio
clearMappingItemsRequests(collectionId: string) { clearMappingItemsRequests(collectionId: string) {
this.getMappingItemsEndpoint(collectionId).pipe(take(1)).subscribe((href: string) => { this.getMappingItemsEndpoint(collectionId).pipe(take(1)).subscribe((href: string) => {
this.requestService.removeByHrefSubstring(href);
this.indexStore.dispatch(new RemoveFromIndexBySubstringAction(IndexName.REQUEST, href)); this.indexStore.dispatch(new RemoveFromIndexBySubstringAction(IndexName.REQUEST, href));
}); });
} }
......
...@@ -10,7 +10,8 @@ export const RequestActionTypes = { ...@@ -10,7 +10,8 @@ export const RequestActionTypes = {
CONFIGURE: type('dspace/core/data/request/CONFIGURE'), CONFIGURE: type('dspace/core/data/request/CONFIGURE'),
EXECUTE: type('dspace/core/data/request/EXECUTE'), EXECUTE: type('dspace/core/data/request/EXECUTE'),
COMPLETE: type('dspace/core/data/request/COMPLETE'), COMPLETE: type('dspace/core/data/request/COMPLETE'),
RESET_TIMESTAMPS: type('dspace/core/data/request/RESET_TIMESTAMPS') RESET_TIMESTAMPS: type('dspace/core/data/request/RESET_TIMESTAMPS'),
REMOVE: type('dspace/core/data/request/REMOVE')
}; };
/* tslint:disable:max-classes-per-file */ /* tslint:disable:max-classes-per-file */
...@@ -82,6 +83,24 @@ export class ResetResponseTimestampsAction implements Action { ...@@ -82,6 +83,24 @@ export class ResetResponseTimestampsAction implements Action {
} }
} }
/**
* An ngrx action to remove a cached request
*/
export class RequestRemoveAction implements Action {
type = RequestActionTypes.REMOVE;
uuid: string;
/**
* Create a new RequestRemoveAction
*
* @param uuid
* the request's uuid
*/
constructor(uuid: string) {
this.uuid = uuid
}
}
/* tslint:enable:max-classes-per-file */ /* tslint:enable:max-classes-per-file */
/** /**
...@@ -91,4 +110,5 @@ export type RequestAction ...@@ -91,4 +110,5 @@ export type RequestAction
= RequestConfigureAction = RequestConfigureAction
| RequestExecuteAction | RequestExecuteAction
| RequestCompleteAction | RequestCompleteAction
| ResetResponseTimestampsAction; | ResetResponseTimestampsAction
| RequestRemoveAction;
import { import {
RequestActionTypes, RequestAction, RequestConfigureAction, RequestActionTypes, RequestAction, RequestConfigureAction,
RequestExecuteAction, RequestCompleteAction, ResetResponseTimestampsAction RequestExecuteAction, RequestCompleteAction, ResetResponseTimestampsAction, RequestRemoveAction
} from './request.actions'; } from './request.actions';
import { RestRequest } from './request.models'; import { RestRequest } from './request.models';
import { RestResponse } from '../cache/response.models'; import { RestResponse } from '../cache/response.models';
...@@ -38,6 +38,10 @@ export function requestReducer(state = initialState, action: RequestAction): Req ...@@ -38,6 +38,10 @@ export function requestReducer(state = initialState, action: RequestAction): Req
return resetResponseTimestamps(state, action as ResetResponseTimestampsAction); return resetResponseTimestamps(state, action as ResetResponseTimestampsAction);
} }
case RequestActionTypes.REMOVE: {
return removeRequest(state, action as RequestRemoveAction);
}
default: { default: {
return state; return state;
} }
...@@ -95,3 +99,13 @@ function resetResponseTimestamps(state: RequestState, action: ResetResponseTimes ...@@ -95,3 +99,13 @@ function resetResponseTimestamps(state: RequestState, action: ResetResponseTimes
}); });
return newState; return newState;
} }
function removeRequest(state: RequestState, action: RequestRemoveAction): RequestState {
const newState = Object.create(null);
for (const value in state) {
if (value !== action.uuid) {
newState[value] = state[value];
}
}
return newState;
}
...@@ -15,16 +15,16 @@ import { ...@@ -15,16 +15,16 @@ import {
import { race as observableRace } from 'rxjs'; import { race as observableRace } from 'rxjs';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { MemoizedSelector, select, Store } from '@ngrx/store'; import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
import { hasNoValue, hasValue, isNotUndefined } from '../../shared/empty.util'; import { hasNoValue, hasValue, isNotEmpty, isNotUndefined } from '../../shared/empty.util';
import { CacheableObject } from '../cache/object-cache.reducer'; import { CacheableObject } from '../cache/object-cache.reducer';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { DSOSuccessResponse, RestResponse } from '../cache/response.models'; import { DSOSuccessResponse, RestResponse } from '../cache/response.models';
import { coreSelector, CoreState } from '../core.reducers'; import { coreSelector, CoreState } from '../core.reducers';
import { IndexName } from '../index/index.reducer'; import { IndexName, IndexState } from '../index/index.reducer';
import { pathSelector } from '../shared/selectors'; import { pathSelector } from '../shared/selectors';
import { UUIDService } from '../shared/uuid.service'; import { UUIDService } from '../shared/uuid.service';
import { RequestConfigureAction, RequestExecuteAction } from './request.actions'; import { RequestConfigureAction, RequestExecuteAction, RequestRemoveAction } from './request.actions';
import { GetRequest, RestRequest } from './request.models'; import { GetRequest, RestRequest } from './request.models';
import { RequestEntry } from './request.reducer'; import { RequestEntry } from './request.reducer';
...@@ -54,6 +54,25 @@ export class RequestService { ...@@ -54,6 +54,25 @@ export class RequestService {
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid); return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid);
} }
private uuidsFromHrefSubstringSelector(selector: MemoizedSelector<any, IndexState>, name: string, href: string): MemoizedSelector<any, string[]> {
return createSelector(selector, (state: IndexState) => this.getUuidsFromHrefSubstring(state, name, href));
}
private getUuidsFromHrefSubstring(state: IndexState, name: string, href: string): string[] {
let result = [];
if (isNotEmpty(state)) {
const subState = state[name];
if (isNotEmpty(subState)) {
for (const value in subState) {
if (value.indexOf(href) > -1) {
result = [...result, subState[value]];
}
}
}
}
return result;
}
generateRequestId(): string { generateRequestId(): string {
return `client/${this.uuidService.generate()}`; return `client/${this.uuidService.generate()}`;
} }
...@@ -119,6 +138,28 @@ export class RequestService { ...@@ -119,6 +138,28 @@ export class RequestService {
} }
} }
removeByHref(href: string) {
this.store.pipe(
select(this.uuidFromHrefSelector(href))
).subscribe((uuid: string) => {
this.removeByUuid(uuid);
});
}
removeByHrefSubstring(href: string) {
this.store.pipe(
select(this.uuidsFromHrefSubstringSelector(pathSelector<CoreState, IndexState>(coreSelector, 'index'), IndexName.REQUEST, href))
).subscribe((uuids: string[]) => {
for (const uuid of uuids) {
this.removeByUuid(uuid);
}
});
}
removeByUuid(uuid: string) {
this.store.dispatch(new RequestRemoveAction(uuid));
}
/** /**
* Check if a request is in the cache or if it's still pending * Check if a request is in the cache or if it's still pending
* @param {GetRequest} request The request to check * @param {GetRequest} request The request to check
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment