Created
March 13, 2017 22:04
-
-
Save billiegoose/f2b3d441ba2b7287fe96e875f27903a2 to your computer and use it in GitHub Desktop.
StorageQuota polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Resources: