@@ -449,26 +449,22 @@ const assetCacheRegistryStartTime = Date.now();
449
449
let assetCacheRegistryPromise;
450
450
let assetCacheRegistry = {};
451
451
452
-
const getAssetCacheRegistry = function(callback) {
452
+
const getAssetCacheRegistry = function() {
453
453
if ( assetCacheRegistryPromise === undefined ) {
454
454
assetCacheRegistryPromise = new Promise(resolve => {
455
-
// start of executor
456
-
µBlock.cacheStorage.get('assetCacheRegistry', bin => {
457
-
if (
458
-
bin instanceof Object &&
459
-
bin.assetCacheRegistry instanceof Object
460
-
) {
461
-
assetCacheRegistry = bin.assetCacheRegistry;
462
-
}
463
-
resolve();
464
-
});
465
-
// end of executor
455
+
µBlock.cacheStorage.get('assetCacheRegistry', bin => {
456
+
if (
457
+
bin instanceof Object &&
458
+
bin.assetCacheRegistry instanceof Object
459
+
) {
460
+
assetCacheRegistry = bin.assetCacheRegistry;
461
+
}
462
+
resolve();
463
+
});
466
464
});
467
465
}
468
466
469
-
assetCacheRegistryPromise.then(( ) => {
470
-
callback(assetCacheRegistry);
471
-
});
467
+
return assetCacheRegistryPromise.then(( ) => assetCacheRegistry);
472
468
};
473
469
474
470
const saveAssetCacheRegistry = (function() {
@@ -513,11 +509,9 @@ const assetCacheRead = function(assetKey, callback) {
513
509
reportBack(bin[internalKey]);
514
510
};
515
511
516
-
let onReady = function() {
512
+
getAssetCacheRegistry().then(( ) => {
517
513
µBlock.cacheStorage.get(internalKey, onAssetRead);
518
-
};
519
-
520
-
getAssetCacheRegistry(onReady);
514
+
});
521
515
};
522
516
523
517
const assetCacheWrite = function(assetKey, details, callback) {
@@ -542,22 +536,35 @@ const assetCacheWrite = function(assetKey, details, callback) {
542
536
if ( details instanceof Object && typeof details.url === 'string' ) {
543
537
entry.remoteURL = details.url;
544
538
}
545
-
µBlock.cacheStorage.set({ assetCacheRegistry, [internalKey]: content });
539
+
µBlock.cacheStorage.set(
540
+
{ [internalKey]: content },
541
+
details => {
542
+
if (
543
+
details instanceof Object &&
544
+
typeof details.bytesInUse === 'number'
545
+
) {
546
+
entry.byteLength = details.bytesInUse;
547
+
}
548
+
saveAssetCacheRegistry(true);
549
+
}
550
+
);
546
551
const result = { assetKey, content };
547
552
if ( typeof callback === 'function' ) {
548
553
callback(result);
549
554
}
550
555
// https://github.com/uBlockOrigin/uBlock-issues/issues/248
551
556
fireNotification('after-asset-updated', result);
552
557
};
553
-
getAssetCacheRegistry(onReady);
558
+
559
+
getAssetCacheRegistry().then(( ) => {
560
+
µBlock.cacheStorage.get(internalKey, onReady);
561
+
});
554
562
};
555
563
556
564
const assetCacheRemove = function(pattern, callback) {
557
-
const onReady = function() {
558
-
const cacheDict = assetCacheRegistry,
559
-
removedEntries = [],
560
-
removedContent = [];
565
+
getAssetCacheRegistry().then(cacheDict => {
566
+
const removedEntries = [];
567
+
const removedContent = [];
561
568
for ( const assetKey in cacheDict ) {
562
569
if ( pattern instanceof RegExp && !pattern.test(assetKey) ) {
563
570
continue;
@@ -582,14 +589,15 @@ const assetCacheRemove = function(pattern, callback) {
582
589
{ assetKey: removedEntries[i] }
583
590
);
584
591
}
585
-
};
586
-
587
-
getAssetCacheRegistry(onReady);
592
+
});
588
593
};
589
594
590
595
const assetCacheMarkAsDirty = function(pattern, exclude, callback) {
591
-
const onReady = function() {
592
-
const cacheDict = assetCacheRegistry;
596
+
if ( typeof exclude === 'function' ) {
597
+
callback = exclude;
598
+
exclude = undefined;
599
+
}
600
+
getAssetCacheRegistry().then(cacheDict => {
593
601
let mustSave = false;
594
602
for ( const assetKey in cacheDict ) {
595
603
if ( pattern instanceof RegExp ) {
@@ -617,12 +625,7 @@ const assetCacheMarkAsDirty = function(pattern, exclude, callback) {
617
625
if ( typeof callback === 'function' ) {
618
626
callback();
619
627
}
620
-
};
621
-
if ( typeof exclude === 'function' ) {
622
-
callback = exclude;
623
-
exclude = undefined;
624
-
}
625
-
getAssetCacheRegistry(onReady);
628
+
});
626
629
};
627
630
628
631
/******************************************************************************/
@@ -642,12 +645,12 @@ const stringIsNotEmpty = function(s) {
642
645
643
646
**/
644
647
645
-
var readUserAsset = function(assetKey, callback) {
646
-
var reportBack = function(content) {
648
+
const readUserAsset = function(assetKey, callback) {
649
+
const reportBack = function(content) {
647
650
callback({ assetKey: assetKey, content: content });
648
651
};
649
652
650
-
var onLoaded = function(bin) {
653
+
const onLoaded = function(bin) {
651
654
if ( !bin ) { return reportBack(''); }
652
655
var content = '';
653
656
if ( typeof bin['cached_asset_content://assets/user/filters.txt'] === 'string' ) {
@@ -671,7 +674,7 @@ var readUserAsset = function(assetKey, callback) {
671
674
}
672
675
return reportBack(content);
673
676
};
674
-
var toRead = assetKey;
677
+
let toRead = assetKey;
675
678
if ( assetKey === µBlock.userFiltersPath ) {
676
679
toRead = [
677
680
assetKey,
@@ -682,7 +685,7 @@ var readUserAsset = function(assetKey, callback) {
682
685
vAPI.storage.get(toRead, onLoaded);
683
686
};
684
687
685
-
var saveUserAsset = function(assetKey, content, callback) {
688
+
const saveUserAsset = function(assetKey, content, callback) {
686
689
var bin = {};
687
690
bin[assetKey] = content;
688
691
// TODO(seamless migration):
@@ -711,27 +714,33 @@ api.get = function(assetKey, options, callback) {
711
714
callback = noopfunc;
712
715
}
713
716
717
+
return new Promise(resolve => {
718
+
// start of executor
714
719
if ( assetKey === µBlock.userFiltersPath ) {
715
-
readUserAsset(assetKey, callback);
720
+
readUserAsset(assetKey, details => {
721
+
callback(details);
722
+
resolve(details);
723
+
});
716
724
return;
717
725
}
718
726
719
-
var assetDetails = {},
727
+
let assetDetails = {},
720
728
contentURLs,
721
729
contentURL;
722
730
723
-
var reportBack = function(content, err) {
724
-
var details = { assetKey: assetKey, content: content };
731
+
const reportBack = function(content, err) {
732
+
const details = { assetKey: assetKey, content: content };
725
733
if ( err ) {
726
734
details.error = assetDetails.lastError = err;
727
735
} else {
728
736
assetDetails.lastError = undefined;
729
737
}
730
738
callback(details);
739
+
resolve(details);
731
740
};
732
741
733
-
var onContentNotLoaded = function() {
734
-
var isExternal;
742
+
const onContentNotLoaded = function() {
743
+
let isExternal;
735
744
while ( (contentURL = contentURLs.shift()) ) {
736
745
isExternal = reIsExternalPath.test(contentURL);
737
746
if ( isExternal === false || assetDetails.hasLocalURL !== true ) {
@@ -748,7 +757,7 @@ api.get = function(assetKey, options, callback) {
748
757
}
749
758
};
750
759
751
-
var onContentLoaded = function(details) {
760
+
const onContentLoaded = function(details) {
752
761
if ( stringIsNotEmpty(details.content) === false ) {
753
762
onContentNotLoaded();
754
763
return;
@@ -762,7 +771,7 @@ api.get = function(assetKey, options, callback) {
762
771
reportBack(details.content);
763
772
};
764
773
765
-
var onCachedContentLoaded = function(details) {
774
+
const onCachedContentLoaded = function(details) {
766
775
if ( details.content !== '' ) {
767
776
return reportBack(details.content);
768
777
}
@@ -780,11 +789,13 @@ api.get = function(assetKey, options, callback) {
780
789
};
781
790
782
791
assetCacheRead(assetKey, onCachedContentLoaded);
792
+
// end of executor
793
+
});
783
794
};
784
795
785
796
/******************************************************************************/
786
797
787
-
var getRemote = function(assetKey, callback) {
798
+
const getRemote = function(assetKey, callback) {
788
799
var assetDetails = {},
789
800
contentURLs,
790
801
contentURL;
@@ -852,10 +863,19 @@ var getRemote = function(assetKey, callback) {
852
863
/******************************************************************************/
853
864
854
865
api.put = function(assetKey, content, callback) {
855
-
if ( reIsUserAsset.test(assetKey) ) {
856
-
return saveUserAsset(assetKey, content, callback);
857
-
}
858
-
assetCacheWrite(assetKey, content, callback);
866
+
return new Promise(resolve => {
867
+
const onDone = function(details) {
868
+
if ( typeof callback === 'function' ) {
869
+
callback(details);
870
+
}
871
+
resolve(details);
872
+
};
873
+
if ( reIsUserAsset.test(assetKey) ) {
874
+
saveUserAsset(assetKey, content, onDone);
875
+
} else {
876
+
assetCacheWrite(assetKey, content, onDone);
877
+
}
878
+
});
859
879
};
860
880
861
881
/******************************************************************************/
@@ -895,14 +915,27 @@ api.metadata = function(callback) {
895
915
if ( cacheRegistryReady ) { onReady(); }
896
916
});
897
917
898
-
getAssetCacheRegistry(function() {
918
+
getAssetCacheRegistry().then(( ) => {
899
919
cacheRegistryReady = true;
900
920
if ( assetRegistryReady ) { onReady(); }
901
921
});
902
922
};
903
923
904
924
/******************************************************************************/
905
925
926
+
api.getBytesInUse = function() {
927
+
return getAssetCacheRegistry().then(cacheDict => {
928
+
let bytesUsed = 0;
929
+
for ( const assetKey in cacheDict ) {
930
+
if ( cacheDict.hasOwnProperty(assetKey) === false ) { continue; }
931
+
bytesUsed += cacheDict[assetKey].byteLength || 0;
932
+
}
933
+
return bytesUsed;
934
+
});
935
+
};
936
+
937
+
/******************************************************************************/
938
+
906
939
api.purge = assetCacheMarkAsDirty;
907
940
908
941
api.remove = function(pattern, callback) {
@@ -1013,7 +1046,7 @@ var updateNext = function() {
1013
1046
updateOne();
1014
1047
});
1015
1048
1016
-
getAssetCacheRegistry(function(dict) {
1049
+
getAssetCacheRegistry().then(dict => {
1017
1050
cacheDict = dict;
1018
1051
if ( !assetDict ) { return; }
1019
1052
updateOne();
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4