Select Git revision
index.mjs 33.14 KiB
const perf =
typeof performance === 'object' &&
performance &&
typeof performance.now === 'function'
? performance
: Date
const hasAbortController = typeof AbortController === 'function'
// minimal backwards-compatibility polyfill
// this doesn't have nearly all the checks and whatnot that
// actual AbortController/Signal has, but it's enough for
// our purposes, and if used properly, behaves the same.
const AC = hasAbortController
? AbortController
: class AbortController {
constructor() {
this.signal = new AS()
}
abort(reason = new Error('This operation was aborted')) {
this.signal.reason = this.signal.reason || reason
this.signal.aborted = true
this.signal.dispatchEvent({
type: 'abort',
target: this.signal,
})
}
}
const hasAbortSignal = typeof AbortSignal === 'function'
// Some polyfills put this on the AC class, not global
const hasACAbortSignal = typeof AC.AbortSignal === 'function'
const AS = hasAbortSignal
? AbortSignal
: hasACAbortSignal
? AC.AbortController
: class AbortSignal {
constructor() {
this.reason = undefined
this.aborted = false
this._listeners = []
}
dispatchEvent(e) {
if (e.type === 'abort') {
this.aborted = true
this.onabort(e)
this._listeners.forEach(f => f(e), this)
}
}
onabort() {}
addEventListener(ev, fn) {
if (ev === 'abort') {
this._listeners.push(fn)
}
}
removeEventListener(ev, fn) {
if (ev === 'abort') {
this._listeners = this._listeners.filter(f => f !== fn)
}
}
}
const warned = new Set()
const deprecatedOption = (opt, instead) => {
const code = `LRU_CACHE_OPTION_${opt}`
if (shouldWarn(code)) {
warn(code, `${opt} option`, `options.${instead}`, LRUCache)
}
}
const deprecatedMethod = (method, instead) => {