Skip to content

Commit 85610d4

Browse files
119276: Fixed search service first emitting cached stale values instead of waiting for non-stale response
This was problematic for the places that used getFist operators. This is because they only emit data once, and the first value could be the old cached value (cherry picked from commit edfaee6)
1 parent da12147 commit 85610d4

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/app/core/shared/search/search.service.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-classes-per-file */
22
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
33
import { Injectable, OnDestroy } from '@angular/core';
4-
import { map, switchMap, take } from 'rxjs/operators';
4+
import { map, switchMap, take, skipWhile } from 'rxjs/operators';
55
import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model';
66
import { ResponseParsingService } from '../../data/parsing.service';
77
import { RemoteData } from '../../data/remote-data';
@@ -140,6 +140,7 @@ export class SearchService implements OnDestroy {
140140
search<T extends DSpaceObject>(searchOptions?: PaginatedSearchOptions, responseMsToLive?: number, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<T>[]): Observable<RemoteData<SearchObjects<T>>> {
141141
const href$ = this.getEndpoint(searchOptions);
142142

143+
let startTime: number;
143144
href$.pipe(
144145
take(1),
145146
map((href: string) => {
@@ -163,14 +164,21 @@ export class SearchService implements OnDestroy {
163164
searchOptions: searchOptions
164165
});
165166

167+
startTime = new Date().getTime();
166168
this.requestService.send(request, useCachedVersionIfAvailable);
167169
});
168170

169171
const sqr$ = href$.pipe(
170172
switchMap((href: string) => this.rdb.buildFromHref<SearchObjects<T>>(href))
171173
);
172174

173-
return this.directlyAttachIndexableObjects(sqr$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
175+
return this.directlyAttachIndexableObjects(sqr$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow).pipe(
176+
// This skip ensures that if a stale object is present in the cache when you do a
177+
// call it isn't immediately returned, but we wait until the remote data for the new request
178+
// is created. If useCachedVersionIfAvailable is false it also ensures you don't get a
179+
// cached completed object
180+
skipWhile((rd: RemoteData<SearchObjects<T>>) => rd.isStale || (!useCachedVersionIfAvailable && rd.lastUpdated < startTime)),
181+
);
174182
}
175183

176184
/**
@@ -291,9 +299,16 @@ export class SearchService implements OnDestroy {
291299
return FacetValueResponseParsingService;
292300
}
293301
});
302+
const startTime = new Date().getTime();
294303
this.requestService.send(request, useCachedVersionIfAvailable);
295304

296-
return this.rdb.buildFromHref(href);
305+
return this.rdb.buildFromHref(href).pipe(
306+
// This skip ensures that if a stale object is present in the cache when you do a
307+
// call it isn't immediately returned, but we wait until the remote data for the new request
308+
// is created. If useCachedVersionIfAvailable is false it also ensures you don't get a
309+
// cached completed object
310+
skipWhile((rd: RemoteData<FacetValues>) => rd.isStale || (!useCachedVersionIfAvailable && rd.lastUpdated < startTime)),
311+
);
297312
}
298313

299314
/**

0 commit comments

Comments
 (0)