Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
StorageQuota polyfill
// MIT License, Copyright William Hilton <wmhilton@gmail.com>
// Implement the proposed W3C Quota Management API (circa 2017)
navigator.storageQuota = {
get supportedTypes () {
let types = []
if (navigator.webkitTemporaryStorage) types.push('temporary')
if (navigator.webkitPersistentStorage) types.push('persistent')
return types
},
queryInfo (type) {
return new Promise((resolve, reject) => {
// feature detect Chrome
if (navigator.webkitTemporaryStorage || navigator.webkitPersistentStorage) {
let success = (usage, quota) => resolve({usage, quota})
let failure = (e) => reject(e)
if (type === 'temporary') {
return navigator.webkitTemporaryStorage.queryUsageAndQuota(success, failure)
} else if (type === 'persistent') {
return navigator.webkitPersistentStorage.queryUsageAndQuota(success, failure)
}
} else {
// Hopefully firefox and others?
}
})
},
requestPersistentQuota (newQuota) {
return new Promise((resolve, reject) => {
// feature detect Chrome
if (navigator.webkitPersistentStorage) {
let success = (usage, quota) => resolve({usage, quota})
let failure = (e) => reject(e)
navigator.webkitPersistentStorage.requestQuota(newQuota, function (grantedBytes) {
// to be API compliant, we need to return 'usage' here too, and requestQuota only returns granted,
// so we have to do a second call to return both values
return navigator.webkitPersistentStorage.queryUsageAndQuota(success, failure)
})
} else {
// Hopefully firefox and others?
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment