Re: [mediacapture-screen-share] Conditional Focus (When Display-Capture Starts) (#190)

> I also prefer an attribute over an explicit call for each track.

👍

> As of event task vs. micro task, there are preexisting examples.
> For instance, the fetch event is fired and some properties are read synchronously after the fetch event listeners, thus before the microtasks that would have been scheduled during the fetch event listeners.

Event listeners must respond synchronously to prevent bubbling/avoid default for historical reasons.

Those "properties" appear to be [fetch](https://w3c.github.io/ServiceWorker/#on-fetch-request-algorithm)  trying to _support_ promise code with [`event.respondWith(async () =>  {}`)](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent#examples). So my takeaway from fetch is we should make affordances for promise use. (_"[[In fetch] we use promises heavily](https://w3c.github.io/ServiceWorker/#execution-context)"_). 

> I believe this approach also allows shiming since the read-focus callback would be executed after the already registered promise resolution callback.

I'll try to explain the shimming problem in more detail: Say a JS library (e.g. [adapter.js](https://github.com/webrtcHacks/adapter/blob/ba8d60c751d33b4c4eefdf2c30dbe1ccb33a3f4e/src/js/firefox/getdisplaymedia.js#L11)) needs to shim gDM for some reason:
```js
const nativeGDM =  window.navigator.mediaDevices.getDisplayMedia;

window.navigator.mediaDevices.getDisplayMedia = async function getDisplayMedia(constraints) {
  // Here we're on Task A
  const stream = await nativeGDM.apply(this, arguments);
  // Here we're on Microtask A₁
  stream.newFeatureX = 3;
  return stream; // the implicit promise returned by this async function is resolved with stream
}
```
The shim is careful not to await anything else, yet a microtask checkpoint is unavoidable, because the promise returned by the shim is not the same as the one from `nativeGDM`. Thus the JS library will unintentionally break all existing code attempting to suppress focus. E.g.:
```js
// Here we're on Task A
const stream = await navigator.mediaDevices.getDisplayMedia({video: true});
// Here we're on Microtask A₂ with the shim (but we'd be on be on A₁ without)
navigator.mediaDevices.displayMediaFocusMode = "no-focus"; // focuses with the shim but not without!
```


-- 
GitHub Notification of comment by jan-ivar
Please view or discuss this issue at https://github.com/w3c/mediacapture-screen-share/issues/190#issuecomment-926852042 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Friday, 24 September 2021 18:54:47 UTC