From 0727a228fd8bf9b7dd7ecc5b42d9225eab00bae7 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 30 Sep 2023 15:30:11 +0200 Subject: [PATCH] Fix: fix #1596 --- src/Logic/State/GeoLocationState.ts | 41 +++++++++++++++++------------ 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Logic/State/GeoLocationState.ts b/src/Logic/State/GeoLocationState.ts index 7c6c7b1e7..435002e79 100644 --- a/src/Logic/State/GeoLocationState.ts +++ b/src/Logic/State/GeoLocationState.ts @@ -99,6 +99,15 @@ export class GeoLocationState { * This class will start watching */ public requestPermission() { + this.requestPermissionAsync() + } + + /** + * Requests the user to allow access to their position. + * When granted, will be written to the 'geolocationState'. + * This class will start watching + */ + public async requestPermissionAsync() { if (typeof navigator === "undefined") { // Not compatible with this browser this.permission.setData("denied") @@ -112,23 +121,21 @@ export class GeoLocationState { this.permission.setData("requested") try { - navigator?.permissions - ?.query({ name: "geolocation" }) - .then((status) => { - const self = this - if (status.state === "granted" || status.state === "denied") { - self.permission.setData(status.state) - return - } - status.addEventListener("change", (e) => { - self.permission.setData(status.state) - }) - // The code above might have reset it to 'prompt', but we _did_ request permission! - this.permission.setData("requested") - // We _must_ call 'startWatching', as that is the actual trigger for the popup... - self.startWatching() - }) - .catch((e) => console.error("Could not get geopermission", e)) + const status = await navigator?.permissions?.query({ name: "geolocation" }) + const self = this + console.log("Got geolocation state", status.state) + if (status.state === "granted" || status.state === "denied") { + self.permission.setData(status.state) + self.startWatching() + return + } + status.addEventListener("change", (e) => { + self.permission.setData(status.state) + }) + // The code above might have reset it to 'prompt', but we _did_ request permission! + this.permission.setData("requested") + // We _must_ call 'startWatching', as that is the actual trigger for the popup... + self.startWatching() } catch (e) { console.error("Could not get permission:", e) }