On Wed, 30 Jul 2014, 'Jake Archibald' via blink-dev wrote:
> On Tue, Jul 29, 2014 at 7:41 PM, Ian Hickson <
i...@hixie.ch> wrote:
> > On Tue, 29 Jul 2014, 'Jake Archibald' via blink-dev wrote:
> > >
> > > We don't want those in ServiceWorker because it's a performance
> > > vampire, but I don't think we particularly want them in SharedWorker
> > > either.
> >
> > Why would you not want them in SharedWorker?
>
> If you're doing sync things within your SharedWorker you're going to
> block messages coming into the SharedWorker.
Yes. For many shared workers, that's _the entire point_. For example, if
the point of your shared worker is to have a defined serialised order of
operations on a client-side database, so that the underlying data is
guaranteed to remain consistent.
> If it's desirable to handle the entirety of each message & potential
> response in series, promises make that really simple:
>
> var queue = Promise.resolve();
>
> this.onconnect = function(event) {
> var port = event.ports[0];
> port.onmessage = function() {
> queue = queue.then(function() {
> // do stuff
> }).catch(function(err) {
> // handle errors
> });
> }
> };
No offense, but your idea of "really simple" is not the same as mine. IMHO
the code above is borderline unreadable. Compare it to:
this.onconnect = function(event) {
var port = event.ports[0];
port.onmessage = function() {
try {
// do stuff
} catch (err) {
// handle errors
}
};
}
On Wed, 30 Jul 2014, Jake Archibald wrote:
>
> var response = fetchSync(url);
> var json = response.body.asJSONSync();
>
> …is simple but blocking.
>
> (async function() {
> var response = await fetch(url);
> var json = await response.body.asJSON();
> }());
>
> …is simple & non-blocking.
That's not "simple". It's not at all obvious what "await" does or what
"async function" means. We also know that authors have huge trouble
understanding lambdas in general, especially lambdas that are immediately
invoked like this.
> I can see the case for heavy sync computation in dedicated workers,
> although I don't think we need sync filesystem APIs there to aid that,
> and as Elliot points out you need to make sure you're yielding
> regularly.
You don't necessarily ever need to yield.
On Wed, 30 Jul 2014, James Robinson wrote:
>
> No matter how much you sugar it asynchronous programming is
> significantly harder to understand and debug in some situations than
> synchronous straight-line code.
Hear hear.
> If the purpose of a worker is to perform file I/O and to do nothing
> else, there's no need to for it to yield regularly for other operations.
> What else could it do other than the task it is dedicated to?
Exactly.
On Wed, 30 Jul 2014, Anne van Kesteren wrote:
>
> End user changes his mind and wants to load a different file.
What if it's not possible for the end user to want a different file? For
example, the file is a game's high score table.
On Wed, 30 Jul 2014, Elliott Sprehn wrote:
>
> Indeed. Or what if the file is on a samba share that just went offline
> and it blocks for many minutes now? All file operations are now blocked
> because your IO thread is hung. The model where the IO thread can block
> like this is the reason that your linux machine completely locks up
> whenever a file share becomes unavailable.
That's why we shouldn't have sync I/O on the UI thread, sure. But if your
I/O is all from the same NFS-backed database, and one call blocks, all
subsequent calls are going to block too, so async doesn't help.
> If we're serious about allowing the hung threads programming model we
> need to add Worker.interrupt(), InterruptException, and
> WorkerGlobalScope#isInterrupted() so you can at least do something
> sensible.
I think a message peeking mechanism would be better than an interrupt, but
sure, adding features to make this kind of thing more powerful is
definitely something we should consider. I recommend bringing up such
proposals on
wha...@whatwg.org or filing them at:
http://whatwg.org/newbug