mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-05 18:32:30 +01:00
Simplyfy code to gather storage used with StorageManager.estimate()
Documentation: https://developer.mozilla.org/docs/Web/API/StorageManager
This commit is contained in:
parent
ac71d6577a
commit
2fd587b7ae
@ -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) {
|
||||
|
@ -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(
|
||||
|
@ -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 ) {
|
||||
|
Loading…
Reference in New Issue
Block a user