Created
September 10, 2012 10:41
-
-
Save tobie/3690242 to your computer and use it in GitHub Desktop.
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
// Query current usage and availability in Temporary storage: | |
navigator.temporaryStorage.getInfo( | |
function (info) { | |
// Continue to initialize local cache using the obtained | |
// usage and quota (availability) information. | |
initializeCache(info); | |
}, function (error) { log("Got error: ", error); | |
}); |
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
function onError(error) { | |
// Handle an error. | |
log("Got error: ", error); | |
} | |
function setUpOfflineMode(availableSpace) { | |
// ... | |
} | |
// A function which is to be called when 'offline-mode' is enabled. | |
function onOfflineEnabled(amountOfSpaceNeeded) { | |
// First check how much we can use in the Persistent storage. | |
navigator.persistentStorage.getInfo( | |
function (info) { | |
var availableSpace = info.quota - info.usage; | |
if (availableSpace >= amountOfSpaceNeeded) { | |
// We're fine; just continue to set up offline mode. | |
setUpOfflineMode(availableSpace); | |
return; | |
} | |
var requestingQuota = amountOfSpaceNeeded + info.usage; | |
navigator.persistentStorage.requestQuota( | |
requestingQuota, | |
function (grantedQuota) { | |
setUpOfflineMode(grantedQuota - info.usage); | |
}, | |
onError); | |
}, | |
onError); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment