Re: [css-houdini-drafts] [worklets] relationship to JavaScript agent clusters

> On the other hand: if there is some kind of reliable and predictable infrastructure around that (eg, the main document can listen for some event that said it was deactivated and reactivated, and the liveness of the worklet can be inferred reliably from that event) then my concern in the previous paragraph may not matter. All I care about is that it should be possible to write reliable threaded code, and for that to happen, programs must know whether threads are shut down by the embedding. (But I don't know enough about the details to the web stack and worklets to make a call on that.)

This is the case, at least for an `AudioWorklet`, which is with an `AudioContext`.

An `AudioContext` has a [state](https://webaudio.github.io/web-audio-api/#widl-BaseAudioContext-state), and a [state transition callback](https://webaudio.github.io/web-audio-api/#widl-BaseAudioContext-onstatechange). If the state is `"running"`, then the audio callbacks are firing, and the worklet code runs. If the `AudioContext` state is `"suspended"` or `"closed"`, then the `AudioContext`'s audio thread is not running, so the worklet is not running.

A triplet of methods (`suspend`, `resume`, `close`) allows authors to control this state.  Those methods return `Promise`s that resolve when the state transition has been reflected to the main thread, and a `"statechange"` event is fired to the `AudioContext`.

When loading up a document (either from history, or going to it from the first time), an `AudioContext` is initially `"suspended"`, and `statechange` is fired when it goes to `"running"`, so it's possible to coordinate the main thread and the worklet.

When navigating away from a document, the `AudioContext` is suspended. When closing the tab, it's closed.

Proper synchronization is easy to do, with this mechanism, so we are in the situation you describe in your second paragraph.

-- 
GitHub Notification of comment by padenot
Please view or discuss this issue at https://github.com/w3c/css-houdini-drafts/issues/380#issuecomment-295287219 using your GitHub account

Received on Wednesday, 19 April 2017 14:22:07 UTC