Skip to content

Commit

Permalink
fix: add catch to permissions query, avoid bind
Browse files Browse the repository at this point in the history
This was recommended by the now enabled eslint typed rules.
  • Loading branch information
no23reason committed Jul 15, 2023
1 parent 4ad9cff commit fa2817d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 17 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function useGeolocated(config: GeolocatedConfig = {}): GeolocatedResult {

const userDecisionTimeoutId = useRef(0);
const isCurrentlyMounted = useRef(true);
const watchId = useRef<number | void>(0);
const watchId = useRef<number>(0);

const [isGeolocationEnabled, setIsGeolocationEnabled] = useState(
isOptimisticGeolocationEnabled,
Expand Down Expand Up @@ -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,
Expand All @@ -201,6 +203,9 @@ export function useGeolocated(config: GeolocatedConfig = {}): GeolocatedResult {
permission.onchange = () => {
setPermissionState(permission.state);
};
})
.catch((e) => {
console.error("Error updating the permissions", e);
});
}

Expand Down

0 comments on commit fa2817d

Please sign in to comment.