Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap perf.now calls to avoid stale mock impls #378

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
*/

// module-private names and types
type Perf = { now: () => number }
const perf: Perf =
const getPerfNow: () => number =
typeof performance === 'object' &&
performance &&
typeof performance.now === 'function'
? performance
: Date
? () => performance.now()
: () => Date.now()

const warned = new Set<string>()

Expand Down Expand Up @@ -1479,7 +1478,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
this.#ttls = ttls
this.#starts = starts

this.#setItemTTL = (index, ttl, start = perf.now()) => {
this.#setItemTTL = (index, ttl, start = getPerfNow()) => {
starts[index] = ttl !== 0 ? start : 0
ttls[index] = ttl
if (ttl !== 0 && this.ttlAutopurge) {
Expand All @@ -1498,7 +1497,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
}

this.#updateItemAge = index => {
starts[index] = ttls[index] !== 0 ? perf.now() : 0
starts[index] = ttls[index] !== 0 ? getPerfNow() : 0
}

this.#statusTTL = (status, index) => {
Expand All @@ -1519,7 +1518,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
// that costly call repeatedly.
let cachedNow = 0
const getNow = () => {
const n = perf.now()
const n = getPerfNow()
if (this.ttlResolution > 0) {
cachedNow = n
const t = setTimeout(
Expand Down Expand Up @@ -1915,7 +1914,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
const ttl = this.#ttls[i]
const start = this.#starts[i]
if (ttl && start) {
const remain = ttl - (perf.now() - start)
const remain = ttl - (getPerfNow() - start)
entry.ttl = remain
entry.start = Date.now()
}
Expand Down Expand Up @@ -1953,7 +1952,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
entry.ttl = this.#ttls[i]
// always dump the start relative to a portable timestamp
// it's ok for this to be a bit slow, it's a rare operation.
const age = perf.now() - (this.#starts[i] as number)
const age = getPerfNow() - (this.#starts[i] as number)
entry.start = Math.floor(Date.now() - age)
}
if (this.#sizes) {
Expand Down Expand Up @@ -1984,7 +1983,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
//
// it's ok for this to be a bit slow, it's a rare operation.
const age = Date.now() - entry.start
entry.start = perf.now() - age
entry.start = getPerfNow() - age
}
this.set(key, entry.value, entry)
}
Expand Down