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

Tweaks #1

Merged
merged 2 commits into from
Mar 22, 2024
Merged

Conversation

haines
Copy link

@haines haines commented Mar 22, 2024

I started out with a couple of performance optimisations:

  • for the deep equality check
    • check === first in case they are reference-equal, which also lets us remove some comparisons if either of the values are primitive or null
    • handle arrays with a for loop, which avoids allocating Object.entries for the array (which is potentially expensive)
    • handle objects with Object.keys, which is cheaper than Object.entries.
  • use useDeepCompareMemoize for the previous state, which uses an incrementing number as a signal to trigger rerenders (apparently this is faster 🤷‍♂️ faster kentcdodds/use-deep-compare-effect#37, fix: effect not called with getDerivedStateFromProps kentcdodds/use-deep-compare-effect#42)
  • also memoize principal and auxData in the provider

On balance I then felt like we should just add a dependency on use-deep-compare-effect; it's stable, tiny, fairly widely used, and depends only on dequal, which is also stable, tiny, and very widely used. I feel like the benefit of not having to test that functionality outweighs the drawbacks of adding a dependency, especially because neither of those packages have needed a release for over 2 years. This did mean we had to bump our minimum supported React version to be in line with that of use-deep-compare-effect, but I don't think that's too bad.

I wasn't able to eliminate the ts-expect-error - the problem is something I've run into repeatedly in this SDK. There's a good summary of it here.

I tweaked the AsyncResult type to allow type narrowing (if you check for loading, and then error, you don't still have to then check whether data is undefined). I also added an aborted flag so that we don't update the state if a new effect has been triggered since we started, or the component has unmounted. In the future we might want to add signal: AbortSignal to the RequestOptions type to support actually aborting in-flight requests in the gRPC and HTTP clients, but that's beyond the scope for now I reckon!

@haines haines force-pushed the feat/init-react-sdk branch 2 times, most recently from 3040849 to 6900baf Compare March 22, 2024 07:49
Signed-off-by: Andrew Haines <haines@cerbos.dev>
Signed-off-by: Andrew Haines <haines@cerbos.dev>
Copy link
Owner

@hasanayan hasanayan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇🏻

packages/react/src/use-cerbos-request.ts Show resolved Hide resolved
packages/react/src/use-cerbos-request.ts Show resolved Hide resolved
packages/react/src/use-cerbos-request.ts Show resolved Hide resolved
@hasanayan hasanayan merged commit ca06f9a into hasanayan:feat/init-react-sdk Mar 22, 2024
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.

2 participants