|
1 | 1 | import { Injectable } from '@angular/core'; |
2 | 2 |
|
3 | | -import { Observable } from 'rxjs'; |
| 3 | +import { Observable, skipWhile } from 'rxjs'; |
4 | 4 | import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators'; |
5 | 5 |
|
6 | 6 | import { RequestService } from '../data/request.service'; |
@@ -115,24 +115,53 @@ export class SubmissionRestService { |
115 | 115 | * The endpoint link name |
116 | 116 | * @param id |
117 | 117 | * 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 |
118 | 120 | * @return Observable<SubmitDataResponseDefinitionObject> |
119 | 121 | * server response |
120 | 122 | */ |
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> { |
123 | 124 | return this.halService.getEndpoint(linkName).pipe( |
124 | 125 | map((endpointURL: string) => this.getEndpointByIDHref(endpointURL, id)), |
125 | 126 | filter((href: string) => isNotEmpty(href)), |
126 | 127 | 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 | + ); |
130 | 143 | }), |
131 | | - mergeMap((request) => this.rdbService.buildSingle(request.href)), |
132 | 144 | getFirstDataDefinition(), |
133 | 145 | ); |
134 | 146 | } |
135 | 147 |
|
| 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 | + |
136 | 165 | /** |
137 | 166 | * Make a new post request |
138 | 167 | * |
|
0 commit comments