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 Jun 26, 2017
1 parent c478764 commit f44910f
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions index.bs
Expand Up @@ -852,9 +852,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 @@ -925,8 +925,8 @@ and "timestamp" as arguments.

### 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(205):

Expand Down Expand Up @@ -1267,6 +1267,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 @@ -1327,6 +1330,74 @@ when [=security check=] would return "insecure"
in order to reduce resource consumption, notably battery usage.


<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 f44910f

Please sign in to comment.