[generic-sensor] - Sensors API - core

Hi all.

This is, as I mentioned in a different e-mail, a second thought about
Sensors API.

This is more like a question, actually.
Why we don't have (or, have we, and I am not aware of it?) a core for the
Sensors API?

The problem:

window.addEventListener('deviceorientation', ... ) // event
navigator.battery.level; // property in navigator
window.addEventListener('deviceproximity', ... ) // event
navigator.getUserMedia( ... ) // method from navigator
window.addEventListener('devicelight', ... ) // event
navigator.geolocation.getCurrentPosition( ... ) // property in navigator

I believe in an "*intuitive web"*.
I think I should be able to type "navigator.sensors" in my console and see
even an autocomplete with the possibilities.

Something like (better defined afterwards, just simplified here):

navigator.sensors.orientation.on('change', ...)
navigator.sensors.battery.on('change', ...)
navigator.sensors.proximity.on('change', ...)
navigator.sensors.light.on('change', ...)
navigator.sensors.geolocation.on('change', ...)

With such a pattern, it would be much more intuitive and would also provide
a "model" for new sensors in the future.

Getting into details...
I know it could cost too much on processing time or use a lot of battery,
so what I would actually envision would be:

navigator.sensors.geolocation().then(geoObj=>{
    console.log(getObj.status);
    geoObj.addEventListener('change', ... );
    geoObj.addEventListener('lostSignal', ... );
    geoObj.close();
});

All the sensors would be promises.
These promises would resolve to "*SensorObject"*s.
Each Sensor would be activated/started only when the promise was triggered
the first time in a given page.
SensorObject has a status with its current values (set with its initial
states during the Promise resolution).
SensorObjects can be closed (equivalent to call removeEventListener for all
existing listeners for that instance). If no other script is using the same
sensor, the page will no longer need to keep retrieving information from it.
The promise will reject (with a message explaining why) if navigator has no
support for the sensor, if the device does not offer that sensor, or if
user denied access (for the sensors that expect user approval).

With this, we would also be able to progressively detect features:

if ('light' in navigator.sensors) { ... }

I don't see *vibrate api* or *camera* as Sensors...they would stay as they
are.

If you have already discussed some similar approach (or maybe even
discarded it), could you let me know why or how?
Of course, the "sensors" could have a different name, to avoid any possible
collisions.

Thank you very much.
[ ]s

*--*

*Felipe N. Moura*
Web Developer, Google Developer Expert
<https://developers.google.com/experts/people/felipe-moura>, Founder of
BrazilJS <https://braziljs.org/> and Nasc <http://nasc.io/>.

Website:  http://felipenmoura.com / http://nasc.io/
Twitter:    @felipenmoura <http://twitter.com/felipenmoura>
Facebook: http://fb.com/felipenmoura
LinkedIn: http://goo.gl/qGmq
---------------------------------
*Changing  the  world*  is the least I expect from  myself!

Received on Thursday, 24 November 2016 04:39:19 UTC