Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the enum SensorState have nosensor ? #104

Closed
riju opened this issue May 9, 2016 · 8 comments
Closed

Should the enum SensorState have nosensor ? #104

riju opened this issue May 9, 2016 · 8 comments

Comments

@riju
Copy link
Contributor

riju commented May 9, 2016

@timvolodine asked : should there be something corresponding to 'there is no relevant hardware
sensor'?

See #104 (comment) for resolutions.

@riju
Copy link
Contributor Author

riju commented May 9, 2016

I think "errored" state suffices as of now, till we work on the Discovery part (with remote sensors) in more detail in v2.
irrc @tobie said that if the api-consumers want to know more, they should listen to the onerror event and get additional information from it.

So, idle -> errored, when there is no relevant hardware sensor.

@tobie
Copy link
Member

tobie commented May 9, 2016

Given we're focusing on local sensors only at this point, the absence or presence of sensors should be know upfront. In this case, the constructor would throw (this is already specified in the spec). If there are concrete cases where sensors go MIA, then we can report this with an error event. Is this something that's already showing up during implementation, @riju?

@anssiko
Copy link
Member

anssiko commented May 16, 2016

It might not be possible to implement the constructor in a non-blocking manner if we require implementations to throw a TypeError if there's no sensor, as spec'd in the Construct Sensor Object:

5. Otherwise, throw a TypeError.

Figuring out whether there's underlying sensor hardware requires crossing process boundary (at least in Blink, and I guess similarly in other modern engines) and sync IPC should be avoided.

That said, @riju and @alexshalamov will experiment with Blink to see if this implementation feedback should motivate spec changes and will update this issue accordingly.

@timvolodine
Copy link

Correct, in chrome it would be impossible to do the "sensor present" check upon construction in a non-blocking way, and we should not make any blocking calls for this anyway. Pre-checking sensor upon startup seems too much overhead. So I think "errored" state kind of makes sense. Maybe there should be enums for "errors" though.. "relevant sensor(s) not available" is a pretty common case and it would probably make sense for the developers to test that quickly.

@tobie
Copy link
Member

tobie commented May 16, 2016

OK, I somehow assumed that a startup pre-check would be standard and cheap.

I'll revamp [[Construct Sensor Object]] accordingly and see what incidences, if any, this has on requesting user permission.

I was planning on using the NotReadableError for cases where sensors went MIA. Do you think there should be something more specific here?

@timvolodine
Copy link

I guess the first start() invocation will trigger user permission when necessary.
Would it be reasonable in the MIA case: to invoke the onerror callback and implicitly stop the sensors?

@tobie
Copy link
Member

tobie commented May 20, 2016

Oh, that's interesting. I had imagined user permission would be triggered during construction (and would fire an "error" event should permission not be granted).

Might make sense to tie it to .start() instead. Especially if .start() return promises (pending #94).

The other option is for construction to trigger a sensor presence check and fire an error if it's missing.

@tobie
Copy link
Member

tobie commented Sep 20, 2016

We're converging on:

  • dropping usage of promise for start() and stop() methods,
  • removing the onstatechange event handler,
  • turning the SensorState enum into slots (or similar unexposed thingies),
  • renaming the "active" state into "activated",
  • adding an onactivate handler which is called when the state transitions from "activating" to "activated".
interface Sensor : EventTarget {
  readonly attribute SensorReading? reading;
  void start();
  void stop();
  attribute EventHandler onchange;
  attribute EventHandler onactivate;
  attribute EventHandler onerror;
};

In order to come to the above conclusions, we experimented with the following code examples:

Promise based

The key issue here is you still need to register an error handler here to catch errors after the start promise gets resolved, but you must either remove duplicate error messages, or only setup the handler after promise completion.

let g = new Gyroscope({frequency: 60});
function loop() {
    console.log(g.reading);
    requestAnimationFrame(loop)
}
g.start().then(_ => requestAnimationFrame(loop), err => {
    if (err.name == "NotReadableError")
        alert("you don't have a gyro!");
    else if (err.name == "NotAllowedError")
        alert("Give me the gyro!");
});

With await

Same lack of post promise resolve error handling.

async function run() {
  let g = new Gyroscope({frequency: 60});
  function loop() {
    console.log(g.reading);
    requestAnimationFrame(loop)
  }
  try {
    await g.start();
    requestAnimationFrame(loop);
  } catch(err) {
    if (err.name == "NotReadableError")
        alert("you don't have a gyro!");
    else if (err.name == "NotAllowedError")
        alert("Give me the gyro!");
  }
}

Event based (current solution)

let g = new Gyroscope({frequency: 60});
function loop() {
    console.log(g.reading);
    requestAnimationFrame(loop)
}

g.onstatechange = _ => {
    if (g.state == "active")
        requestAnimationFrame(loop);
}

g.onerror = err => {
    if (err.name == "NotReadableError")
        alert("you don't have a gyro!");
    else if (err.name == "NotAllowedError")
        alert("Give me the gyro!");
}

g.start();

With a single "activate" event (preferred solution).

let g = new Gyroscope({frequency: 60});
function loop() {
    console.log(g.reading);
    requestAnimationFrame(loop)
}

g.onactivate = _ => requestAnimationFrame(loop);
// g.state => activated

g.onerror = err => {
    if (err.name == "NotReadableError")
        alert("you don't have a gyro!");
    else if (err.name == "NotAllowedError")
        alert("Give me the gyro!");
}

g.start();

@tobie tobie modified the milestone: Level 1 Oct 27, 2016
@tobie tobie self-assigned this Oct 28, 2016
tobie added a commit that referenced this issue Oct 31, 2016
tobie added a commit that referenced this issue Nov 1, 2016
tobie added a commit that referenced this issue Nov 1, 2016
tobie added a commit to tobie/sensors that referenced this issue Jan 26, 2017
This fixes a number of pending issues listed below,
for which there was consensus, but editing was still pending.

This introduces a dependency on the infra standard [[INFRA]],
to define precisely the low-level data structures
used by slots and concepts.

This also specifies slots more clearly.

Additionally, this rewrite sprinkles inline issues
in areas of the specs which require more work.

* Handle GC issues by getting rid of the SensorReading interface. 
  Closes w3c#153. Closes w3c#101.
* Check sensor existence before requesting permission. Closes w3c#145.
* Remove side effects from constructors. Closes w3c#138.
* Move default sensor check to start() and add "unconnected" state.
  Closes w3c#128 and closes w3c#104.
* Throttle update frequency to 60Hz by relying on
  requestAnimationFrame. Closes w3c#100.
tobie added a commit that referenced this issue Jan 26, 2017
This fixes a number of pending issues listed below,
for which there was consensus, but editing was still pending.

This introduces a dependency on the infra standard [[INFRA]],
to define precisely the low-level data structures
used by slots and concepts.

This also specifies slots more clearly.

Additionally, this rewrite sprinkles inline issues
in areas of the specs which require more work.

* Handle GC issues by getting rid of the SensorReading interface. 
  Closes #153. Closes #101.
* Check sensor existence before requesting permission. Closes #145.
* Remove side effects from constructors. Closes #138.
* Move default sensor check to start() and add "unconnected" state.
  Closes #128 and closes #104.
* Throttle update frequency to 60Hz by relying on
  requestAnimationFrame. Closes #100.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants