-
Notifications
You must be signed in to change notification settings - Fork 40
Description
The storage access API provides for querying the storage access state (hasStorageAccess) and requesting changes (requestStorageAccess), but not observing changes. Authors could poll hasStorageAccess, but this is inefficient and unergonomic.
A frame might have multiple libraries or widgets which need to be updated when the user approves storage access. Currently, the solutions available to the author are:
- manually identify all libraries/widgets which need to be notified and all widgets which can trigger a request, and route notifications between them
- replace
requestStorageAccesswith a wrapper - poll
hasStorageAccess(wasting CPU cycles)
The first of these gets even harder if the flag set changes due to a change outside the document. For instance, a storage access request originating from a same-origin frame elsewhere on the page causes a change with no notification in the current CG draft (since these share a partitioned storage key and thus flag set).
The simplest solution would seem to be:
partial interface Document {
readonly attribute Promise<void> storageAccessAvailable;
};which resolves whenever hasStorageAccess would start resolving to true (i.e., has storage access flag for the flag set corresponding to the document is set and the was expressly denied storage flag is not set).
Sample usage:
let loggedInWidget = document.getElementById('logged-in');
loggedInWidget.textContent = 'Unknown';
document.storageAccessAvailable.then(() => {
// Runs immediately if storage access is already available.
// Otherwise runs when it is granted, if ever.
loggedInWidget.textContent = localStorage.getItem('user') ?? 'Logged out';
});