fix(disposable/deduped): race condition when disposed during acquisition

Introduced in 43aad3d11
This commit is contained in:
Julien Fontanet
2021-03-04 10:46:03 +01:00
parent 43aad3d117
commit a4b209c654
2 changed files with 14 additions and 1 deletions

View File

@@ -29,7 +29,6 @@ exports.deduped = (factory, keyFn = (...args) => args) =>
}
return () => {
++state.users
return wrapper
}
}
@@ -47,6 +46,7 @@ exports.deduped = (factory, keyFn = (...args) => args) =>
states.set(keys, state)
}
++state.users
return state.factory()
}
})()

View File

@@ -60,4 +60,17 @@ describe('deduped()', () => {
expect(dispose).toHaveBeenCalledTimes(1)
})
it('no race condition on dispose before async acquisition', async () => {
const dispose = jest.fn()
const dedupedGetResource = deduped(async () => ({ value: 42, dispose }))
const d1 = await dedupedGetResource()
dedupedGetResource()
d1.dispose()
expect(dispose).not.toHaveBeenCalled()
})
})