Skip to content

Commit

Permalink
Fixes w3c#152. Each Sensor instance fires 'change' event considering …
Browse files Browse the repository at this point in the history
…its individual

frequency hint. Thus we achieve that  appearance of a new Sensor instance with a
higher frequency hint does not affect the 'onchange' notification of the existing
Sensor instances of the same type.
  • Loading branch information
Mikhail Pozdnyakov committed May 29, 2017
1 parent 2d22842 commit 352d8f5
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ with the internal slots described in the following table:
</tr>
<tr>
<td><dfn attribute for=Sensor>\[[waitingForUpdate]]</dfn></td>
<td>A boolean which indicates wether the observers have been updated
<td>A boolean which indicates whether the observers have been updated
or whether the object is waiting for a new reading to do so.
It is initially `true`.</td>
It is initially `false`.</td>
</tr>
<tr>
<td><dfn attribute for=Sensor>\[[identifyingParameters]]</dfn></td>
Expand Down Expand Up @@ -826,8 +826,8 @@ The {{Sensor/stop()}} method must run these steps or their [=equivalent=]:

### Sensor.onchange ### {#sensor-onchange}

{{Sensor/onchange}} is an {{EventHandler}} which is called
whenever a new [=sensor reading|reading=] is available.
{{Sensor/onchange}} is an {{EventHandler}} which is called to notify that new [=sensor reading|reading=] is available.


Issue: Should this be renamed `onreading`?
Should we instead add an `ondata` {{EventHandler}} for continuous data
Expand Down Expand Up @@ -1175,6 +1175,9 @@ Gets the {{Error}} object passed to {{SensorErrorEventInit}}.
value of |reading| to see if there's a change
that needs to be propagated.
1. Unset |sensor|’s [=reporting flag=].
1. [=set/For each=] |s| of |activated_sensors|,
1. Invoke the [=Report Latest Reading updated=] abstract operation,
passing it |s| as argument.
</div>


Expand Down Expand Up @@ -1203,6 +1206,74 @@ Gets the {{Error}} object passed to {{SensorErrorEventInit}}.
</div>


<h3 dfn>Find the reporting frequency of a Sensor</h3>

<div algorithm="find the reporting frequency of a sensor">

: input
:: |sensor_instance|, a {{Sensor}} object.
: output
:: |frequency|, a [=frequency=].
1. Let |frequency| be `null`.
1. Let |f| be |sensor_instance|.{{[[desiredPollingFrequency]]}}.
1. if |f| is set,
1. set |frequency| to |f| capped by the upper and lower [=current polling frequency=]
bounds for the associated [=sensor=].
1. Otherwise,
1. user agent can assign |frequency| to an appropriate value.
1. return |frequency|.
</div>


<h3 dfn>Report Latest Reading updated</h3>

<div algorithm="report latest reading updated">

: input
:: |sensor_instance|, a {{Sensor}} object.
: output
:: None

1. If |sensor_instance|.{{[[waitingForUpdate]]}} is `true`,
1. Abort these steps.
1. Set |sensor_instance|.{{[[waitingForUpdate]]}} to `true`.
1. Let |lastReportedTimestamp| be the value of |sensor_instance|.{{[[lastEventFiredAt]]}}.
1. If |lastReportedTimestamp| is not set
1. Queue a task to run the [=Notify Sensor Object about new reading=] abstract operation,
passing it |sensor_instance| as argument.
1. Abort these steps.
1. Let |reportingFrequency| be result of invoking the [=Find the reporting frequency of a Sensor=]
abstract operation.
1. If |reportingFrequency| is `null`
1. Queue a task to run the [=Notify Sensor Object about new reading=] abstract operation,
passing it |sensor_instance| as argument.
1. Abort these steps.
1. Let |reportingInterval| be the result of 1 / |reportingFrequency|.
1. Let |timestampDelta| be the result of [=latest reading=]["timestamp"] - |lastReportedTimestamp|.
1. If |timestampDelta| is greater than or equal to |reportingInterval|
1. Queue a task to run the [=Notify Sensor Object about new reading=] abstract operation,
passing it |sensor_instance| as argument.
1. Abort these steps.
1. Let |deferUpdateTime| be the result of |reportingInterval| - |timestampDelta|.
1. User agent must defer queueing a task to run the [=Notify Sensor Object about new reading=] abstract
operation for a period of time equal to |deferUpdateTime|.
</div>

<h3 dfn>Notify Sensor Object about new reading</h3>

<div algorithm="notify sensor object about new reading">

: input
:: |sensor_instance|, a {{Sensor}} object.
: output
:: None

1. Set |sensor_instance|.{{[[waitingForUpdate]]}} to `false`.
1. Set |sensor_instance|.{{[[lastEventFiredAt]]}} to [=latest reading=]["timestamp"].
1. [=Fire an event=] named "change" at |sensor_instance|.
</div>


<h3 dfn>Handle Errors</h3>

<div algorithm="handle errors">
Expand Down

0 comments on commit 352d8f5

Please sign in to comment.