103236: fix issue where setStaleByHrefSubtring wouldn't emit after all requests were stale

This commit is contained in:
Art Lowel
2023-06-14 18:51:42 +02:00
committed by Alexandre Vryghem
parent ca864379c8
commit ae6b183fae

View File

@@ -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'; import { cloneDeep } from 'lodash';
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';
@@ -292,22 +292,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)
);
}
})
); );
} }