Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const SHA1 = require("crypto-js/sha1");
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
const BASE_DIR = RNFetchBlob.fs.dirs.CacheDir + "/react-native-img-cache";
const FILE_PREFIX = Platform.OS === "ios" ? "" : "file://";
export type CacheHandler = (path: string) => void;
export type CacheHandler = (path: string|null) => void;

export interface CachedImageURISource extends ImageURISource {
uri: string;
Expand Down Expand Up @@ -102,13 +102,19 @@ export class ImageCache {
cache.downloading = true;
const method = source.method ? source.method : "GET";
cache.task = RNFetchBlob.config({ path }).fetch(method, uri, source.headers);
cache.task.then(() => {
cache.task.then((res: any) => {
cache.downloading = false;
cache.path = path;
this.notify(uri);
if (res.respInfo.status >= 400) {
RNFetchBlob.fs.unlink(path);
this.notify(uri, false);
} else {
cache.path = path;
this.notify(uri, true);
}
}).catch(() => {
cache.downloading = false;
RNFetchBlob.fs.unlink(path);
this.notify(uri, false);
});
}
}
Expand All @@ -130,10 +136,14 @@ export class ImageCache {

}

private notify(uri: string) {
private notify(uri: string, status = true) {
const handlers = this.cache[uri].handlers;
handlers.forEach(handler => {
handler(this.cache[uri].path as string);
if (status) {
handler(this.cache[uri].path as string);
} else {
handler(null);
}
});
}
}
Expand Down