[encrypted-media] Clarify explicitly when promises are to be resolved (#461)

jrummell-chromium has just created a new issue for https://github.com/w3c/encrypted-media:

== Clarify explicitly when promises are to be resolved ==
Currently the spec notes "Since promise handlers are queued as microtasks, these will be executed ahead of any events queued by the preceding steps" in several places. For example, in [generateRequest](https://w3c.github.io/encrypted-media/#methods-2):

> 10. Queue a task to run the following steps:
> > 1. If any of the preceding steps failed, reject promise with a new DOMException whose name is the appropriate error name.
> > 2. Set the sessionId attribute to session id.
> > 3. Set this object's callable value to true.
> > 4. Run the Queue a "message" Event algorithm on the session, providing message type and message.
> > 5. Resolve promise.

> NOTE: Since promise handlers are queued as microtasks, these will be executed ahead of any events queued by the preceding steps.

To make the spec clearer, it would be better to indicate exactly when the promise is resolved, compared to other steps. The example above would switch 4 and 5 to become:
> 4. Resolve promise.
> 5. Run the Queue a "message" Event algorithm on the session, providing message type and message.

And then the note is not needed, as it is now clear that the promise should be resolved before the "message" event is generated (and does not depend on promise handlers being implemented as microtasks). This also appears to be the suggestion in #19 ([comment](https://github.com/w3c/encrypted-media/issues/19#issuecomment-188497121)) although it was switched back when addressing #14.

Resolving the promise before firing an event is used in other specs (e.g. [Presentation API](https://w3c.github.io/presentation-api/#starting-a-presentation-connection)).

There is a potential problem with this in close(). Currently the attribute closed promise is resolved before the close() method promise is resolved. This will break the WPT tests for [playback-temporary-events](https://github.com/web-platform-tests/wpt/blob/master/encrypted-media/scripts/playback-temporary-events.js#L86) as it checks that the attribute promise is resolved before the close() method promise is resolved.


Please view or discuss this issue at https://github.com/w3c/encrypted-media/issues/461 using your GitHub account

Received on Tuesday, 10 December 2019 23:13:37 UTC