From 12798c23aa443c1a386216a7833bcfff19973c29 Mon Sep 17 00:00:00 2001 From: Cadence Fish Date: Fri, 14 Feb 2020 23:28:13 +1300 Subject: [PATCH] this could really do with tests --- src/lib/cache.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/cache.js b/src/lib/cache.js index cf5a26c..5524980 100644 --- a/src/lib/cache.js +++ b/src/lib/cache.js @@ -161,7 +161,23 @@ class UserRequestCache extends TtlCache { this.cleanKey(key) if (this.cache.has(key)) { const existing = this.cache.get(key) - if ((!existing.isReel || !isHtmlPreferred || existing.htmlFailed) && !existing.isFailedPromise) return Promise.resolve(existing.data) + if (!existing.isFailedPromise) { // if the existing entry contains usable data + if (!existing.isReel) { // hurrah, the best we could get! + return Promise.resolve(existing.data) + } + // we don't have HTML, only reel + if (!isHtmlPreferred) { // well that's cool, we only wanted reel anyway + return Promise.resolve(existing.data) + } else { // (isHtmlPreferred ~= true): we'd _like_ some HTML, but we don't have it currently. if HTML is blocked then using reel is smart + if (existing.htmlFailed) { // HTML is in fact blocked, so we will have to settle for reel. fortunately we already have reel! + return Promise.resolve(existing.data) + } + } + } else { // (existing.isFailedPromise ~= true): the existing entry is a failed request + if (existing.htmlFailed && !willFetchReel) { // it's no use! the HTML attempt will fail again: abandon it. + return Promise.resolve(existing.data) // this is actually a promise rejection + } + } } const pending = callback().then(result => { if (this.getWithoutClean(key) === pending) { // if nothing has replaced the current cache in the meantime