Repository navigation
feat: add React hooks API - #48
Merged
Merged
Conversation
Add useHealthConnectAvailability, useHealthConnectPermissions, useReadRecords, useMostRecentRecord, useAggregate and useChanges on top of the imperative API. Hooks refetch on foreground via pollOnForeground, compare arguments by value, accept range factories for now-relative windows, and drop results after unmount or for stale arguments. Documented in README, docs/guides.html, docs/api.html and skills.md.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds typed React hooks on top of the existing imperative API. It's the follow-up to the competitor review:
@kingstinct/react-native-healthkitships hooks, and until now we only haduseForegroundPoll.valueuseHealthConnectAvailability(opts?)'available' | 'notInstalled' | 'notSupported'useHealthConnectPermissions(perms, opts?)[status, request];status.valueis{ granted, missing, allGranted }useReadRecords<T>(type, opts?)ReadRecordsResponse<T>useMostRecentRecord<T>(type, opts?)T | nulluseAggregate(type, range, opts?)AggregateResultuseChanges(token, opts?)PullChangesResultThe data hooks return the same
{ value, satisfied, loading, error, refresh }shape asuseForegroundPolland acceptenabled,ready,timeoutMsandintervalMs.Design notes
pollOnForegroundfromsrc/foreground.ts. Each one fetches on mount and on every resume, and aborts the in-flight fetch when the app goes to the background.JSON.stringifyof the arguments, so inline arrays and objects don't cause refetch loops.timeRangeFilter/rangealso accept a function such as() => timeRangeToday(). The function runs on each fetch, so "today" moves forward on resume, and it is left out of the key. PassingtimeRangeToday()directly would create a new range on every render. The docs andskills.mdwarn about this.ready, each mount or resume does one read. Passingreadyturns on polling through the "0 right after resume" gap.request()asks only for the missing permissions, re-checks them, refreshes the hook and resolves with the result.useChangesdrains pages withpullChanges, then continues fromnextChangesTokenon the next resume. It reports each pull throughonChanges, which is where the caller persists the token. It doesn't advance past an expired token. A newtokenprop restarts the sync, andnullpauses it.>=0.84.0).Docs
docs/guides.html: a new section, 14 "React hooks", with the nav and TOC updated and later sections renumbered. The meta description now mentions React hooks.docs/api.html: a new "React hooks" section.skills.md: when to use the hooks, a table, an example, and a "do not" for range factories.Testing
src/__tests__/hooks.test.tsxadds 22 tests using React test renderer. They cover the resume refetch,enabled, errors and retry, results arriving after unmount, results for stale arguments, refetch on argument change, range factories,readypolling,requestasking only for missing permissions, anduseChangestoken advance, expiry, reset and unmount.yarn lint,yarn typecheckandyarn test:coverageall pass (253 tests).hooks.tscoverage: 100% lines and 94% branches. Global coverage gates pass.