Skip to content

Commit ab1a929

Browse files
committed
Making functional events simpler
1 parent d0df742 commit ab1a929

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
@@ -2388,13 +2388,9 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
23882388
</section>
23892389

23902390
<section>
2391-
<h3 id="request-functional-event-dispatch">Request Functional Event Dispatch</h3>
2391+
<h3 id="firing-functional-events">Firing Functional Events</h3>
23922392

2393-
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.
2394-
2395-
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.
2396-
2397-
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>.
2393+
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>.
23982394
</section>
23992395
</section>
24002396

@@ -3105,35 +3101,57 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
31053101
</section>
31063102

31073103
<section algorithm>
3108-
<h3 id="handle-functional-event-algorithm"><dfn export>Handle Functional Event</dfn></h3>
3104+
<h3 id="fire-functional-event-algorithm"><dfn export>Fire a Functional Event</dfn></h3>
31093105

31103106
: Input
3111-
:: |event|, an {{ExtendableEvent}} object
3107+
:: |eventName|, a DOMString
3108+
:: |eventConstructor|, an event constructor that extends {{ExtendableEvent}}
31123109
:: |registration|, a [=/service worker registration=]
3113-
:: |callbackSteps|, an algorithm
3110+
:: |initializationSteps|, optional steps to initialise |event|, constructed from |eventConstructor|
3111+
:: |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=].
31143112
: Output
31153113
:: None
31163114

3117-
1. Assert: <a>scope to registration map</a> contains a value equal to |registration|.
3118-
1. Assert: |registration|'s <a>active worker</a> is not null.
3119-
1. Let |activeWorker| be |registration|'s <a>active worker</a>.
3120-
1. If |activeWorker|'s <a>set of event types to handle</a> does not [=set/contain=] |event|'s {{Event/type}}, then:
3121-
1. Return and continue running these steps <a>in parallel</a>.
3122-
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|.
3115+
1. Assert: [=scope to registration map=] contains a value equal to |registration|.
3116+
1. Assert: |registration|'s [=active worker=] is not null.
3117+
1. Let |activeWorker| be |registration|'s [=active worker=].
3118+
1. If |activeWorker|'s [=set of event types to handle=] does not [=set/contain=] |eventName|, then return and run the following steps [=in parallel=]:
3119+
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|.
31233120
1. Abort these steps.
31243121

31253122
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.
31263123

3127-
1. If |activeWorker|'s <a>state</a> is *activating*, wait for |activeWorker|'s <a>state</a> to become *activated*.
3128-
1. Invoke <a>Run Service Worker</a> algorithm with |activeWorker| as the argument.
3129-
1. <a>Queue a task</a> |task| to run these substeps:
3130-
1. Invoke |callbackSteps| with |activeWorker|'s [=service worker/global object=] as its argument.
3124+
1. If |activeWorker|'s [=state=] is *activating*, wait for |activeWorker|'s [=state=] to become *activated*.
3125+
1. Invoke [=Run Service Worker=] algorithm with |activeWorker| as the argument.
3126+
1. [=Queue a task=] |task| to run these substeps:
3127+
1. Let |event| be the result of [=creating an event=] with |eventConstructor| and the [=relevant realm=] of |activeWorker|'s [=service worker/global object=].
3128+
1. If |initializationSteps| is not null, then initialize |event| with |initializationSteps|.
3129+
1. [=Dispatch=] |event| on |activeWorker|'s [=service worker/global object=].
31313130
1. Invoke [=Update Service Worker Extended Events Set=] with |activeWorker| and |event|.
3131+
1. If |postDispatchSteps| is not null, then run |postDispatchSteps| passing |event| as |dispatchedEvent|.
31323132

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

31353135
1. Wait for |task| to have executed or been discarded.
3136-
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|.
3136+
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|.
3137+
3138+
<div class="example">
3139+
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:
3140+
3141+
1. [=Fire a functional event=] named "`amazingthing`" using `AmazingThingEvent` on |serviceWorkerRegistration| with the following properties:
3142+
: propertyName
3143+
:: value
3144+
: anotherPropertyName
3145+
:: anotherValue
3146+
3147+
Then run the following steps with |dispatchedEvent|:
3148+
3149+
1. Do whatever you need to with |dispatchedEvent|.
3150+
3151+
Note that the initialization steps and post-dispatch steps are optional. If they aren't needed, the prose would be:
3152+
3153+
1. [=Fire a functional event=] named "`whatever`" using {{ExtendableEvent}} on |serviceWorkerRegistration|.
3154+
</div>
31373155
</section>
31383156

31393157
<section algorithm>

0 commit comments

Comments
 (0)