// Key that conveys the end of a write batch. Filtered out from state and // events. const SENTINEL_KEY = "WRITE_BATCH_SENTINEL";
var storageEventsPromise = null; function listenForStorageEvents(sentinelValue) { const recordedEvents = [];
storageEventsPromise = new Promise(function (resolve, reject) {
window.addEventListener("storage", function thisHandler(event) { if (event.key === SENTINEL_KEY) { // There should be no way for this to have the wrong value, but reject // if it is wrong. if (event.newValue === sentinelValue) {
window.removeEventListener("storage", thisHandler);
resolve(recordedEvents);
} else {
reject(event.newValue);
}
} else {
recordedEvents.push([event.key, event.newValue, event.oldValue]);
}
});
});
}
// Returns a promise that is resolve when the sentinel key has taken on the // sentinel value. Oddly structured to make sure promises don't let us // accidentally side-step the timeout clamping logic. function waitForSentinelValue(sentinelValue) { returnnew Promise(function (resolve) { function checkFunc() { if (localStorage.getItem(SENTINEL_KEY) === sentinelValue) {
resolve();
} else { // I believe linters will only yell at us if we use a non-zero constant. // Other forms of back-off were considered, including attempting to // issue a round-trip through PBackground, but that still potentially // runs afoul of labeling while also making us dependent on unrelated // APIs.
setTimeout(checkFunc, 0);
}
}
checkFunc();
});
}
async function getStorageState(maybeSentinel) { if (maybeSentinel) {
await waitForSentinelValue(maybeSentinel);
}
let numKeys = localStorage.length;
let state = {}; for (var iKey = 0; iKey < numKeys; iKey++) {
let key = localStorage.key(iKey); if (key !== SENTINEL_KEY) {
state[key] = localStorage.getItem(key);
}
} return state;
}
function returnAndClearStorageEvents() { return storageEventsPromise;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.