From 45476e6f3d7bee79016b3bb76ffae112a1d56d9d Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Fri, 9 Feb 2018 14:33:40 +0200 Subject: [PATCH] Update feature detection example Fixes: #339 --- index.bs | 39 ++++++++++++++++++++++++++++++--------- index.html | 48 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 23 deletions(-) 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:
+ This simple example illustrates how to check whether the User Agent + exposes an interface for a particular sensor type. To handle errors in a robust + manner, please refer to [this example](#robust-example).
         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; } - +