Simplyfy code to gather storage used with StorageManager.estimate()

Documentation:
https://developer.mozilla.org/docs/Web/API/StorageManager
This commit is contained in:
Raymond Hill 2019-03-22 22:09:27 -03:00
parent ac71d6577a
commit 2fd587b7ae
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 8 additions and 38 deletions

View File

@ -537,17 +537,7 @@ const assetCacheWrite = function(assetKey, details, callback) {
if ( details instanceof Object && typeof details.url === 'string' ) {
entry.remoteURL = details.url;
}
µBlock.cacheStorage.set(
{ [internalKey]: content }
).then(details => {
if (
details instanceof Object &&
typeof details.bytesInUse === 'number'
) {
entry.byteLength = details.bytesInUse;
}
saveAssetCacheRegistry(true);
});
µBlock.cacheStorage.set({ assetCacheRegistry, [internalKey]: content });
const result = { assetKey, content };
if ( typeof callback === 'function' ) {
callback(result);
@ -893,19 +883,6 @@ api.metadata = function(callback) {
/******************************************************************************/
api.getBytesInUse = function() {
return getAssetCacheRegistry().then(cacheDict => {
let bytesUsed = 0;
for ( const assetKey in cacheDict ) {
if ( cacheDict.hasOwnProperty(assetKey) === false ) { continue; }
bytesUsed += cacheDict[assetKey].byteLength || 0;
}
return bytesUsed;
});
};
/******************************************************************************/
api.purge = assetCacheMarkAsDirty;
api.remove = function(pattern, callback) {

View File

@ -215,7 +215,6 @@
STORAGE_NAME,
{ keyPath: 'key' }
);
table.createIndex('value', 'value', { unique: false });
} catch(ex) {
req.onerror();
}
@ -349,22 +348,13 @@
const entries = [];
const dontCompress =
µBlock.hiddenSettings.cacheStorageCompression !== true;
let bytesInUse = 0;
const handleEncodingResult = result => {
if ( typeof result.data === 'string' ) {
bytesInUse += result.data.length;
} else if ( result.data instanceof Blob ) {
bytesInUse += result.data.size;
}
entries.push({ key: result.key, value: result.data });
};
for ( const key of keys ) {
const data = keyvalStore[key];
const isString = typeof data === 'string';
if ( isString === false || dontCompress ) {
if ( isString ) {
bytesInUse += data.length;
}
entries.push({ key, value: data });
continue;
}
@ -379,7 +369,7 @@
if ( callback === undefined ) { return; }
let cb = callback;
callback = undefined;
cb({ bytesInUse });
cb();
};
try {
const transaction = db.transaction(

View File

@ -50,10 +50,13 @@
countdown += 1;
vAPI.storage.getBytesInUse(null, process);
}
if ( this.cacheStorage.name !== 'browser.storage.local' ) {
if (
navigator.storage instanceof Object &&
navigator.storage.estimate instanceof Function
) {
countdown += 1;
this.assets.getBytesInUse().then(count => {
process(count);
navigator.storage.estimate().then(estimate => {
process(estimate.usage);
});
}
if ( countdown === 0 ) {