- From: Rick Waldron via GitHub <sysbot+gh@w3.org>
- Date: Fri, 08 May 2015 18:17:50 +0000
- To: public-device-apis@w3.org
> (for example, onchange events emitted at 60 Hz and a sensor
emitting values at 1,000 Hz)
So for this example, there would be at least ~17 values between each
`ondata`(or whatever it's called) event. A 200 Hz accelerometer would
have ~3 values between each `ondata`. Is it smart to expose a feature
that will inevitably lead to code that does something like this:
```js
let { Accelerometer } = sensors;
let accel = new Accelerometer({ frequency: 60, batch: true });
accel.on("data", function(event) {
// For illustration only...
// "event.batch" is an array of objects containing frozen
"snapshots" of
// all the readings in the last 16.666666ms
event.batch.length; // 17 for a 1000 Hz sensor
event.batch.length; // 3 for a 200 Hz sensor
});
```
Ok, that's fine right? But what happens when it's an `onchange` event
and the user sets the phone down on a table and has the screen sleep
set to 30 minutes? 29 minutes later, they pick up the phone, which
triggers the `onchange` which now delivers an array containing
`104400000` (`1000ms * 60s * 60m * 29m`) frozen snapshot objects.
**This will lock up your browser**
```js
var batch = [];
for (var i = 0; i < 104400000; i++) {
batch[i] = {x: 1, y: 2, z: 3};
}
console.log(batch);
```
Another issue is that developers may write code that's somehow
dependent on the number of records in a batch for the `ondata` event,
eg. 17 for the 60 Hz reporting of the 1000 Hz sensor, or 3 for the 60
Hz reporting of the 200 Hz sensor.
Unless the batching period is _only_ for the last period (16.66--ms in
our example)?
What if there is no batching included, but design in a way that allows
later introduction if there is demand. I suggest this because
batching could be done in user code:
```js
let { Accelerometer } = sensors;
let accel = new Accelerometer(); // Important!! This will default to
the sensor's read resolution
let batch = [];
let start, end;
accel.on("data", function(event) {
if (batch.length === 0) {
start = event.timestamp;
end = start + this.period;
}
if (event.timestamp < end) {
// I can pick what information is useful to me...
batch.push(this.acceleration);
} else {
doSomethingWithBatch(batch.slice());
batch.length = 0;
}
});
```
If this becomes common, we can extend the api to alleviate the pain.
--
GitHub Notif of comment by rwaldron
See https://github.com/w3c/sensors/issues/13#issuecomment-100318968
Received on Friday, 8 May 2015 18:17:51 UTC