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

Fix a race condition bug #261

Merged
merged 1 commit into from Feb 14, 2020
Merged

Fix a race condition bug #261

merged 1 commit into from Feb 14, 2020

Conversation

shuding
Copy link
Member

@shuding shuding commented Feb 14, 2020

await CONCURRENT_PROMISES[key] should be called before deduping. Because there could be this potential bug, say dedupingInterval is 2s:

0s: A ... GET /api
2s: delete CONCURRENT_PROMISES[key]
3s: B ... GET /api
4s: B -> response
5s: A -> response

Which is a race condition, the request fired first got returned last.

(Also added a test for mutate in this PR)

@shuding shuding merged commit 696bc5c into master Feb 14, 2020
@shuding shuding deleted the fix-deduping branch February 14, 2020 20:32
shuding added a commit that referenced this pull request Feb 19, 2020
* Update README.md (#235)

* test(typo): fix typo (#244)

* fix weakmap key is `null` (#251)

* fix race condition and add test (#261)

* 0.1.17

* Add regression test for weakmap key is `null`

This add a regression test for the fix of #251

Co-authored-by: Shu Ding <ds303077135@gmail.com>
Co-authored-by: Darren Jennings <dmjenn02@gmail.com>
shuding added a commit that referenced this pull request Feb 24, 2020
* Improve mutate function

- If no data call trigger against the key
- If data is a function call it passing current cached value
- Return data or throw error after function

* useSWR now returns a bound mutate call with key predefined

* Remove trick to make shouldRevalidate true only with local mutation

Also update related test to manually pass shouldRevalidate as false

* Add mutate to list of returned values from useSWR

* Replace trigger with mutate

* Add Mutate Based on Current Data example

* Add Returned Data from Mutate example

* Update Optimistic UI example

* Update README.md (#235)

* test(typo): fix typo (#244)

* fix weakmap key is `null` (#251)

* fix race condition and add test (#261)

* 0.1.17

* Apply changes

* Update src/use-swr.ts

* Update src/use-swr.ts

* Apply suggestions from code review

Co-authored-by: Shu Ding <ds303077135@gmail.com>
Co-authored-by: Darren Jennings <dmjenn02@gmail.com>
shuding added a commit that referenced this pull request Feb 24, 2020
* Add CacheInterface type

* Create defautl cache and use it in the code

* Add test to custom cache support

* Replace custom cache with exposed cache

* Fix error in comment

* Update README.md (#235)

* test(typo): fix typo (#244)

* fix weakmap key is `null` (#251)

* fix race condition and add test (#261)

* 0.1.17

* Move getKeyArgs and getErrorKey to serializeKey in Cache

* Make the Map instance in Cache private

* Add methods to subscribe to cache updates and get all keys

* Apply suggestions from code review

Co-Authored-By: Shu Ding <ds303077135@gmail.com>

* Don't ignore tests

* Cache methods receive keyInterface and serialize it if it's not a string

* Don't expose Cache class anymore

Co-authored-by: Shu Ding <ds303077135@gmail.com>
Co-authored-by: Darren Jennings <dmjenn02@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants