Skip to content

Commit

Permalink
Change add/remove event listener behavior for service workers
Browse files Browse the repository at this point in the history
This changes addEventListener() and removeEventListener() to not throw even after the very first evalution of the service worker script. Instead, this specifies user agents have to show a console warning.

Related: w3c/ServiceWorker#1004 and w3c/ServiceWorker#1322.

Tests: web-platform-tests/wpt#19496.

Fixes #371.
  • Loading branch information
jungkees authored and annevk committed Nov 4, 2019
1 parent 57512fa commit 7a48d64
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions dom.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,16 +1150,13 @@ participate in a tree structure.</p>
and an <a>event listener</a> <var>listener</var>, run these steps:

<ol>
<li>
<p>If <var>eventTarget</var>'s <a>relevant global object</a> is a {{ServiceWorkerGlobalScope}}
object and its associated <a>service worker</a>'s <a for="service worker">script resource</a>'s
<a for="script resource">has ever been evaluated flag</a> is set, then <a>throw</a> a
<code>TypeError</code>.
[[!SERVICE-WORKERS]]

<p class="note no-backref">To optimize storing the event types allowed for the service worker and
to avoid non-deterministic changes to the event listeners, invocation of the method is allowed
only during the very first evaluation of the service worker script.
<li><p>If <var>eventTarget</var> is a {{ServiceWorkerGlobalScope}} object, its
<a for="ServiceWorkerGlobalScope">service worker</a>'s
<a for="service worker">script resource</a>'s
<a for="script resource">has ever been evaluated flag</a> is set, and <var>listener</var>'s
<a for="event listener">type</a> matches the {{Event/type}} attribute value of any of the
<a>service worker events</a>, then <a>report a warning to the console</a> that this might not give
the expected results. [[!SERVICE-WORKERS]]

<li><p>If <var>listener</var>'s <a for="event listener">callback</a> is null, then return.

Expand Down Expand Up @@ -1189,30 +1186,35 @@ method, when invoked, must run these steps:
<var>once</var>.
</ol>

<p>To <dfn>remove an event listener</dfn>, given an {{EventTarget}} object <var>eventTarget</var>
and an <a>event listener</a> <var>listener</var>, set <var>listener</var>'s
<a for="event listener">removed</a> to true and <a for=list>remove</a> <var>listener</var> from
<var>eventTarget</var>'s <a for=EventTarget>event listener list</a>.
<!-- Intentionally not exported. -->
<p>To <dfn export>remove an event listener</dfn>, given an {{EventTarget}} object
<var>eventTarget</var> and an <a>event listener</a> <var>listener</var>, run these steps:

<ol>
<li><p>If the <a>context object</a> is a {{ServiceWorkerGlobalScope}} object and its
<a for="ServiceWorkerGlobalScope">service worker</a>'s
<a for="service worker">set of event types to handle</a> contains <var>type</var>, then
<a>report a warning to the console</a> that this might not give the expected results.
[[!SERVICE-WORKERS]]

<li><p>Set <var>listener</var>'s <a for="event listener">removed</a> to true and
<a for=list>remove</a> <var>listener</var> from <var>eventTarget</var>'s
<a for=EventTarget>event listener list</a>.
</ol>

<p class=note>HTML needs this to define event handlers. [[HTML]]

<p>To <dfn export>remove all event listeners</dfn>, given an {{EventTarget}} object
<var>eventTarget</var>, <a for=list>for each</a> <var>listener</var> of <var>eventTarget</var>'s
<a for=EventTarget>event listener list</a>, <a>remove an event listener</a> with
<var>eventTarget</var> and <var>listener</var>.

<p class="note">HTML needs this to define <code>document.open()</code>. [[HTML]]
<p class=note>HTML needs this to define <code>document.open()</code>. [[HTML]]

<p>The
<dfn method for=EventTarget><code>removeEventListener(<var>type</var>, <var>callback</var>, <var>options</var>)</code></dfn>
method, when invoked, must run these steps:

<ol>
<li><p>If the <a>context object</a>'s <a>relevant global object</a> is a
{{ServiceWorkerGlobalScope}} object and its associated <a>service worker</a>'s
<a for="service worker">script resource</a>'s
<a for="script resource">has ever been evaluated flag</a> is set, then <a>throw</a> a
<code>TypeError</code>. [[!SERVICE-WORKERS]]

<li><p>Let <var>capture</var> be the result of <a>flattening</a> <var>options</var>.

<li><p>If the <a>context object</a>'s <a for=EventTarget>event listener list</a>
Expand Down

0 comments on commit 7a48d64

Please sign in to comment.