Skip to content

Commit 99e8c10

Browse files
127655: Submission get data stale re-request
1 parent 287d35c commit 99e8c10

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/app/core/submission/submission-rest.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('SubmissionRestService test suite', () => {
6262
scheduler.schedule(() => service.getDataById(resourceEndpoint, resourceScope).subscribe());
6363
scheduler.flush();
6464

65-
expect(requestService.send).toHaveBeenCalledWith(expected);
65+
expect(requestService.send).toHaveBeenCalledWith(expected, false);
6666
});
6767
});
6868

src/app/core/submission/submission-rest.service.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22

3-
import { Observable } from 'rxjs';
3+
import { Observable, skipWhile } from 'rxjs';
44
import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators';
55

66
import { RequestService } from '../data/request.service';
@@ -115,24 +115,53 @@ export class SubmissionRestService {
115115
* The endpoint link name
116116
* @param id
117117
* The submission Object to retrieve
118+
* @param useCachedVersionIfAvailable
119+
* If this is true, the request will only be sent if there's no valid & cached version. Defaults to false
118120
* @return Observable<SubmitDataResponseDefinitionObject>
119121
* server response
120122
*/
121-
public getDataById(linkName: string, id: string): Observable<SubmitDataResponseDefinitionObject> {
122-
const requestId = this.requestService.generateRequestId();
123+
public getDataById(linkName: string, id: string, useCachedVersionIfAvailable = false): Observable<SubmitDataResponseDefinitionObject> {
123124
return this.halService.getEndpoint(linkName).pipe(
124125
map((endpointURL: string) => this.getEndpointByIDHref(endpointURL, id)),
125126
filter((href: string) => isNotEmpty(href)),
126127
distinctUntilChanged(),
127-
map((endpointURL: string) => new SubmissionRequest(requestId, endpointURL)),
128-
tap((request: RestRequest) => {
129-
this.requestService.send(request);
128+
mergeMap((endpointURL: string) => {
129+
const request = this.sendGetDataRequest(endpointURL, useCachedVersionIfAvailable);
130+
const startTime: number = new Date().getTime();
131+
return this.rdbService.buildSingle(request.href).pipe(
132+
// This skip ensures that if a stale object is present in the cache when you do a
133+
// call it isn't immediately returned, but we wait until the remote data for the new request
134+
// is created. If useCachedVersionIfAvailable is false it also ensures you don't get a
135+
// cached completed object
136+
skipWhile((rd: RemoteData<SubmissionResponse>) => rd.isStale || (!useCachedVersionIfAvailable && rd.lastUpdated < startTime)),
137+
tap((rd: RemoteData<SubmissionResponse>) => {
138+
if (hasValue(rd) && rd.isStale) {
139+
this.sendGetDataRequest(endpointURL, useCachedVersionIfAvailable);
140+
}
141+
})
142+
);
130143
}),
131-
mergeMap((request) => this.rdbService.buildSingle(request.href)),
132144
getFirstDataDefinition(),
133145
);
134146
}
135147

148+
/**
149+
* Send a GET SubmissionRequest
150+
*
151+
* @param href
152+
* Endpoint URL of the submission data
153+
* @param useCachedVersionIfAvailable
154+
* If this is true, the request will only be sent if there's no valid & cached version. Defaults to false
155+
* @return RestRequest
156+
* Request sent
157+
*/
158+
private sendGetDataRequest(href: string, useCachedVersionIfAvailable = false): RestRequest {
159+
const requestId = this.requestService.generateRequestId();
160+
const request = new SubmissionRequest(requestId, href);
161+
this.requestService.send(request, useCachedVersionIfAvailable);
162+
return request;
163+
}
164+
136165
/**
137166
* Make a new post request
138167
*

0 commit comments

Comments
 (0)