Skip to content

Commit

Permalink
fix: request permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Mar 19, 2023
1 parent 4e9048f commit e5e73b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
19 changes: 16 additions & 3 deletions modules/web-bluetooth/src/BLESourceNode.ts
Expand Up @@ -30,9 +30,22 @@ export class BLESourceNode extends SourceNode<RFDataFrame> {
});
}

requestPermission(): Promise<void> {
return new Promise((resolve) => {
resolve();
static checkPermissions(): Promise<boolean> {
return this.requestPermissions();
}

static requestPermissions(): Promise<boolean> {
return new Promise((resolve, reject) => {
navigator.permissions
.query({ name: 'bluetooth-le-scan' as PermissionName })
.then((result) => {
if (result.state === 'granted') {
resolve(true);
} else {
resolve(false);
}
})
.catch(reject);
});
}

Expand Down
12 changes: 8 additions & 4 deletions modules/web-geolocation/src/GeolocationSourceNode.ts
Expand Up @@ -28,15 +28,19 @@ export class GeolocationSourceNode extends SourceNode<DataFrame> {
this.options.source = this.source ?? new DataObject();
}

requestPermission(): Promise<void> {
static checkPermissions(): Promise<boolean> {
return this.requestPermissions();
}

static requestPermissions(): Promise<boolean> {
return new Promise((resolve, reject) => {
navigator.permissions
.query({ name: 'gelocation' as PermissionName })
.query({ name: 'geolocation' as PermissionName })
.then((result) => {
if (result.state === 'granted') {
resolve();
resolve(true);
} else {
reject(new Error(`No permission to use the geolocation api!`));
resolve(false);
}
})
.catch(reject);
Expand Down
2 changes: 0 additions & 2 deletions modules/web-sensors/src/SensorSourceNodeInterface.ts
@@ -1,8 +1,6 @@
import { SourceNode, DataFrame } from '@openhps/core';

export interface SensorSourceNodeInterface extends SourceNode<DataFrame> {
requestPermission(): Promise<void>;

start(): Promise<void>;

stop(): Promise<void>;
Expand Down
14 changes: 9 additions & 5 deletions modules/web-sensors/src/web/SensorSourceNode.ts
Expand Up @@ -40,10 +40,14 @@ export class SensorSourceNode extends SourceNode<DataFrame> implements SensorSou
this.once('destroy', this.stop.bind(this));
}

requestPermission(): Promise<void> {
static checkPermissions(sensors: SensorType[]): Promise<boolean> {
return this.requestPermissions(sensors);
}

static requestPermissions(sensors: SensorType[]): Promise<boolean> {
return new Promise((resolve, reject) => {
Promise.all(
this.options.sensors
sensors
.map((sensor) =>
this.getPermissions(sensor).map((permission) =>
navigator.permissions.query({ name: permission as PermissionName }),
Expand All @@ -53,9 +57,9 @@ export class SensorSourceNode extends SourceNode<DataFrame> implements SensorSou
)
.then((results) => {
if (results.every((result) => result.state === 'granted')) {
resolve();
resolve(true);
} else {
reject(new Error(`No permission to use the required sensors!`));
resolve(false);
}
})
.catch(reject);
Expand Down Expand Up @@ -218,7 +222,7 @@ export class SensorSourceNode extends SourceNode<DataFrame> implements SensorSou
}
}

protected getPermissions(sensor: new () => SensorObject): string[] {
protected static getPermissions(sensor: new () => SensorObject): string[] {
switch (sensor) {
// case SensorType.AMBIENT_LIGHT:
// return ["ambient-light-sensor"];
Expand Down

0 comments on commit e5e73b6

Please sign in to comment.