From fa2817dd59db81b070e5994a85a79714d6c27bb7 Mon Sep 17 00:00:00 2001 From: Dan Homola Date: Sat, 15 Jul 2023 16:53:33 +0200 Subject: [PATCH] fix: add catch to permissions query, avoid bind This was recommended by the now enabled eslint typed rules. --- .eslintrc.json | 16 ++++++++++++++++ src/index.ts | 29 +++++++++++++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 09f4f77b..630912f6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,6 +7,22 @@ "plugin:react-hooks/recommended", "plugin:prettier/recommended" ], + "overrides": [ + { + "files": ["./src/**/*.ts", "./src/**/*.tsx"], + "parserOptions": { + "project": ["./tsconfig.json"] + }, + "extends": [ + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-type-checked", + "plugin:@typescript-eslint/strict", + "plugin:react/recommended", + "plugin:react-hooks/recommended", + "plugin:prettier/recommended" + ] + } + ], "env": { "browser": true, "node": true, diff --git a/src/index.ts b/src/index.ts index 5cc76fc4..c90f2cbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,7 +103,7 @@ export function useGeolocated(config: GeolocatedConfig = {}): GeolocatedResult { const userDecisionTimeoutId = useRef(0); const isCurrentlyMounted = useRef(true); - const watchId = useRef(0); + const watchId = useRef(0); const [isGeolocationEnabled, setIsGeolocationEnabled] = useState( isOptimisticGeolocationEnabled, @@ -160,23 +160,25 @@ export function useGeolocated(config: GeolocatedConfig = {}): GeolocatedResult { throw new Error("The provided geolocation provider is invalid"); } - const funcPosition = ( - watchPosition - ? geolocationProvider.watchPosition - : geolocationProvider.getCurrentPosition - ).bind(geolocationProvider); - if (userDecisionTimeout) { userDecisionTimeoutId.current = window.setTimeout(() => { handlePositionError(); }, userDecisionTimeout); } - watchId.current = funcPosition( - handlePositionSuccess, - handlePositionError, - positionOptions, - ); + if (watchPosition) { + watchId.current = geolocationProvider.watchPosition( + handlePositionSuccess, + handlePositionError, + positionOptions, + ); + } else { + geolocationProvider.getCurrentPosition( + handlePositionSuccess, + handlePositionError, + positionOptions, + ); + } }, [ geolocationProvider, watchPosition, @@ -201,6 +203,9 @@ export function useGeolocated(config: GeolocatedConfig = {}): GeolocatedResult { permission.onchange = () => { setPermissionState(permission.state); }; + }) + .catch((e) => { + console.error("Error updating the permissions", e); }); }