@@ -270,16 +270,11 @@ export class ImageLoader {
270
270
271
271
this . concurrency = this . config . concurrency ;
272
272
273
- this . cacheDirectoryExists
274
- . catch ( ( ) => {
275
- // doesn't exist
276
- return this . createCacheDirectory ( replace )
277
- . catch ( e => {
278
-
279
- this . throwError ( e ) ;
280
- this . isInit = true ;
281
-
282
- } ) ;
273
+ // create cache directories if they do not exist
274
+ this . createCacheDirectory ( replace )
275
+ . catch ( e => {
276
+ this . throwError ( e ) ;
277
+ this . isInit = true ;
283
278
} )
284
279
. then ( ( ) => this . indexCache ( ) )
285
280
. then ( ( ) => {
@@ -502,27 +497,52 @@ export class ImageLoader {
502
497
503
498
/**
504
499
* Check if the cache directory exists
500
+ * @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory
505
501
* @returns {Promise<boolean|FileError> } Returns a promise that resolves if exists, and rejects if it doesn't
506
502
*/
507
- private get cacheDirectoryExists ( ) : Promise < boolean > {
508
- return this . file
509
- . checkDir ( this . file . cacheDirectory , this . config . cacheDirectoryName )
510
- . then ( ( ) => {
511
- return ( this . isWKWebView ? this . file . checkDir ( this . file . tempDirectory , this . config . cacheDirectoryName ) : Promise . resolve ( true ) ) ;
512
- } ) ;
503
+ private cacheDirectoryExists ( directory : string ) : Promise < boolean > {
504
+ return this . file . checkDir ( directory , this . config . cacheDirectoryName ) ;
513
505
}
514
506
515
507
/**
516
- * Creates the cache directory
508
+ * Create the cache directories
517
509
* @param replace {boolean} override directory if exists
518
- * @returns {Promise<DirectoryEntry|FileError> } Returns a promise that resolves if the directory was created, and rejects on error
510
+ * @returns {Promise<DirectoryEntry|FileError> } Returns a promise that resolves if the directories were created, and rejects on error
519
511
*/
520
- private createCacheDirectory ( replace : boolean = false ) : Promise < DirectoryEntry > {
521
- return this . file
522
- . createDir ( this . file . cacheDirectory , this . config . cacheDirectoryName , replace )
523
- . then ( ( res : DirectoryEntry ) => {
524
- return this . isWKWebView ? this . file . createDir ( this . file . tempDirectory , this . config . cacheDirectoryName , replace ) : Promise . resolve ( res ) ;
525
- } ) ;
512
+ private createCacheDirectory ( replace : boolean = false ) : Promise < any > {
513
+ let cacheDirectoryPromise : Promise < any > ,
514
+ tempDirectoryPromise : Promise < any > ;
515
+
516
+
517
+ if ( replace ) {
518
+ // create or replace the cache directory
519
+ cacheDirectoryPromise = this . file . createDir ( this . file . cacheDirectory , this . config . cacheDirectoryName , replace ) ;
520
+ } else {
521
+ // check if the cache directory exists.
522
+ // if it does not exist create it!
523
+ cacheDirectoryPromise = this . cacheDirectoryExists ( this . file . cacheDirectory )
524
+ . catch ( ( ) => {
525
+ return this . file . createDir ( this . file . cacheDirectory , this . config . cacheDirectoryName ) ;
526
+ } ) ;
527
+ }
528
+
529
+ if ( this . isWKWebView ) {
530
+ if ( replace ) {
531
+ // create or replace the temp directory
532
+ tempDirectoryPromise = this . file . createDir ( this . file . tempDirectory , this . config . cacheDirectoryName , replace ) ;
533
+ } else {
534
+ // check if the temp directory exists.
535
+ // if it does not exist create it!
536
+ tempDirectoryPromise = this . cacheDirectoryExists ( this . file . tempDirectory )
537
+ . catch ( ( ) => {
538
+ return this . file . createDir ( this . file . tempDirectory , this . config . cacheDirectoryName ) ;
539
+ } ) ;
540
+ }
541
+ } else {
542
+ tempDirectoryPromise = Promise . resolve ( ) ;
543
+ }
544
+
545
+ return Promise . all ( [ cacheDirectoryPromise , tempDirectoryPromise ] ) ;
526
546
}
527
547
528
548
/**
0 commit comments