Skip to content

Commit b80ba58

Browse files
committed
Observe event listener additions only during the very first script evalution; enforce early return at all the functional events based on this info.
1 parent 1413c06 commit b80ba58

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

spec/service_worker/index.html

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ <h1>Service Worker</h1>
141141
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-containing-service-worker-registration">containing service worker registration</dfn> (a <a href="#dfn-service-worker-registration">service worker registration</a>), which contains itself.</p>
142142
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-service-worker-id">id</dfn> (an opaque string), which uniquely identifies itself during the lifetime of its <a href="#dfn-containing-service-worker-registration">containing service worker registration</a>.</p>
143143
<p>A <a href="#dfn-service-worker">service worker</a> is dispatched a set of <dfn id="dfn-lifecycle-events">lifecycle events</dfn>, <a href="#service-worker-global-scope-install-event">install</a> and <a href="#service-worker-global-scope-activate-event">activate</a>, and <dfn id="dfn-functional-events">functional events</dfn> including <a href="#service-worker-global-scope-fetch-event">fetch</a>.</p>
144-
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-resource">script resource</dfn>, which represents its own script resource. It is initially set to null.</p>
144+
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-resource">script resource</dfn>, which represents its own script resource. It is initially set to null. A <a href="#dfn-script-resource">script resource</a> has an associated <dfn id="dfn-has-ever-been-evaluated-flag">has ever been evaluated flag</dfn>. It is initially unset.</p>
145145
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-resource-map">script resource map</dfn> which is a <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-list-and-record-specification-type">List</a> of the <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-list-and-record-specification-type">Record</a> {[[key]], [[value]]} where [[key]] is a <a href="https://url.spec.whatwg.org/#concept-url">URL</a> and [[value]] is a script resource.</p>
146146
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-skip-waiting-flag">skip waiting flag</dfn>. Unless stated otherwise it is unset.</p>
147147
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-imported-scripts-updated-flag">imported scripts updated flag</dfn>. It is initially unset.</p>
@@ -3078,16 +3078,21 @@ <h1>Run Service Worker</h1>
30783078
</dl>
30793079
</li>
30803080
<li>Create a separate parallel execution environment (i.e. a separate thread or process or equivalent construct), and run the rest of these steps in that context.</li>
3081-
<li>Let <var>source</var> be the result of running the <a href="https://html.spec.whatwg.org/multipage/infrastructure.html#utf-8-decode">UTF-8 decode</a> algorithm on the script resource of <var>serviceWorker</var>.</li>
3081+
<li>Let <var>source</var> be the result of running the <a href="https://html.spec.whatwg.org/multipage/infrastructure.html#utf-8-decode">UTF-8 decode</a> algorithm on <var>serviceWorker</var>'s <a href="#dfn-script-resource">script resource</a> <var>scriptResource</var>.</li>
30823082
<li>Let <var>language</var> be JavaScript.</li>
30833083
<li>In the newly created execution environment, create a <a href="https://html.spec.whatwg.org/multipage/infrastructure.html#javascript-global-environment">JavaScript global environment</a> whose <em>global object</em> is <var>workerGlobalScope</var>. (The <a href="https://html.spec.whatwg.org/multipage/infrastructure.html#javascript-global-environment">JavaScript global environment</a> whose <em>global object</em> is a <code><a href="#service-worker-global-scope-interface">ServiceWorkerGlobalScope</a></code> object is defined as the <dfn id="service-worker-environment">service worker environment</dfn>, which is a type of <a href="https://html.spec.whatwg.org/multipage/webappapis.html#worker-environment">worker environments</a>.)</li>
30843084
<li>Let <var>script</var> be a new <a href="https://html.spec.whatwg.org/multipage/webappapis.html#concept-script">script</a>.</li>
30853085
<li>Obtain the appropriate <a href="https://html.spec.whatwg.org/multipage/webappapis.html#script-execution-environment">script execution environment</a> for the scripting language <var>language</var> from <var>settingsObject</var>.</li>
30863086
<li>Parse/compile/initialize <var>source</var> using that <a href="https://html.spec.whatwg.org/multipage/webappapis.html#script-execution-environment">script execution environment</a>, as appropriate for <var>language</var>, and thus obtain a <a href="https://html.spec.whatwg.org/multipage/webappapis.html#code-entry-point">code entry-point</a>. If the script was not compiled successfully, let the <a href="https://html.spec.whatwg.org/multipage/webappapis.html#code-entry-point">code entry-point</a> be a no-op script, and act as if a corresponding uncaught script error had occurred.</li>
30873087
<li>Let <var>script</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#settings-object">settings object</a> be <var>settingsObject</var>.</li>
30883088
<li><a href="https://html.spec.whatwg.org/multipage/webappapis.html#jump-to-a-code-entry-point">Jump</a> to the <var>script</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#code-entry-point">code entry-point</a>, and let that run until it either returns, fails to catch an exception, or gets aborted by the <a href="https://html.spec.whatwg.org/multipage/workers.html#kill-a-worker">kill a worker</a> or <a href="#terminate-service-worker-algorithm">Terminate Service Worker</a> algorithms.</li>
3089-
<li>Set <var>workerGlobalScope</var>'s associated <a href="#dfn-service-worker-global-scope-service-worker">service worker</a>'s <a href="#dfn-set-of-event-types-to-handle">set of event types to handle</a> to the set of event types created from <var>settingsObject</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#global-object">global object</a>'s associated list of <a href="https://dom.spec.whatwg.org/#concept-event-listener">event listeners</a>' event types.
3090-
<p class="note">If the global object's associated list of event listeners does not have any event listener added at this moment, the service worker's set of event types to handle is set to an empty set. The user agents are encouraged to show a warning that the event listeners must be added on the initial evaluation of worker script.</p>
3089+
<li>If <var>scriptResource</var>'s <a href="#dfn-has-ever-been-evaluated-flag">has ever been evaluated flag</a> is unset, then:
3090+
<ol>
3091+
<li>Set <var>workerGlobalScope</var>'s associated <a href="#dfn-service-worker-global-scope-service-worker">service worker</a>'s <a href="#dfn-set-of-event-types-to-handle">set of event types to handle</a> to the set of event types created from <var>settingsObject</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#global-object">global object</a>'s associated list of <a href="https://dom.spec.whatwg.org/#concept-event-listener">event listeners</a>' event types.
3092+
<p class="note">If the global object's associated list of event listeners does not have any event listener added at this moment, the service worker's set of event types to handle is set to an empty set. The user agents are encouraged to show a warning that the event listeners must be added on the very first evaluation of the worker script.</p>
3093+
</li>
3094+
<li>Set <var>scriptResource</var>'s <a href="#dfn-has-ever-been-evaluated-flag">has ever been evaluated flag</a>.</li>
3095+
</ol>
30913096
</li>
30923097
<li>Run the <a href="https://html.spec.whatwg.org/multipage/webappapis.html#responsible-event-loop">responsible event loop</a> specified by <var>settingsObject</var> until it is destroyed.</li>
30933098
<li>Empty <var>workerGlobalScope</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#list-of-active-timers">list of active timers</a>.</li>
@@ -3164,7 +3169,7 @@ <h1>Handle Fetch</h1>
31643169
</li>
31653170
<li>Let <var>activeWorker</var> be <var>registration</var>'s <a href="#dfn-active-worker">active worker</a>.</li>
31663171
<li>If <var>activeWorker</var>'s <a href="#dfn-set-of-event-types-to-handle">set of event types to handle</a> does not contain <code>fetch</code>, return null.
3167-
<p class="note">To avoid unnecessary delays, the Handle Fetch enforces early return when no event listeners have been deterministically added in the service worker's global during the initial script execution.</p>
3172+
<p class="note">To avoid unnecessary delays, the Handle Fetch enforces early return when no event listeners have been deterministically added in the service worker's global during the very first script execution.</p>
31683173
</li>
31693174
<li>If <var>activeWorker</var>'s <a href="#dfn-state">state</a> is <em>activating</em>, wait for <var>activeWorker</var>'s <a href="#dfn-state">state</a> to become <em>activated</em>.</li>
31703175
<li>Invoke <a href="#run-service-worker-algorithm">Run Service Worker</a> algorithm with <var>activeWorker</var> as the argument.</li>
@@ -3254,6 +3259,9 @@ <h1>Handle Functional Event</h1>
32543259
<li><a href="http://www.ecma-international.org/ecma-262/6.0/#sec-algorithm-conventions">Assert</a>: a <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-list-and-record-specification-type">Record</a> with the [[value]] equals to <var>registration</var> is contained in <a href="#dfn-scope-to-registration-map">scope to registration map</a>.</li>
32553260
<li><a href="http://www.ecma-international.org/ecma-262/6.0/#sec-algorithm-conventions">Assert</a>: <var>registration</var>'s <a href="#dfn-active-worker">active worker</a> is not null.</li>
32563261
<li>Let <var>activeWorker</var> be <var>registration</var>'s <a href="#dfn-active-worker">active worker</a>.</li>
3262+
<li>If <var>activeWorker</var>'s <a href="#dfn-set-of-event-types-to-handle">set of event types to handle</a> does not contain the event type for this functional event, return.
3263+
<p class="note">To avoid unnecessary delays, the Handle Functional Event enforces early return when no event listeners have been deterministically added in the service worker's global during the very first script execution.</p>
3264+
</li>
32573265
<li>If <var>activeWorker</var>'s <a href="#dfn-state">state</a> is <em>activating</em>, wait for <var>activeWorker</var>'s <a href="#dfn-state">state</a> to become <em>activated</em>.</li>
32583266
<li>Invoke <a href="#run-service-worker-algorithm">Run Service Worker</a> algorithm with <var>activeWorker</var> as the argument.</li>
32593267
<li><a href="https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task">Queue a task</a> <var>task</var> to invoke <var>callbackSteps</var> with <var>activeWorker</var>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object"> environment settings object</a>'s <a href="https://html.spec.whatwg.org/multipage/webappapis.html#global-object">global object</a> as its argument.

0 commit comments

Comments
 (0)