mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-11-22 08:07:30 +00:00
Fix simultaneous accesses to inflight RequestCache
This commit is contained in:
parent
43c2d66b04
commit
78cefb0776
@ -92,7 +92,7 @@ class TtlCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends TtlCache<T>
|
* @extends TtlCache<Promise<T>>
|
||||||
* @template T
|
* @template T
|
||||||
*/
|
*/
|
||||||
class RequestCache extends TtlCache {
|
class RequestCache extends TtlCache {
|
||||||
@ -110,14 +110,12 @@ class RequestCache extends TtlCache {
|
|||||||
*/
|
*/
|
||||||
getOrFetch(key, callback) {
|
getOrFetch(key, callback) {
|
||||||
this.cleanKey(key)
|
this.cleanKey(key)
|
||||||
if (this.cache.has(key)) return Promise.resolve({result: this.get(key), fromCache: true})
|
if (this.cache.has(key)) {
|
||||||
else {
|
return this.getWithoutClean(key).then(result => ({result, fromCache: true}))
|
||||||
const pending = callback().then(result => {
|
} else {
|
||||||
this.set(key, result)
|
const pending = callback()
|
||||||
return {result, fromCache: false}
|
|
||||||
})
|
|
||||||
this.set(key, pending)
|
this.set(key, pending)
|
||||||
return pending
|
return pending.then(result => ({result, fromCache: false}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,19 +400,12 @@ function fetchShortcodeData(shortcode) {
|
|||||||
const p = new URLSearchParams()
|
const p = new URLSearchParams()
|
||||||
p.set("query_hash", constants.external.shortcode_query_hash)
|
p.set("query_hash", constants.external.shortcode_query_hash)
|
||||||
p.set("variables", JSON.stringify({shortcode}))
|
p.set("variables", JSON.stringify({shortcode}))
|
||||||
let status
|
|
||||||
return requestCache.getOrFetchPromise("shortcode/"+shortcode, () => {
|
return requestCache.getOrFetchPromise("shortcode/"+shortcode, () => {
|
||||||
return switcher.request("post_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => {
|
return switcher.request("post_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => {
|
||||||
status = res.status
|
|
||||||
if (res.status === 429) throw constants.symbols.RATE_LIMITED
|
if (res.status === 429) throw constants.symbols.RATE_LIMITED
|
||||||
}).then(res => res.json()).then(root => {
|
}).then(res => res.json()).then(root => {
|
||||||
/** @type {import("./types").TimelineEntryN3} */
|
/** @type {import("./types").TimelineEntryN3} */
|
||||||
const data = root.data.shortcode_media
|
const data = root.data.shortcode_media
|
||||||
if (data && !data.__typename) { // empty data? that's really weird.
|
|
||||||
console.error(`Shortcode request ${shortcode} gave empty data object. status: ${status}, root:`)
|
|
||||||
console.error(root)
|
|
||||||
console.error("will just proceed to crash as normal.")
|
|
||||||
}
|
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
// the thing doesn't exist
|
// the thing doesn't exist
|
||||||
throw constants.symbols.NOT_FOUND
|
throw constants.symbols.NOT_FOUND
|
||||||
|
Loading…
Reference in New Issue
Block a user