@@ -396,26 +396,25 @@ const updateAssetSourceRegistry = function(json, silent) {
396
396
397
397
const getAssetSourceRegistry = function ( callback ) {
398
398
if ( assetSourceRegistryPromise === undefined ) {
399
- assetSourceRegistryPromise = new Promise ( resolve => {
400
- // start of executor
401
- µBlock . cacheStorage . get ( 'assetSourceRegistry' , bin => {
399
+ assetSourceRegistryPromise = µBlock . cacheStorage . get (
400
+ 'assetSourceRegistry'
401
+ ) . then ( bin => {
402
402
if (
403
- bin instanceof Object === false ||
404
- bin . assetSourceRegistry instanceof Object === false
403
+ bin instanceof Object &&
404
+ bin . assetSourceRegistry instanceof Object
405
405
) {
406
+ assetSourceRegistry = bin . assetSourceRegistry ;
407
+ return ;
408
+ }
409
+ return new Promise ( resolve => {
406
410
api . fetchText (
407
411
µBlock . assetsBootstrapLocation || 'assets/assets.json' ,
408
412
details => {
409
413
updateAssetSourceRegistry ( details . content , true ) ;
410
414
resolve ( ) ;
411
415
}
412
416
) ;
413
- return ;
414
- }
415
- assetSourceRegistry = bin . assetSourceRegistry ;
416
- resolve ( ) ;
417
- } ) ;
418
- // end of executor
417
+ } ) ;
419
418
} ) ;
420
419
}
421
420
@@ -451,16 +450,15 @@ let assetCacheRegistry = {};
451
450
452
451
const getAssetCacheRegistry = function ( ) {
453
452
if ( assetCacheRegistryPromise === undefined ) {
454
- assetCacheRegistryPromise = new Promise ( resolve => {
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
- } ) ;
453
+ assetCacheRegistryPromise = µBlock . cacheStorage . get (
454
+ 'assetCacheRegistry'
455
+ ) . then ( bin => {
456
+ if (
457
+ bin instanceof Object &&
458
+ bin . assetCacheRegistry instanceof Object
459
+ ) {
460
+ assetCacheRegistry = bin . assetCacheRegistry ;
461
+ }
464
462
} ) ;
465
463
}
466
464
@@ -509,8 +507,11 @@ const assetCacheRead = function(assetKey, callback) {
509
507
reportBack ( bin [ internalKey ] ) ;
510
508
} ;
511
509
512
- getAssetCacheRegistry ( ) . then ( ( ) => {
513
- µBlock . cacheStorage . get ( internalKey , onAssetRead ) ;
510
+ Promise . all ( [
511
+ getAssetCacheRegistry ( ) ,
512
+ µBlock . cacheStorage . get ( internalKey ) ,
513
+ ] ) . then ( results => {
514
+ onAssetRead ( results [ 1 ] ) ;
514
515
} ) ;
515
516
} ;
516
517
@@ -537,17 +538,16 @@ const assetCacheWrite = function(assetKey, details, callback) {
537
538
entry . remoteURL = details . url ;
538
539
}
539
540
µ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 ) ;
541
+ { [ internalKey ] : content }
542
+ ) . then ( details => {
543
+ if (
544
+ details instanceof Object &&
545
+ typeof details . bytesInUse === 'number'
546
+ ) {
547
+ entry . byteLength = details . bytesInUse ;
549
548
}
550
- ) ;
549
+ saveAssetCacheRegistry ( true ) ;
550
+ } ) ;
551
551
const result = { assetKey, content } ;
552
552
if ( typeof callback === 'function' ) {
553
553
callback ( result ) ;
@@ -556,9 +556,7 @@ const assetCacheWrite = function(assetKey, details, callback) {
556
556
fireNotification ( 'after-asset-updated' , result ) ;
557
557
} ;
558
558
559
- getAssetCacheRegistry ( ) . then ( ( ) => {
560
- µBlock . cacheStorage . get ( internalKey , onReady ) ;
561
- } ) ;
559
+ getAssetCacheRegistry ( ) . then ( ( ) => onReady ( ) ) ;
562
560
} ;
563
561
564
562
const assetCacheRemove = function ( pattern , callback ) {
0 commit comments