forked from kfiroo/react-native-cached-image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
4,579 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["react-native"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
jest.mock('react-native-fetch-blob', () => ({default: {fs: {}}})); | ||
jest.mock('react-native-clcasher/MemoryCache', () => ({default: {}})); | ||
|
||
import ImageCacheManager from '../ImageCacheManager'; | ||
import SimpleMemoryCache from './SimpleMemoryCache'; | ||
import SimpleMemoryFs from './SimpleMemoryFs'; | ||
|
||
const icm = ImageCacheManager({}, SimpleMemoryCache, SimpleMemoryFs); | ||
|
||
describe('ImageCacheManager', () => { | ||
|
||
beforeEach(() => icm.clearCache()); | ||
|
||
describe('downloadAndCacheUrl', () => { | ||
|
||
it('should fail if URL is not cacheable', () => { | ||
return icm.getCacheInfo() | ||
.then(res => console.log(res)) | ||
.then(() => { | ||
return expect(icm.downloadAndCacheUrl('not a real url')).rejects.toBeDefined(); | ||
}); | ||
}); | ||
|
||
it('should download a file when not in cache', () => { | ||
return icm.getCacheInfo() | ||
.then(res => console.log(res)) | ||
.then(() => icm.downloadAndCacheUrl('https://example.com/image.jpg')) | ||
.then(() => icm.getCacheInfo()) | ||
.then(res => console.log(res)) | ||
}); | ||
|
||
it('should add new entry to the cache if not in cache', () => { | ||
|
||
}); | ||
|
||
it('should return file name if image is in cache', () => { | ||
|
||
}); | ||
|
||
it('should not return cached entry if expired', () => { | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const DEFAULT_EXPIRES = 999999; | ||
|
||
function currentTime() { | ||
return Math.floor((new Date().getTime() / 1000)); | ||
} | ||
|
||
let cache = {}; | ||
|
||
const SimpleMemoryCache = {}; | ||
|
||
SimpleMemoryCache.set = (key, value, expires = DEFAULT_EXPIRES) => { | ||
cache[key] = { | ||
value: value, | ||
expires: currentTime() + parseInt(expires) | ||
}; | ||
return Promise.resolve(); | ||
}; | ||
|
||
SimpleMemoryCache.get = (key) => { | ||
const curTime = currentTime(); | ||
const v = cache[key]; | ||
if (v && v.expires && v.expires >= curTime) { | ||
return Promise.resolve(v.value); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
|
||
SimpleMemoryCache.remove = async (key) => { | ||
delete cache[key]; | ||
return Promise.resolve(); | ||
}; | ||
|
||
SimpleMemoryCache.flush = async () => { | ||
cache = {}; | ||
return Promise.resolve(); | ||
}; | ||
export default SimpleMemoryCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
let fs = {}; | ||
|
||
/** | ||
* wrapper around common filesystem actions | ||
*/ | ||
module.exports = { | ||
|
||
/** | ||
* returns the local cache dir | ||
* @returns {String} | ||
*/ | ||
getCacheDir() { | ||
return '/imagesCacheDir'; | ||
}, | ||
|
||
/** | ||
* returns a promise that is resolved when the download of the requested file | ||
* is complete and the file is saved. | ||
* if the download fails, or was stopped the partial file is deleted, and the | ||
* promise is rejected | ||
* @param fromUrl String source url | ||
* @param toFile String destination path | ||
* @param headers Object with headers to use when downloading the file | ||
* @returns {Promise} | ||
*/ | ||
downloadFile(fromUrl, toFile, headers) { | ||
fs[toFile] = fromUrl; | ||
return Promise.resolve(toFile); | ||
}, | ||
|
||
/** | ||
* remove the file in filePath if it exists. | ||
* this method always resolves | ||
* @param filePath | ||
* @returns {Promise} | ||
*/ | ||
deleteFile(filePath) { | ||
delete fs[filePath]; | ||
return Promise.resolve(); | ||
}, | ||
|
||
/** | ||
* copy a file from fromFile to toFile | ||
* @param fromFile | ||
* @param toFile | ||
* @returns {Promise} | ||
*/ | ||
copyFile(fromFile, toFile) { | ||
fs[toFile] = fs[fromFile] || fromFile; | ||
return Promise.resolve(); | ||
}, | ||
|
||
/** | ||
* remove the contents of dirPath | ||
* @param dirPath | ||
* @returns {Promise} | ||
*/ | ||
cleanDir(dirPath) { | ||
fs = _.omitBy(fs, (v, k) => _.startsWith(k, dirPath)); | ||
return Promise.resolve(); | ||
}, | ||
|
||
/** | ||
* get info about files in a folder | ||
* @param dirPath | ||
* @returns {Promise.<{file:Array, size:Number}>} | ||
*/ | ||
getDirInfo(dirPath) { | ||
const files = _(fs) | ||
.pickBy((v, k) => _.startsWith(k, dirPath)) | ||
.map((v, k) => ({ | ||
filename: k, | ||
source: v, | ||
size: 1 | ||
})) | ||
.value(); | ||
return Promise.resolve({ | ||
files, | ||
size: files.length | ||
}); | ||
}, | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.