mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2025-04-16 04:22:02 +00:00
38 lines
875 B
JavaScript
38 lines
875 B
JavaScript
class RequestHistory {
|
|
/**
|
|
* @param {string[]} tracked list of things that can be tracked
|
|
*/
|
|
constructor(tracked) {
|
|
this.tracked = new Set(tracked)
|
|
/** @type {Map<string, {lastRequestAt: number | null, lastRequestSuccessful: boolean | null}>} */
|
|
this.store = new Map()
|
|
for (const key of tracked) {
|
|
this.store.set(key, {
|
|
lastRequestAt: null,
|
|
lastRequestSuccessful: null
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {boolean} success
|
|
*/
|
|
report(key, success) {
|
|
if (!this.tracked.has(key)) throw new Error(`Trying to report key ${key}, but is not tracked`)
|
|
const entry = this.store.get(key)
|
|
entry.lastRequestAt = Date.now()
|
|
entry.lastRequestSuccessful = success
|
|
}
|
|
|
|
export() {
|
|
const result = {}
|
|
for (const key of this.store.keys()) {
|
|
result[key] = this.store.get(key)
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
module.exports = RequestHistory
|