Skip to content

Commit

Permalink
Merge pull request #15 from luggit/handle-uri-changes
Browse files Browse the repository at this point in the history
Handle URI Prop Changes
  • Loading branch information
billmalarky committed Mar 13, 2018
2 parents 6722d08 + a6ebd36 commit 1597a57
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/imageCacheHoc.js
Expand Up @@ -167,6 +167,30 @@ export default function imageCacheHoc(Image, options = {}) {

}

async componentWillReceiveProps(nextProps) {
const uri = traverse(this.props).get(['source', 'uri']);
const nextUri = traverse(nextProps).get(['source', 'uri']);
if (uri === nextUri) return;

const fileName = await this.fileSystem.getFileNameFromUrl(uri);
const nextFileName = await this.fileSystem.getFileNameFromUrl(nextUri);

FileSystem.unlockCacheFile(fileName, this.componentId);
FileSystem.lockCacheFile(nextFileName, this.componentId);

const { permanent } = this.props;
let localFilePath = null;
try {
localFilePath = await this.fileSystem.getLocalFilePathFromUrl(nextUri, !!permanent);
} catch (error) {
console.warn(error); // eslint-disable-line no-console
}

if (this._isMounted && localFilePath) {
this.setState({ localFilePath });
}
}

async componentWillUnmount() {

// Track component mount status to avoid calling setState() on unmounted component.
Expand Down Expand Up @@ -207,4 +231,4 @@ export default function imageCacheHoc(Image, options = {}) {

};

}
}

0 comments on commit 1597a57

Please sign in to comment.