(
url: string,
ref: PRRef,
reason: PRContextRefreshReason,
options?: {
readonly force?: boolean;
readonly queueAfterInflight?: boolean;
},
)
| 205 | } |
| 206 | |
| 207 | private refresh( |
| 208 | url: string, |
| 209 | ref: PRRef, |
| 210 | reason: PRContextRefreshReason, |
| 211 | options?: { |
| 212 | readonly force?: boolean; |
| 213 | readonly queueAfterInflight?: boolean; |
| 214 | }, |
| 215 | ): Promise<PRContext> { |
| 216 | const entry = this.ensureEntry(url, ref); |
| 217 | if (entry.inflight) { |
| 218 | if (options?.queueAfterInflight) entry.refreshAfterInflight = true; |
| 219 | return entry.inflight; |
| 220 | } |
| 221 | |
| 222 | const now = this.now(); |
| 223 | if (!options?.force && now < entry.nextAllowedRefreshAt) { |
| 224 | if (entry.context) return Promise.resolve(entry.context); |
| 225 | return Promise.reject(new Error(entry.error ?? "PR context refresh is paused")); |
| 226 | } |
| 227 | |
| 228 | this.clearScheduledTimer(entry); |
| 229 | this.broadcast(entry, { type: "loading", url, version: entry.version }); |
| 230 | |
| 231 | const promise = Promise.resolve() |
| 232 | .then(() => this.fetchContext(ref)) |
| 233 | .then((context) => { |
| 234 | entry.context = context; |
| 235 | entry.error = null; |
| 236 | entry.lastSuccessAt = this.now(); |
| 237 | entry.nextAllowedRefreshAt = 0; |
| 238 | entry.version += 1; |
| 239 | this.broadcast(entry, { |
| 240 | type: "updated", |
| 241 | url, |
| 242 | version: entry.version, |
| 243 | context, |
| 244 | stale: false, |
| 245 | }); |
| 246 | return context; |
| 247 | }) |
| 248 | .catch((cause: unknown) => { |
| 249 | const message = messageFromUnknown(cause); |
| 250 | const retryAt = this.now() + this.failureCooldownMs; |
| 251 | entry.error = message; |
| 252 | entry.nextAllowedRefreshAt = retryAt; |
| 253 | entry.version += 1; |
| 254 | this.broadcast(entry, { |
| 255 | type: "error", |
| 256 | url, |
| 257 | version: entry.version, |
| 258 | error: message, |
| 259 | stale: entry.context !== null, |
| 260 | retryAt, |
| 261 | }); |
| 262 | throw cause; |
| 263 | }) |
| 264 | .finally(() => { |
no test coverage detected