-
Notifications
You must be signed in to change notification settings - Fork 315
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
When does the openWindow promise resolve? #728
Comments
Isn't the sync between listener setup and sender's posting a message between different threads rather a general case? I'm not sure whether a snippet below could help? /cc @jakearchibald // Page
navigator.serviceWorker.addEventListener('message', function (e) {
console.log('got message:', e.data);
});
document.addEventListener('DOMContentLoaded', e => {
var controller = navigator.serviceWorker.controller;
if (controller)
controller.postMessage('clientloaded');
})
// SW
// Yeah, we need a persistent state to store |client_id|, so assuming that ..
self.addEventListener('notificationclick', e => {
e.waitUntil(clients.openWindow(url).then(c => { client_id = c.id; }));
});
self.onmessage = e => {
if (e.data == 'clientloaded') {
clients.matchAll().then(clients => {
clients.forEach(c => {
if (c.id == client_id) {
c.postMessage('fromnotificationclick');
}
});
});
}
}; |
I think we need some kind of explicit ready() on window side, which would resolve the promise |
F2F: We should buffer messages until
No change on when |
(I'm slightly against making yet another broken onfoo event handler, but I guess since MessagePort already has similarly broken onmessage, doesn't matter too much.) |
FWIW I didn't know about the |
F2F resolution: |
I'd like us to consider allowing |
If |
EDIT: nevermind, this has mostly been suggested before. Just an idea: triggering
|
We decided to keep the previous f2f decision. Messages are automatically started on DOMContentLoaded, but if you want messages before that you need to call |
@annevk I'm thinking of defining a task source called client message queue on I was thinking of defining the task source in the service worker client which is a type of environment settings object, but then it can't cover the If the above outline makes sense, I'll make a PR to HTML. /cc @domenic |
Sounds reasonable. |
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: - when a document is dispatched a DOMContentLoaded event - when a worker client has executed run a worker algorithm Related Issue: w3c/ServiceWorker#728 Related commit (WIP): w3c/ServiceWorker@c2db88b
I made patches as follows:
I defined the task source in ServiceWorkerContainer object instead of the global objects. I think it's better since a ServiceWorkerContainer object is the target object at which the message event is dispatched, and we don't pollute the globals. Please review them. |
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: - when a document is dispatched a DOMContentLoaded event - when a worker client has executed run a worker algorithm Related Issue: w3c/ServiceWorker#728 Related commit (WIP): w3c/ServiceWorker@c2db88b
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: - when a document is dispatched a DOMContentLoaded event - when a worker client has executed run a worker algorithm Related Issue: w3c/ServiceWorker#728 Related commit: w3c/ServiceWorker@5c0ecae
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: - when a document is dispatched a DOMContentLoaded event - when a worker client has executed run a worker algorithm Related Issue: w3c/ServiceWorker#728 Related commit: w3c/ServiceWorker@5c0ecae
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: * when DOMContentLoaded is dispatched * when a worker client has executed run a worker algorithm Related issue: w3c/ServiceWorker#728 Related commit: w3c/ServiceWorker@5c0ecae
Addressed by 5c0ecae and whatwg/html@d615947 Closing. |
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: * when DOMContentLoaded is dispatched * when a worker client has executed run a worker algorithm Related issue: w3c/ServiceWorker#728 Related commit: w3c/ServiceWorker@5c0ecae
A ServiceWorkerContainer object has a client message queue task source, initially disabled, which holds tasks until the client is ready to get the messages from its service worker. This patch adds a condition that enables the task source: * when DOMContentLoaded is dispatched * when a worker client has executed run a worker algorithm Related issue: w3c/ServiceWorker#728 Related commit: w3c/ServiceWorker@5c0ecae
I am trying to send a message to a page from a serviceWorker. The goal is to let the page respond to the click (opening what the notification is about).
With this code inside a
notificationclick
event handler:There is a race between delivering the message event and attaching the
message
event listener (while loading the page). Experimentally, I have found that the only way to receive the event is to add themessage
event listener before<html>
, like so:This appears very unreliable to me (tested in Chrome for Android version 44 and 45).
A possible solution is letting the promise resolve around the
DOMContentLoaded
orload
events. Another option could be to let the service worker detect when the page is loaded (via an event).I have used a workaround: sending the needed information in the URL fragment identifier and navigating to the proper URL using
history.replaceState
in the client (webpage). This works reliable so far.The text was updated successfully, but these errors were encountered: