Skip to content

Commit b6c6b3a

Browse files
committed
perf: simplify cache hit flow
1 parent 767ab1a commit b6c6b3a

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/main/js/cache.js

+12-25
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,20 @@ export const isNoCache = () => getCache() === voidCache
4040

4141
export const withCache = (name, cb, ttl) => {
4242
if (!hits.has(name)) {
43-
(() => {
44-
let p
45-
46-
hits.set(name, async () => {
47-
if (p) {
48-
return p
49-
}
50-
51-
p = (async () => {
52-
const cache = getCache()
53-
if (await cache.has(name)) {
54-
return cache.get(name)
55-
}
56-
57-
const value = await cb()
58-
await cache.add(name, value, ttl)
59-
60-
p = null
61-
return value
62-
})().finally(() => hits.delete(name))
63-
64-
return p
65-
})
66-
})()
43+
hits.set(name, (async () => {
44+
const cache = getCache()
45+
if (await cache.has(name)) {
46+
return cache.get(name)
47+
}
48+
49+
const value = await cb()
50+
await cache.add(name, value, ttl)
51+
52+
return value
53+
})().finally(() => hits.delete(name)))
6754
}
6855

69-
return hits.get(name)()
56+
return hits.get(name)
7057
}
7158

7259
export const createCache = ({ttl, evictionTimeout = ttl, warmup, limit = Infinity}) => {

src/main/js/worker/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ const processQueue = () => {
4646
}
4747
concurrency -= 1
4848
processQueue()
49+
worker.unref()
4950
})
5051
}

0 commit comments

Comments
 (0)