From bf9ec474dcd5536f3b7f24ba877f505ddc45cdda Mon Sep 17 00:00:00 2001 From: Wonday Date: Tue, 24 Sep 2019 23:10:10 +0800 Subject: [PATCH] fix isUrlCached(), add getCacheFilename() --- README.md | 19 +++++++++++++++---- index.js | 21 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 33b023e..d115fa0 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ import CachedImage from 'react-native-image-cache-wrapper'; CachedImage.clearCache(); ``` -**CachedImage.isUrlCached(url)** +**CachedImage.isUrlCached(url,success=(cachFile)=>void,fail=(error)=>void))** check if a url is cached. @@ -156,11 +156,22 @@ Example: import CachedImage from 'react-native-image-cache-wrapper'; // check if a url is cached. -if (CachedImage.isUrlCached(url)) { - // do something -}; +CachedImage.isUrlCached(url,(exists)=>{ + alert(exists); +}); ``` +**CachedImage.getCacheFilename(url)** + +make a cache filename. + +Example: +``` +import CachedImage from 'react-native-image-cache-wrapper'; + +// check if a url is cached. +let cachedFilename = CachedImage.getCacheFilename(url); +``` **CachedImage.cacheDir** diff --git a/index.js b/index.js index 870b762..8779642 100644 --- a/index.js +++ b/index.js @@ -46,11 +46,26 @@ export default class CachedImage extends Component { /** * check if a url is cached */ - static isUrlCached = async (url) => { + static isUrlCached = (url: string, success: Function, failure: Function) => { const cacheFile = _getCacheFilename(url); - return !!(await RNFetchBlob.fs.stat(cacheFile)); + RNFetchBlob.fs.exists(cacheFile) + .then((exists) => { + success && success(exists); + }) + .catch((error) => { + failure && failure(error); + }); }; + /** + * make a cache filename + * @param url + * @returns {string} + */ + static getCacheFilename = (url) => { + return _getCacheFilename(url); + } + /** * Same as ReactNaive.Image.getSize only it will not download the image if it has a cached version * @param url @@ -298,4 +313,4 @@ async function _saveCacheFile(url: string, success: Function, failure: Function) } catch (error) { failure && failure(error); } -} +} \ No newline at end of file