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

useMutableSource hook #18000

Merged
merged 27 commits into from
Mar 11, 2020
Merged

useMutableSource hook #18000

merged 27 commits into from
Mar 11, 2020

Commits on Mar 10, 2020

  1. useMutableSource hook

    useMutableSource() enables React components to safely and efficiently read from a mutable external source in Concurrent Mode. The API will detect mutations that occur during a render to avoid tearing and it will automatically schedule updates when the source is mutated.
    
    RFC: reactjs/rfcs#147
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    0eb26dd View commit details
    Browse the repository at this point in the history
  2. Refactored useMutableSource to use update queue

    This commit changes useMutableSource to use an update queue by composing useEffect and useState internally. This ended up being a little bigger than I would have expected due to the need to create mount, update, and rerender implementations, but that's probably okay for now.
    
    This commit also makes a few orthogonal changes as the result of discussions with Relay, Redux, etc:
    1. Snapshots are eagerly evaluated so components can bailout early even when 'scoped subscriptions' are not possible.
    2. The subscribe callback function now expects to be passed the updated snapshot value.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    8e751b2 View commit details
    Browse the repository at this point in the history
  3. Changed subscribe callback signature to not require the latest snapsh…

    …ot value
    
    This is being done to reduce how frequently we potentially deopt during render.
    
    I also tidied up the useMutableSource mount/update/rerender implementations to reduce their overall file size.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    06a571e View commit details
    Browse the repository at this point in the history
  4. Handle errors that occur during eager snapshot evaluation

    A selector might throw after a source mutation. e.g. it might try to read from a part of the store that no longer exists. In this case we should still schedule an update with React. Worst case the selector will throw again and then an error boundary will handle it.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    2caa4e9 View commit details
    Browse the repository at this point in the history
  5. Fixed a typo/bug in setState updater function

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    6263c97 View commit details
    Browse the repository at this point in the history
  6. Avoid deopt on changed getSnapshot function unless snapshot also changes

    getSnapshot should be memoized to only change when its inputs change, but if that memoization is done incorrectly (or if the new snapshot just happens to be the same regardless of a changed dependency) then we can avoid deopting to a root re-render.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    905a9c0 View commit details
    Browse the repository at this point in the history
  7. Read mutable source composed hooks from current dispatcher

    Rather than passing them in explicitly. This removes the extra function call.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    112ace4 View commit details
    Browse the repository at this point in the history
  8. Split useMutableSource tests into separate suite

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    6d1dd6a View commit details
    Browse the repository at this point in the history
  9. Refactor useMutableSource to be more efficient

    Remove unnecessary useRef in favor of hook state.
    
    Directly mutate composed state hook queue to reset in the case of a new subscribe or source.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    73b2628 View commit details
    Browse the repository at this point in the history
  10. Added createMutableSource and useMutableSource exports to new ES entr…

    …ypoints
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    5f37cba View commit details
    Browse the repository at this point in the history
  11. Added React debug tools test for new hook

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    8f16725 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    8596425 View commit details
    Browse the repository at this point in the history
  13. Use a second effect to sync getSnapshot and stateHook values on commit

    This prevents the subscription callback from closing over stale values.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    bf9025b View commit details
    Browse the repository at this point in the history
  14. Always reset state queue when getSnapshot changes

    Even if the current snapshot value is the same, there may be pending, lower priority updates that we no longer want to eventually render.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    38a2044 View commit details
    Browse the repository at this point in the history
  15. Always treat reads from source as unsafe when getSnapshot changes

    I initially thought that we could treat them as safe if the returned snapshot value was the same, but this ignored the case where the underlying source was mutated between when the state update was scheduled and when the component later rendered with a new getSnapshot function. (This commit includes a test that failed without this fix.)
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    bf8949e View commit details
    Browse the repository at this point in the history
  16. Pass underlying source to getVersion fn

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    f231a4b View commit details
    Browse the repository at this point in the history
  17. Removed unused 'isMount' param from uMS

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    0c61fed View commit details
    Browse the repository at this point in the history
  18. Misc. cleanup

    1. Reset mutable source WIP version in complete and unwind work (rather than prepare fresh stack).
    2. Add check to warn about multiple primary/secondary renderers using the same mutable source. (Patterned after our current context warning.)
    3. Changed the way I'm calculating expiration time for mutated sources to use computeExpirationForFiber().
    4. Changed undefined unsubscribe function from invariant to warning.
    5. Replaced !== checks with Object.is() checks to be more consistent with precedent.
    6. Added a couple of new warning tests. (One of them has a pending TODO.)
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    160b0a5 View commit details
    Browse the repository at this point in the history
  19. Whitespace only change

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    7327787 View commit details
    Browse the repository at this point in the history
  20. Reenabled a pending disabled mutable source test

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    9c6dd03 View commit details
    Browse the repository at this point in the history
  21. Removed createMutableSource and useMutableSource exports from React s…

    …table release
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    cd2ef30 View commit details
    Browse the repository at this point in the history
  22. Defer getWorkInProgressRoot() call unless root is needed

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    153a8f1 View commit details
    Browse the repository at this point in the history
  23. Misc. cleanup

    1. Use currentlyRenderingFiber to calculate expiration time rather than using a ref.
    2. Use polyfilled is() rather than Object.is()
    3. Add __EXPERIMENTAL__ guard to test since new APIs aren't in stable build
    4. Removed error code that was changed to warning.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    293aa74 View commit details
    Browse the repository at this point in the history
  24. Configuration menu
    Copy the full SHA
    aa46144 View commit details
    Browse the repository at this point in the history
  25. Added an additional muti renderer test

    If a mutation happens between the first and second renderer to use a source, we need to explicitly reset the source before restarting the second renderer.
    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    0ef4bdb View commit details
    Browse the repository at this point in the history
  26. Added new failing tests (pending a separate fix to React)

    Brian Vaughn committed Mar 10, 2020
    Configuration menu
    Copy the full SHA
    45ed506 View commit details
    Browse the repository at this point in the history

Commits on Mar 11, 2020

  1. Merged master and resolved useEvent conflicts

    I am being a little lazy here and merging instead of rebasing because practically every commit conflicted with Dominic's recent useEvent PR. If anyone feel strongly about this, I will revert the commit and rebase. Since the commit will be squashes away during the merge though, I don't think it matters much at this point.
    Brian Vaughn committed Mar 11, 2020
    Configuration menu
    Copy the full SHA
    dee1164 View commit details
    Browse the repository at this point in the history