-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Declarative WebWorker? #4786
Comments
So I'm sure this was discussed before as |
with |
Or maybe only allow module workers since non-modules are considered "classic". |
My problem with this has always been that existing |
Domenic, but that's the thing. People are already doing something that "looks so similar but behaves so drastically different". Except it's not explicit. Chrome has a bunch of code like this:
So we already have code that is on a I don't think a better String to Worker API would solve this. People will keep doing things like the code above because you get the benefits of the So my thinking is: we can make this less hacky, clearer and simpler for people to use. |
I agree people can hack up something that is unintuitive and weird today. I don't think that's necessarily a great motivation for adding such semantics to the platform natively, especially if---as you explain---it's pretty easy to do without native support. I think we could make it even easier, while maintaining the current invariant that in-page scripts run in the page's process, via a new string API. So that your example would become <!doctype html>
<html>
<body>
<canvas id='output' width='100' height='100'></canvas>
<script id='myWorker' type='text/worker'>
// wortker JS code
</script>
<script>
var worker = Worker.fromString(document.getElementById('myWorker').textContent);
</script>
</body>
</html> This seems to strike a better balance to me. |
I'm not sold that your example is cleaner than what I suggested, but I agree that removing the Blob/createObjectURL would be a good thing, so I'm fine moving in this direction. Do we know anyone on Firefox or Safari that deal with workers? |
Hi, I work on Firefox Workers. I would have the following concerns with the element based solution:
I am agreed that anything we can do to reduce calls to createObjectURL and the potential leakage of the underlying blob by a failure to revoke the URL would be an improvement. So I like the idea of |
Interesting, that'd be basically as convenient, and would help with some of the header-based issues: var worker = new Worker(new Response(document.getElementById('myWorker').textContent, headers)); However I guess such responses would have no URL, so we'd have to do a bit of legwork to figure out what that means... and it might amount to reinventing similar semantics to blob: URLs, hmm. But at least it's work that would pay off in other forms (i.e. for creating other resources from |
Yeah, to be clear, I meant assume we've solved whatwg/fetch#49 first. (And maybe assume that we provide some means of stuffing a URL into the response on the basis that ServiceWorkers allow origins to lie to themselves about such things, and that w3c/ServiceWorker#1026 and the proposal at w3c/ServiceWorker#1195 (comment) ties into our desire for the above fetch issue to be resolved, in which case we wouldn't have to roundtrip through a ServiceWorker.) |
I came across this issue while searching the tracker to see if someone proposed creating a Worker from Response before. @domenic's snippet looks pretty close to what I had in mind, but I was wondering if it would make more sense to add a Promise-based API similar to Worker.instantiate('...').then(worker => ...);
Worker.instantiate(fetch('...')).then(worker => ...);
Worker.instantiate(new Response('...')).then(worker => ...); With this, we could kill two birds with one stone and provide both a way to create workers from custom requests/responses, as well as a way to get load/error status in a unified way. |
This is not a full proposal yet, but I'd like to float the idea.
How do people feel about having a declarative WebWorker API by providing a new attribute to the script tag that makes the script load on a separate WebWorker? For example:
or maybe using a different type:
I think this is much nicer and more descriptive than the current
createObjectURL(new Blob(textContent))
approach to inline workers. It could boost and simplify usage of WebWorkers.The text was updated successfully, but these errors were encountered: