Skip to content

Commit

Permalink
Merge pull request #116 from kingstinct/revert-113-fix/infinite_updat…
Browse files Browse the repository at this point in the history
…e_useStatisticsForQuantity
  • Loading branch information
robertherber authored Nov 13, 2024
2 parents ac33edc + 2bf9475 commit 12a2ba1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Test
# Controls when the action will run.
on:
push:
branches: [ "**" ]
pull_request:
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
Expand Down
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,26 @@ Some imperative examples:
console.log(quantity) // 17.5
console.log(unit) // %

/* Listen to data */
await HealthKit.requestAuthorization([HKQuantityTypeIdentifier.heartRate]); // request read permission for heart rate

/* Make sure to request permissions before subscribing to changes */
const unsubscribe = HealthKit.subscribeToChanges(HKQuantityTypeIdentifier.heartRate, () => {
// refetch whichever queries you need
});
/* Subscribe to data (Make sure to request permissions before subscribing to changes) */
const [hasRequestedAuthorization, setHasRequestedAuthorization] = useState(false);

useEffect(() => {
HealthKit.requestAuthorization([HKQuantityTypeIdentifier.heartRate]).then(() => {
setHasRequestedAuthorization(true);
});
}, []);

useEffect(() => {
if (hasRequestedAuthorization) {
const unsubscribe = HealthKit.subscribeToChanges(HKQuantityTypeIdentifier.heartRate, () => {
// refetch data as needed
});
}

return () => unsubscribe();
}, [hasRequestedAuthorization]);

/* write data */
await HealthKit.requestAuthorization([], [HKQuantityTypeIdentifier.insulinDelivery]); // request write permission for insulin delivery
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useStatisticsForQuantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function useStatisticsForQuantity<TIdentifier extends HKQuantityTypeIdentifier,

useEffect(() => {
void update()
}, [])
}, [update])

useSubscribeToChanges(identifier, update)

Expand Down
2 changes: 1 addition & 1 deletion src/native-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ export enum HKUnits {
Percent = '%',
Count = 'count',
InternationalUnit = 'IU',
AppleEffortScore = "appleEffortScore"
AppleEffortScore = 'appleEffortScore'
}

export type MeterUnit<Prefix extends HKMetricPrefix = HKMetricPrefix.None> =
Expand Down

0 comments on commit 12a2ba1

Please sign in to comment.