Skip to content

Commit

Permalink
fix(image-loader): create both cache directories independently (#40)
Browse files Browse the repository at this point in the history
* Bugfix: create both cache directories independently

Otherwise on iOS the temp directory will NOT be created if the cache
directory exists

* Bugfix: Only check existence of cache directories if they are not replaced anyhow
  • Loading branch information
swiftyone authored and ihadeed committed Apr 20, 2017
1 parent d90a02f commit a9c330f
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/providers/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,11 @@ export class ImageLoader {

this.concurrency = this.config.concurrency;

this.cacheDirectoryExists
.catch(() => {
// doesn't exist
return this.createCacheDirectory(replace)
.catch(e => {

this.throwError(e);
this.isInit = true;

});
// create cache directories if they do not exist
this.createCacheDirectory(replace)
.catch(e => {
this.throwError(e);
this.isInit = true;
})
.then(() => this.indexCache())
.then(() => {
Expand Down Expand Up @@ -502,27 +497,52 @@ export class ImageLoader {

/**
* Check if the cache directory exists
* @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory
* @returns {Promise<boolean|FileError>} Returns a promise that resolves if exists, and rejects if it doesn't
*/
private get cacheDirectoryExists(): Promise<boolean> {
return this.file
.checkDir(this.file.cacheDirectory, this.config.cacheDirectoryName)
.then(() => {
return (this.isWKWebView ? this.file.checkDir(this.file.tempDirectory, this.config.cacheDirectoryName) : Promise.resolve(true));
});
private cacheDirectoryExists(directory: string): Promise<boolean> {
return this.file.checkDir(directory, this.config.cacheDirectoryName);
}

/**
* Creates the cache directory
* Create the cache directories
* @param replace {boolean} override directory if exists
* @returns {Promise<DirectoryEntry|FileError>} Returns a promise that resolves if the directory was created, and rejects on error
* @returns {Promise<DirectoryEntry|FileError>} Returns a promise that resolves if the directories were created, and rejects on error
*/
private createCacheDirectory(replace: boolean = false): Promise<DirectoryEntry> {
return this.file
.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace)
.then((res: DirectoryEntry) => {
return this.isWKWebView ? this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace) : Promise.resolve(res);
});
private createCacheDirectory(replace: boolean = false): Promise<any> {
let cacheDirectoryPromise: Promise<any>,
tempDirectoryPromise: Promise<any>;


if (replace) {
// create or replace the cache directory
cacheDirectoryPromise = this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace);
} else {
// check if the cache directory exists.
// if it does not exist create it!
cacheDirectoryPromise = this.cacheDirectoryExists(this.file.cacheDirectory)
.catch(() => {
return this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName);
});
}

if (this.isWKWebView) {
if (replace) {
// create or replace the temp directory
tempDirectoryPromise = this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace);
} else {
// check if the temp directory exists.
// if it does not exist create it!
tempDirectoryPromise = this.cacheDirectoryExists(this.file.tempDirectory)
.catch(() => {
return this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName);
});
}
} else {
tempDirectoryPromise = Promise.resolve();
}

return Promise.all([cacheDirectoryPromise, tempDirectoryPromise]);
}

/**
Expand Down

0 comments on commit a9c330f

Please sign in to comment.