Re: [w3c/clipboard-apis] What happens if promises to Blobs are not resolved within a reasonable amount of time? (Issue #161)

I think a deferral style API for the web platform would considerably simplify this, and some other related problems, without having to invent API-specific solutions. For example:

```js
document.addEventListener("click", e =>
{
 // user gesture rules allow certain APIs synchronously here
 const deferral = e.getDeferral();
 
 prepareSomeClipboardDataAsync().then(data =>
 {
  // user gesture rules now apply synchronously here
  deferral.use();
  
  // can now write to clipboard normally
  clipboard.write(data);
 });
});
```

The core problem is certain API calls like `clipboard.write()` are only allowed at certain times, and doing async work means it might not be allowed by the time the async work finishes. A deferral system allows the developer to save the allowed call as a deferral, and then call it at a time of their choosing later on. This avoids the need to have a special promise mechanism specific to the clipboard API, and generalises the solution to work with any data processing with any API.

To ensure single-use, `getDeferral()` would only work if no sensitive APIs were previously called, and subsequently no sensitive APIs can be used synchronously either, until `deferral.use()` is called. That itself is also single use and only allows a single use of a sensitive API synchronously after the call. The browser can also impose further restrictions on `deferral.use()`, such as imposing a timeout, throwing if the document is not visible, and so on.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/clipboard-apis/issues/161#issuecomment-963272967

Received on Monday, 8 November 2021 15:30:09 UTC