perf(hooks): extract restorePurchases into useCallback for stable reference#3172
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a performance and consistency issue within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactored Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3172 +/- ##
==========================================
+ Coverage 68.90% 68.97% +0.07%
==========================================
Files 9 9
Lines 1791 1792 +1
Branches 584 585 +1
==========================================
+ Hits 1234 1236 +2
+ Misses 552 551 -1
Partials 5 5
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request refactors the restorePurchases function within the useIAP hook by extracting its logic into a memoized useCallback hook. The reviewer has identified a potential redundancy in the restorePurchases implementation, noting that restorePurchasesTopLevel already calls getAvailablePurchases, making the subsequent call to getAvailablePurchasesInternal potentially redundant. An optimization is suggested to avoid duplicate network requests, particularly for iOS by using syncIOS directly.
…Purchases Call syncIOS directly instead of restorePurchasesTopLevel to avoid double network request, since getAvailablePurchasesInternal already fetches available purchases afterward. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
Extracts the inline
restorePurchasesfunction from theuseIAPhook's return statement into a properly memoizeduseCallback.Problem
All other public functions returned by
useIAP(e.g.finishTransaction,requestPurchase,fetchProducts,validateReceipt) are wrapped inuseCallbackto provide a stable reference across renders. However,restorePurchaseswas defined as an inlineasync () => {}directly in the return statement, causing it to be recreated on every render.This inconsistency means:
restorePurchasesas a prop and use it in auseEffectoruseCallbackdependency array will trigger unnecessary re-runsreact-hooks/exhaustive-deps) can flag its usage since it's not stableChanges
restorePurchasesinto auseCallbackwith appropriate dependencies[getAvailablePurchasesInternal, invokeOnError]// No local restorePurchases; use the top-level helper via returned APIrestorePurchasesvariableWhy
Improves performance consistency and aligns
restorePurchaseswith the rest of the hook's API design.Summary by CodeRabbit