Skip to content

Commit 8b4dab1

Browse files
authored
fix(api): Support method and header parameters from ImageURISource
2 parents 80f2cc8 + 7f02317 commit 8b4dab1

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/index.tsx

+34-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const BASE_DIR = RNFetchBlob.fs.dirs.CacheDir + "/react-native-img-cache";
88
const FILE_PREFIX = Platform.OS === "ios" ? "" : "file://";
99
export type CacheHandler = (path: string) => void;
1010

11+
export interface CachedImageURISource extends ImageURISource {
12+
uri: string;
13+
}
14+
1115
type CacheEntry = {
16+
source: CachedImageURISource;
1217
downloading: boolean;
1318
handlers: CacheHandler[];
1419
path: string | undefined;
@@ -40,20 +45,18 @@ export class ImageCache {
4045
return ImageCache.instance;
4146
}
4247

43-
static getCache(): ImageCache {
44-
return ImageCache.get();
45-
}
46-
4748
private cache: { [uri: string]: CacheEntry } = {};
4849

4950
clear() {
5051
this.cache = {};
5152
return RNFetchBlob.fs.unlink(BASE_DIR);
5253
}
5354

54-
on(uri: string, handler: CacheHandler, immutable?: boolean) {
55+
on(source: CachedImageURISource, handler: CacheHandler, immutable?: boolean) {
56+
const {uri} = source;
5557
if (!this.cache[uri]) {
5658
this.cache[uri] = {
59+
source,
5760
downloading: false,
5861
handlers: [handler],
5962
immutable: immutable === true,
@@ -91,11 +94,14 @@ export class ImageCache {
9194
}
9295
}
9396

94-
private download(uri: string, cache: CacheEntry) {
97+
private download(cache: CacheEntry) {
98+
const {source} = cache;
99+
const {uri} = source;
95100
if (!cache.downloading) {
96101
const path = this.getPath(uri, cache.immutable);
97102
cache.downloading = true;
98-
cache.task = RNFetchBlob.config({ path }).fetch("GET", uri, {});
103+
const method = source.method ? source.method : "GET";
104+
cache.task = RNFetchBlob.config({ path }).fetch(method, uri, source.headers);
99105
cache.task.then(() => {
100106
cache.downloading = false;
101107
cache.path = path;
@@ -116,11 +122,11 @@ export class ImageCache {
116122
if (exists) {
117123
this.notify(uri);
118124
} else {
119-
this.download(uri, cache);
125+
this.download(cache);
120126
}
121127
});
122128
} else {
123-
this.download(uri, cache);
129+
this.download(cache);
124130
}
125131

126132
}
@@ -165,11 +171,11 @@ export abstract class BaseCachedImage<P extends CachedImageProps> extends Compon
165171
}
166172
}
167173

168-
private observe(uri: string, mutable: boolean) {
169-
if (uri !== this.uri) {
174+
private observe(source: CachedImageURISource, mutable: boolean) {
175+
if (source.uri !== this.uri) {
170176
this.dispose();
171-
this.uri = uri;
172-
ImageCache.get().on(uri, this.handler, !mutable);
177+
this.uri = source.uri;
178+
ImageCache.get().on(source, this.handler, !mutable);
173179
}
174180
}
175181

@@ -185,15 +191,26 @@ export abstract class BaseCachedImage<P extends CachedImageProps> extends Compon
185191
return props;
186192
}
187193

194+
195+
private checkSource(source: ImageURISource | ImageURISource[]): CachedImageURISource {
196+
if (Array.isArray(source)) {
197+
throw new Error(`Giving multiple URIs to CachedImage is not yet support.
198+
If you want to see this feature supported, please file and issue at
199+
https://github.com/wcandillon/react-native-img-cache`);
200+
} else if (!source.uri) {
201+
throw new Error(`uri property missing ImageURISource parameter.`);
202+
}
203+
return source as CachedImageURISource;
204+
}
205+
188206
componentWillMount() {
189-
const {mutable} = this.props;
190-
const source = this.props.source as ImageURISource;
191-
this.observe(source.uri as string, mutable === true);
207+
const {mutable, source} = this.props;
208+
this.observe(this.checkSource(source), mutable === true);
192209
}
193210

194211
componentWillReceiveProps(nextProps: P) {
195212
const {source, mutable} = nextProps;
196-
this.observe((source as ImageURISource).uri as string, mutable === true);
213+
this.observe(this.checkSource(source), mutable === true);
197214
}
198215

199216
componentWillUnmount() {

0 commit comments

Comments
 (0)