diff --git a/index.bs b/index.bs index a9e79e9..7b67a97 100644 --- a/index.bs +++ b/index.bs @@ -202,6 +202,9 @@ but rather to put it in the context of detecting hardware-dependent features. Consider the below feature detection examples:
if (typeof Gyroscope === "function") { // run in circles... @@ -249,20 +252,38 @@ and defensive programming which includes: enhanced by the possible usage of a sensor, not degraded by its absence. -++ The following snippet illustrates how an Accelerometer sensor can be created + in a robust manner. Web application may choose different options for error + handling, for example, show notification, choose different sensor type or + fallback to other API.-- try { // No need to feature detect thanks to try..catch block. - var sensor = new GeolocationSensor(); - sensor.start(); - sensor.onerror = error => gracefullyDegrade(error); - sensor.onreading = _ => updatePosition(sensor.latitude, sensor.longitude); - } catch(error) { - gracefullyDegrade(error); + let accelerometer = null; + try { + accelerometer = new Accelerometer({ frequency: 10 }); + accelerometer.addEventListener('error', event => { + // Handle runtime errors. + if (event.error.name === 'NotAllowedError') { + console.log('Permission to access sensor was denied.'); + } else if (event.error.name === 'NotReadableError' ) { + console.log('Cannot connect to the sensor.'); + } + }); + accelerometer.addEventListener('reading', () => reloadOnShake(accelerometer)); + accelerometer.start(); + } catch (error) { + // Handle construction errors. + if (error.name === 'SecurityError') { + console.log('Sensor construction was blocked by the Feature Policy.'); + } else if (error.name === 'ReferenceError') { + console.log('Sensor is not supported by the User Agent.'); + } else { + throw error; + } }Security and privacy considerations
[=sensor readings|Sensor readings=] are sensitive data and could become a subject of diff --git a/index.html b/index.html index 9c78fb1..dbd0f3a 100644 --- a/index.html +++ b/index.html @@ -1183,7 +1183,7 @@ background-attachment: fixed; } - +