mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Merge pull request #2349 from alexandrevryghem/fix-setStaleByHrefSubtring-not-emitting-after-all-requests-were-stale_contribute-main
Fix `setStaleByHrefSubstring` not emitting true when all requests are stale
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Store, StoreModule } from '@ngrx/store';
|
import { Store, StoreModule } from '@ngrx/store';
|
||||||
import { cold, getTestScheduler } from 'jasmine-marbles';
|
import { cold, getTestScheduler } from 'jasmine-marbles';
|
||||||
import { EMPTY, of as observableOf } from 'rxjs';
|
import { EMPTY, Observable, of as observableOf } from 'rxjs';
|
||||||
import { TestScheduler } from 'rxjs/testing';
|
import { TestScheduler } from 'rxjs/testing';
|
||||||
|
|
||||||
import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock';
|
import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock';
|
||||||
@@ -638,4 +638,48 @@ describe('RequestService', () => {
|
|||||||
expect(done$).toBeObservable(cold('-----(t|)', { t: true }));
|
expect(done$).toBeObservable(cold('-----(t|)', { t: true }));
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('setStaleByHrefSubstring', () => {
|
||||||
|
let dispatchSpy: jasmine.Spy;
|
||||||
|
let getByUUIDSpy: jasmine.Spy;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
dispatchSpy = spyOn(store, 'dispatch');
|
||||||
|
getByUUIDSpy = spyOn(service, 'getByUUID').and.callThrough();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with an empty/no matching requests in the state', () => {
|
||||||
|
it('should return true', () => {
|
||||||
|
const done$: Observable<boolean> = service.setStaleByHrefSubstring('https://rest.api/endpoint/selfLink');
|
||||||
|
expect(done$).toBeObservable(cold('(a|)', { a: true }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with a matching request in the state', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const state = Object.assign({}, initialState, {
|
||||||
|
core: Object.assign({}, initialState.core, {
|
||||||
|
'index': {
|
||||||
|
'get-request/href-to-uuid': {
|
||||||
|
'https://rest.api/endpoint/selfLink': '5f2a0d2a-effa-4d54-bd54-5663b960f9eb'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
mockStore.setState(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an Observable that emits true as soon as the request is stale', () => {
|
||||||
|
dispatchSpy.and.callFake(() => { /* empty */ }); // don't actually set as stale
|
||||||
|
getByUUIDSpy.and.returnValue(cold('a-b--c--d-', { // but fake the state in the cache
|
||||||
|
a: { state: RequestEntryState.ResponsePending },
|
||||||
|
b: { state: RequestEntryState.Success },
|
||||||
|
c: { state: RequestEntryState.SuccessStale },
|
||||||
|
d: { state: RequestEntryState.Error },
|
||||||
|
}));
|
||||||
|
const done$: Observable<boolean> = service.setStaleByHrefSubstring('https://rest.api/endpoint/selfLink');
|
||||||
|
expect(done$).toBeObservable(cold('-----(a|)', { a: true }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -2,8 +2,8 @@ import { Injectable } from '@angular/core';
|
|||||||
import { HttpHeaders } from '@angular/common/http';
|
import { HttpHeaders } from '@angular/common/http';
|
||||||
|
|
||||||
import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
|
import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable, from as observableFrom } from 'rxjs';
|
||||||
import { filter, map, take, tap } from 'rxjs/operators';
|
import { filter, find, map, mergeMap, switchMap, take, tap, toArray } from 'rxjs/operators';
|
||||||
import cloneDeep from 'lodash/cloneDeep';
|
import cloneDeep from 'lodash/cloneDeep';
|
||||||
import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../../shared/empty.util';
|
import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../../shared/empty.util';
|
||||||
import { ObjectCacheEntry } from '../cache/object-cache.reducer';
|
import { ObjectCacheEntry } from '../cache/object-cache.reducer';
|
||||||
@@ -300,22 +300,42 @@ export class RequestService {
|
|||||||
* Set all requests that match (part of) the href to stale
|
* Set all requests that match (part of) the href to stale
|
||||||
*
|
*
|
||||||
* @param href A substring of the request(s) href
|
* @param href A substring of the request(s) href
|
||||||
* @return Returns an observable emitting whether or not the cache is removed
|
* @return Returns an observable emitting when those requests are all stale
|
||||||
*/
|
*/
|
||||||
setStaleByHrefSubstring(href: string): Observable<boolean> {
|
setStaleByHrefSubstring(href: string): Observable<boolean> {
|
||||||
this.store.pipe(
|
const requestUUIDs$ = this.store.pipe(
|
||||||
select(uuidsFromHrefSubstringSelector(requestIndexSelector, href)),
|
select(uuidsFromHrefSubstringSelector(requestIndexSelector, href)),
|
||||||
take(1)
|
take(1)
|
||||||
).subscribe((uuids: string[]) => {
|
);
|
||||||
|
requestUUIDs$.subscribe((uuids: string[]) => {
|
||||||
for (const uuid of uuids) {
|
for (const uuid of uuids) {
|
||||||
this.store.dispatch(new RequestStaleAction(uuid));
|
this.store.dispatch(new RequestStaleAction(uuid));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((reqHref: string) => reqHref.indexOf(href) < 0);
|
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((reqHref: string) => reqHref.indexOf(href) < 0);
|
||||||
|
|
||||||
return this.store.pipe(
|
// emit true after all requests are stale
|
||||||
select(uuidsFromHrefSubstringSelector(requestIndexSelector, href)),
|
return requestUUIDs$.pipe(
|
||||||
map((uuids) => isEmpty(uuids))
|
switchMap((uuids: string[]) => {
|
||||||
|
if (isEmpty(uuids)) {
|
||||||
|
// if there were no matching requests, emit true immediately
|
||||||
|
return [true];
|
||||||
|
} else {
|
||||||
|
// otherwise emit all request uuids in order
|
||||||
|
return observableFrom(uuids).pipe(
|
||||||
|
// retrieve the RequestEntry for each uuid
|
||||||
|
mergeMap((uuid: string) => this.getByUUID(uuid)),
|
||||||
|
// check whether it is undefined or stale
|
||||||
|
map((request: RequestEntry) => hasNoValue(request) || isStale(request.state)),
|
||||||
|
// if it is, complete
|
||||||
|
find((stale: boolean) => stale === true),
|
||||||
|
// after all observables above are completed, emit them as a single array
|
||||||
|
toArray(),
|
||||||
|
// when the array comes in, emit true
|
||||||
|
map(() => true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user