Skip to content

Commit dd52822

Browse files
committed
Making functional events simpler
1 parent 657540a commit dd52822

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

docs/index.bs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,13 +2144,9 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
21442144
</section>
21452145

21462146
<section>
2147-
<h3 id="request-functional-event-dispatch">Request Functional Event Dispatch</h3>
2147+
<h3 id="firing-functional-events">Firing Functional Events</h3>
21482148

2149-
To request a <a>functional event</a> dispatch to a [=/service worker=], specifications *may* invoke <a>Handle Functional Event</a> algorithm with its [=/service worker registration=] <var ignore>registration</var> and the algorithm |callbackSteps| as the arguments.
2150-
2151-
Specifications *may* define an algorithm |callbackSteps| where the corresponding <a>functional event</a> can be created and fired with specification specific objects. The algorithm is passed <var ignore>globalObject</var> (a {{ServiceWorkerGlobalScope}} object) at which it *may* fire its <a>functional events</a>. This algorithm is called on a <a>task</a> <a lt="queue a task">queued</a> by <a>Handle Functional Event</a> algorithm.
2152-
2153-
Note: See an <a href="https://notifications.spec.whatwg.org/#activating-a-notification">example</a> hook defined in <a biblio data-biblio-type="informative" lt="notifications">Notifications API</a>.
2149+
To request a <a>functional event</a> dispatch to the [=service worker registration/active worker=] of a [=/service worker registration=], specifications *may* invoke <a>fire a functional event</a>.
21542150
</section>
21552151
</section>
21562152

@@ -2813,35 +2809,57 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
28132809
</section>
28142810

28152811
<section algorithm>
2816-
<h3 id="handle-functional-event-algorithm"><dfn export>Handle Functional Event</dfn></h3>
2812+
<h3 id="fire-functional-event-algorithm"><dfn export>Fire a Functional Event</dfn></h3>
28172813

28182814
: Input
2819-
:: |event|, an {{ExtendableEvent}} object
2815+
:: |eventName|, a DOMString
2816+
:: |eventConstructor|, an event constructor that extends {{ExtendableEvent}}
28202817
:: |registration|, a [=/service worker registration=]
2821-
:: |callbackSteps|, an algorithm
2818+
:: |initializationSteps|, optional steps to initialise |event|, constructed from |eventConstructor|
2819+
:: |postDispatchSteps|, optional steps to run on the [=service worker registration/active worker=]'s event loop, with |dispatchedEvent| set to the instance of |eventConstructor| that was [=dispatched=].
28222820
: Output
28232821
:: None
28242822

2825-
1. Assert: <a>scope to registration map</a> contains a value equal to |registration|.
2826-
1. Assert: |registration|'s <a>active worker</a> is not null.
2827-
1. Let |activeWorker| be |registration|'s <a>active worker</a>.
2828-
1. If |activeWorker|'s <a>set of event types to handle</a> does not [=set/contain=] |event|'s {{Event/type}}, then:
2829-
1. Return and continue running these steps <a>in parallel</a>.
2830-
1. If the time difference in seconds calculated by the current time minus |registration|'s <a>last update check time</a> is greater than 86400, invoke <a>Soft Update</a> algorithm with |registration|.
2823+
1. Assert: [=scope to registration map=] contains a value equal to |registration|.
2824+
1. Assert: |registration|'s [=active worker=] is not null.
2825+
1. Let |activeWorker| be |registration|'s [=active worker=].
2826+
1. If |activeWorker|'s [=set of event types to handle=] does not [=set/contain=] |eventName|, then return and run the following steps [=in parallel=]:
2827+
1. If the time difference in seconds calculated by the current time minus |registration|'s [=last update check time=] is greater than 86400, invoke [=Soft Update=] algorithm with |registration|.
28312828
1. Abort these steps.
28322829

28332830
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.
28342831

2835-
1. If |activeWorker|'s <a>state</a> is *activating*, wait for |activeWorker|'s <a>state</a> to become *activated*.
2836-
1. Invoke <a>Run Service Worker</a> algorithm with |activeWorker| as the argument.
2837-
1. <a>Queue a task</a> |task| to run these substeps:
2838-
1. Invoke |callbackSteps| with |activeWorker|'s [=service worker/global object=] as its argument.
2832+
1. If |activeWorker|'s [=state=] is *activating*, wait for |activeWorker|'s [=state=] to become *activated*.
2833+
1. Invoke [=Run Service Worker=] algorithm with |activeWorker| as the argument.
2834+
1. [=Queue a task=] |task| to run these substeps:
2835+
1. Let |event| be the result of [=creating an event=] with |eventConstructor| and the [=relevant realm=] of |activeWorker|'s [=service worker/global object=].
2836+
1. If |initializationSteps| is not null, then initialize |event| with |initializationSteps|.
2837+
1. [=Dispatch=] |event| on |activeWorker|'s [=service worker/global object=].
28392838
1. Invoke [=Update Service Worker Extended Events Set=] with |activeWorker| and |event|.
2839+
1. If |postDispatchSteps| is not null, then run |postDispatchSteps| passing |event| as |dispatchedEvent|.
28402840

2841-
The |task| *must* use |activeWorker|'s <a>event loop</a> and the <a>handle functional event task source</a>.
2841+
The |task| *must* use |activeWorker|'s [=event loop=] and the [=handle functional event task source=].
28422842

28432843
1. Wait for |task| to have executed or been discarded.
2844-
1. If the time difference in seconds calculated by the current time minus |registration|'s <a>last update check time</a> is greater than 86400, invoke <a>Soft Update</a> algorithm with |registration|.
2844+
1. If the time difference in seconds calculated by the current time minus |registration|'s [=last update check time=] is greater than 86400, invoke [=Soft Update=] algorithm with |registration|.
2845+
2846+
<div class="example">
2847+
To fire an "`amazingthing`" event (which is of type `AmazingThingEvent`) on a particular |serviceWorkerRegistration|, and initialize the event object's properties, the prose would be:
2848+
2849+
1. [=Fire a functional event=] named "`amazingthing`" using `AmazingThingEvent` on |serviceWorkerRegistration| with the following properties:
2850+
: propertyName
2851+
:: value
2852+
: anotherPropertyName
2853+
:: anotherValue
2854+
2855+
Then run the following steps with |dispatchedEvent|:
2856+
2857+
1. Do whatever you need to with |dispatchedEvent|.
2858+
2859+
Note that the initialization steps and post-dispatch steps are optional. If they aren't needed, the prose would be:
2860+
2861+
1. [=Fire a functional event=] named "`whatever`" using {{ExtendableEvent}} on |serviceWorkerRegistration|.
2862+
</div>
28452863
</section>
28462864

28472865
<section algorithm>

0 commit comments

Comments
 (0)