127655: Submission get data stale re-request

This commit is contained in:
Kristof De Langhe
2025-04-28 15:15:26 +02:00
parent 287d35cb26
commit 99e8c1044c
2 changed files with 37 additions and 8 deletions

View File

@@ -62,7 +62,7 @@ describe('SubmissionRestService test suite', () => {
scheduler.schedule(() => service.getDataById(resourceEndpoint, resourceScope).subscribe()); scheduler.schedule(() => service.getDataById(resourceEndpoint, resourceScope).subscribe());
scheduler.flush(); scheduler.flush();
expect(requestService.send).toHaveBeenCalledWith(expected); expect(requestService.send).toHaveBeenCalledWith(expected, false);
}); });
}); });

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable, skipWhile } from 'rxjs';
import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators';
import { RequestService } from '../data/request.service'; import { RequestService } from '../data/request.service';
@@ -115,24 +115,53 @@ export class SubmissionRestService {
* The endpoint link name * The endpoint link name
* @param id * @param id
* The submission Object to retrieve * The submission Object to retrieve
* @param useCachedVersionIfAvailable
* If this is true, the request will only be sent if there's no valid & cached version. Defaults to false
* @return Observable<SubmitDataResponseDefinitionObject> * @return Observable<SubmitDataResponseDefinitionObject>
* server response * server response
*/ */
public getDataById(linkName: string, id: string): Observable<SubmitDataResponseDefinitionObject> { public getDataById(linkName: string, id: string, useCachedVersionIfAvailable = false): Observable<SubmitDataResponseDefinitionObject> {
const requestId = this.requestService.generateRequestId();
return this.halService.getEndpoint(linkName).pipe( return this.halService.getEndpoint(linkName).pipe(
map((endpointURL: string) => this.getEndpointByIDHref(endpointURL, id)), map((endpointURL: string) => this.getEndpointByIDHref(endpointURL, id)),
filter((href: string) => isNotEmpty(href)), filter((href: string) => isNotEmpty(href)),
distinctUntilChanged(), distinctUntilChanged(),
map((endpointURL: string) => new SubmissionRequest(requestId, endpointURL)), mergeMap((endpointURL: string) => {
tap((request: RestRequest) => { const request = this.sendGetDataRequest(endpointURL, useCachedVersionIfAvailable);
this.requestService.send(request); const startTime: number = new Date().getTime();
return this.rdbService.buildSingle(request.href).pipe(
// This skip ensures that if a stale object is present in the cache when you do a
// call it isn't immediately returned, but we wait until the remote data for the new request
// is created. If useCachedVersionIfAvailable is false it also ensures you don't get a
// cached completed object
skipWhile((rd: RemoteData<SubmissionResponse>) => rd.isStale || (!useCachedVersionIfAvailable && rd.lastUpdated < startTime)),
tap((rd: RemoteData<SubmissionResponse>) => {
if (hasValue(rd) && rd.isStale) {
this.sendGetDataRequest(endpointURL, useCachedVersionIfAvailable);
}
})
);
}), }),
mergeMap((request) => this.rdbService.buildSingle(request.href)),
getFirstDataDefinition(), getFirstDataDefinition(),
); );
} }
/**
* Send a GET SubmissionRequest
*
* @param href
* Endpoint URL of the submission data
* @param useCachedVersionIfAvailable
* If this is true, the request will only be sent if there's no valid & cached version. Defaults to false
* @return RestRequest
* Request sent
*/
private sendGetDataRequest(href: string, useCachedVersionIfAvailable = false): RestRequest {
const requestId = this.requestService.generateRequestId();
const request = new SubmissionRequest(requestId, href);
this.requestService.send(request, useCachedVersionIfAvailable);
return request;
}
/** /**
* Make a new post request * Make a new post request
* *