-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change add/remove event listener behavior for service workers #653
Change add/remove event listener behavior for service workers #653
Conversation
@domfarolino what is your expected timeline for fixing whatwg/console#57? |
Under two weeks to be safe. I can prioritize that, thanks for the ping. |
@annevk, please take a look. I pushed a commit that uses the Console standard. |
dom.bs
Outdated
<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. | ||
<p>If <var>listener</var> observes a <a>functional event</a>, <var>eventTarget</var> is a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels clearer to say "If listener's type is…" then link to the list https://w3c.github.io/ServiceWorker/#execution-context-events.
Right now "observes" is a little vague, and "functional event" doesn't cover the lifecycle events.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DOM uses "observe" as in:
An event listener can be used to observe a specific event
If we use listener's type, we might need to compare it with the event's type attribute value, which makes it something like:
If listener's type is the type attribute value of one of the events in this list, ...
I'm fine with either, but also think that we might be able to drop this first condition entirely and just start with:
If eventTarget is a ServiceWorkerGlobalScope object, ...
This condition basically covers the listener's type is one in the list. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and "functional event" doesn't cover the lifecycle events.
Good point!
dom.bs
Outdated
worker">script resource</a>'s <a for="script resource">has ever been evaluated flag</a> is set, | ||
then the user agent must <a>report a warning to the console</a> with a description that explains | ||
that the user agent stores the event <a for="event listener">type</a> of the <a>functional | ||
event</a> for the <a>service worker</a> only during the very first evalution of the <a>service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the explanation be developer-focused here?
If so:
…the user agent must report a warning to the console explaining that an event with a type of listener's type should be added synchronously, otherwise, then the service worker restarts, events of this type will be dispatched before the listener is added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated it. Just kept the wording,"with a description", to denote an argument and tried to explain the service worker will not start with the asynchronously added event.
dom.bs
Outdated
<p class="note no-backref">To optimize storing the event types allowed for the <a>service | ||
worker</a>, Service Workers specification does not store the asynchronously added event <a | ||
for="event listener">type</a>. The <a>event listener</a> will still be added under this condition, | ||
but the implmentations have to inform developers about this behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might not need this note if the developer-facing explanation is clear enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this note would help implementers understand why this behavior was added (while the warning description says what to display to the console as you pointed). I tried to simplify the words.
dom.bs
Outdated
resource">has ever been evaluated flag</a> is set, then the user agent must <a>report a warning to | ||
the console</a> with a description that explains that the user agent will still start the | ||
<a>service worker</a> for the <a>functional event</a> that has been stored during the very first | ||
evalution of the <a>service worker</a> script and is asynchronously removed. [[!SERVICE-WORKERS]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(any changes to addEventListener should also be made here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this accordingly too.
This introduces the concept, service worker events, to refer to the events that are one of lifecycle events, functional events, and the legacy message events dispatched to ServiceWorkerGlobalScope. Related PR: whatwg/dom#653.
@jakearchibald, I uploaded a new snapshot. Please take another pass. This change references a change that I made in w3c/ServiceWorker#1452. |
This introduces the concept, service worker events, to refer to the events that are one of lifecycle events, functional events, and the legacy message events dispatched to ServiceWorkerGlobalScope. Related PR: whatwg/dom#653.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, mostly nits from me.
dom.bs
Outdated
only during the very first evaluation of the service worker script. | ||
<p>If <var>eventTarget</var> 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 the user agent must <a>report a warning to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then <a>report ...
No need to say must here. (Also applies below.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Out of my own curiosity, what'd be the actual meaning of it in terms of the conformance requirement after this change?
dom.bs
Outdated
<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 the user agent must <a>report a warning to the | ||
console</a> with a description explaining that the <a>service worker event</a> should be added | ||
synchronously, otherwise the user agent will not start</a> the <a>service worker</a> for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a stray </a>
. I'm surprised that builds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
dom.bs
Outdated
<p>If <var>eventTarget</var> 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 the user agent must <a>report a warning to the | ||
console</a> with a description explaining that the <a>service worker event</a> should be added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is to be added" (let's not use "should" as it's meant to indicate requirements to the implementer within this document).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
dom.bs
Outdated
<a>event</a>. [[!SERVICE-WORKERS]] | ||
|
||
<p class="note no-backref">To optimize with the set of <a>service worker events</a> to handle, the | ||
Service Workers specification does not store the asynchronously added event. The <a>event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to say what object does not store the event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
dom.bs
Outdated
<a>event</a>. [[!SERVICE-WORKERS]] | ||
|
||
<p class="note no-backref">To optimize with the set of <a>service worker events</a> to handle, the | ||
Service Workers specification does not store the asynchronously added event. The <a>event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No newlines inside <a>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Thanks for reviewing, @annevk. I addressed your comments and uploaded a new snapshot. Please take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Via some indirection user agents must implement the algorithm as written. Any must statement in the algorithm is redundant with that. A "should" statement or some such would work however, although that's not applicable here.
dom.bs
Outdated
<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>report a warning to the console</a> with a description explaining that the user agent will still | ||
start the <a>service worker</a> for the <a>service worker event</a> that was synchronously added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't really know whether it was added at the correct time, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are able to know whether the event has been added to the service worker's set of event types to handle. But I thought that we'd want to show the warning message when the function is called outside of the first evaluation regardless of whether the event has been added synchronously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event yes, but not this specific listener.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking over it again, I think what we want to achieve for removeEventListener() is to warn, regardless whether it's called sync or async, removing the listener won't effect the service worker start-up if the service worker event was added in the script evaluation. Update the text in the follow-up commit.
dom.bs
Outdated
<a>service worker event</a> is to be added synchronously, otherwise the user agent will not start | ||
the <a>service worker</a> for the <a>event</a>. [[!SERVICE-WORKERS]] | ||
|
||
<p class="note no-backref">To optimize with the set of <a>service worker events</a> to handle, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't read well. I also don't think we should use "synchronous" and "asynchronous" as it's not really clear relative to what they're being used. I'm not really sure what to suggest that doesn't get very wordy though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to rephrase the note by using "deterministically added .. during the very first script evaluation".
Description: Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a
dom.bs
Outdated
<p class="note no-backref">To optimize with the set of <a>service worker events</a> to handle, | ||
asynchronously added <a>service worker events</a> are not added to the <a>service worker</a>'s | ||
<a for="service worker">set of event types to handle</a>. The <a>event listener</a> will still be | ||
added under this condition, but the implmentations have to inform developers about this behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"implementations" is misspelled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks.
Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a
Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <[email protected]> Commit-Queue: Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/master@{#702753}
Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <[email protected]> Commit-Queue: Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/master@{#702753}
Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <[email protected]> Commit-Queue: Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/master@{#702753}
I uploaded a new snapshot. Please take a look. Also note that @grandfather1 added a test for this: web-platform-tests/wpt@bc28ef5. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can say something simpler here. Something along the lines of adding/removing listeners after script evaluation might not give the expected result.
dom.bs
Outdated
<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>report a warning to the console</a> with a description explaining that the user agent will still | ||
start the <a>service worker</a> for the <a>service worker event</a> that was synchronously added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event yes, but not this specific listener.
dom.bs
Outdated
<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>report a warning to the console</a> with a description explaining that the | ||
<a>service worker event</a> is to be added synchronously, otherwise the user agent may not start |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't know that it's a service worker event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I added a condition to do it only for service worker events in the follow-up commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think keeping it vaguer as I suggested might be nicer, since if you know what you're doing, adding event listeners late (for your own dispatch logic) is okay. Seems rather odd to do something like that, but I also don't really see reason to depend on so many details of service worker logic here for a warning.
I gave it another try. Please take a look. (I'd like to come up with simpler explanation but needs clarity about the behavior. Open to better language.) The build is failing due to the reference to ServiceWorkerGlobalScope's service worker that I introduced. I made a PR to Service Workers to resolve it: w3c/ServiceWorker#1476. |
…1476) whatwg/dom#653 needs a reference to this dfn.
…ronously doesn't throw., a=testonly Automatic update from web-platform-tests Service Worker: Fetch event added asynchronously doesn't throw. Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <[email protected]> Commit-Queue: Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/master@{#702753} -- wpt-commits: 42efecfdb246ea0f22b5a82f5c13659894de3d72 wpt-pr: 19496
…ronously doesn't throw., a=testonly Automatic update from web-platform-tests Service Worker: Fetch event added asynchronously doesn't throw. Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <[email protected]> Commit-Queue: Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/master@{#702753} -- wpt-commits: 42efecfdb246ea0f22b5a82f5c13659894de3d72 wpt-pr: 19496
…ronously doesn't throw., a=testonly Automatic update from web-platform-tests Service Worker: Fetch event added asynchronously doesn't throw. Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <falkenchromium.org> Commit-Queue: Daniel Soromou <fosoromomicrosoft.com> Cr-Commit-Position: refs/heads/master{#702753} -- wpt-commits: 42efecfdb246ea0f22b5a82f5c13659894de3d72 wpt-pr: 19496 UltraBlame original commit: 85284299266e6cd046f12ed3a588f973b6013590
…ronously doesn't throw., a=testonly Automatic update from web-platform-tests Service Worker: Fetch event added asynchronously doesn't throw. Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <falkenchromium.org> Commit-Queue: Daniel Soromou <fosoromomicrosoft.com> Cr-Commit-Position: refs/heads/master{#702753} -- wpt-commits: 42efecfdb246ea0f22b5a82f5c13659894de3d72 wpt-pr: 19496 UltraBlame original commit: 85284299266e6cd046f12ed3a588f973b6013590
…ronously doesn't throw., a=testonly Automatic update from web-platform-tests Service Worker: Fetch event added asynchronously doesn't throw. Based on the spec (whatwg/dom#653), addEventListener after the first evaluation of the Service worker script should not throw. Change-Id: I46af276a67e021cf277a98195bf5c04583ad0b0a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838376 Reviewed-by: Matt Falkenhagen <falkenchromium.org> Commit-Queue: Daniel Soromou <fosoromomicrosoft.com> Cr-Commit-Position: refs/heads/master{#702753} -- wpt-commits: 42efecfdb246ea0f22b5a82f5c13659894de3d72 wpt-pr: 19496 UltraBlame original commit: 85284299266e6cd046f12ed3a588f973b6013590
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 that the asynchronously added listener's event type will not affect service worker's behavior with the functional event stored during the first evaluation. This change referneces the funcional event concept defined in Service Workers spec, and confines the EventTarge object of the specified behavior to ServiceWorkerGlobalScope object instead of all the platform object in the global object. Related SW issue: w3c/ServiceWorker#1004. Related SW PR: w3c/ServiceWorker#1322. Fixes whatwg#371.
- Update the explanation to be more developer-focused - Use the newly defined service worker events concept
- Adjust the conformance requirement expression - Clarify the struct that stores events - Fix markup and alignment
- Reflect skipping event dispatch in Service Workers is optional - Rephrase notes - Fix a typo
343c5c1
to
351fd52
Compare
@jungkees I've made the changes I suggested above and I also moved the removal warning to "remove an event listener" as it should probably apply to event handlers equally. Please let me know what you think. Ideally we'd merge this early next week. |
dom.bs
Outdated
the expected results. [[!SERVICE-WORKERS]] | ||
|
||
<p class="note no-backref">Service workers are only started for event listeners added during the | ||
first script evaluation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an occasion that is not covered by this note. Service Workers changed the skipping behavior to optional: https://w3c.github.io/ServiceWorker/#should-skip-event-algorithm step 1 uses "may". So, the listeners added after the first script evaluation still can see the service worker starts. Should we say: "Service workers may only be started for event listeners added during the first script evaluation?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was that changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up removing the note as the situation is complex and explaining it in detail here feels rather distracting for these general purpose methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the change was made to match the implementation. @mattto, can you give us more context on why you used may in that change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it was mostly to match the implementations. It was the consensus at the F2Fs according to w3c/ServiceWorker#1200 (comment) and w3c/ServiceWorker#1200 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Limiting it to fetch
seems okay, but making it random effectively requires user agents to look at each other's code if sites take a dependency. That's not great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reopened the issue to track this.
<a>report a warning to the console</a> that this might not give the expected results. | ||
[[!SERVICE-WORKERS]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To give the context of what the expected results would be, does leaving a short note along the lines of "Service Workers will still be started after the removal." make sense?
@annevk, thanks for helping on this. It reads a lot simpler, indeed. I left two comments. I'm happy to see it merged early next week. |
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 that the asynchronously
added listener's event type will not affect service worker's behavior with the
functional event stored during the first evaluation.
This change referneces the funcional event concept defined in Service Workers
spec, and confines the EventTarge object of the specified behavior to
ServiceWorkerGlobalScope object instead of all the platform object in the global
object.
Related SW issue: w3c/ServiceWorker#1004.
Related SW PR: w3c/ServiceWorker#1322.
Fixes #371.
Preview | Diff