Skip to content

Commit

Permalink
Fix cache saving during parallel run
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 13, 2019
1 parent ab84645 commit 05e95b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/time/get-running-time.js
Expand Up @@ -43,10 +43,15 @@ async function getThrottling () {
}

let throttlingCache
let throttlingCalculating

module.exports = async function getRunningTime (file) {
if (!JS_FILES.test(file)) return 0
if (!throttlingCache) throttlingCache = await getThrottling()
if (throttlingCalculating) await throttlingCalculating
if (!throttlingCache) {
throttlingCalculating = getThrottling()
throttlingCache = await throttlingCalculating
}
return getTime(file, throttlingCache)
}

Expand Down
10 changes: 10 additions & 0 deletions packages/time/test/get-running-time.test.js
Expand Up @@ -34,3 +34,13 @@ it('uses cache', async () => {
it('ignores non-JS files', async () => {
expect(await getRunningTime('/a.jpg')).toEqual(0)
})

it('works in parallel', async () => {
let times = await Promise.all([
getRunningTime(EXAMPLE),
getRunningTime(EXAMPLE),
getRunningTime(EXAMPLE),
getRunningTime(EXAMPLE)
])
expect(times).toHaveLength(5)
})

0 comments on commit 05e95b8

Please sign in to comment.