@@ -8,7 +8,12 @@ const BASE_DIR = RNFetchBlob.fs.dirs.CacheDir + "/react-native-img-cache";
8
8
const FILE_PREFIX = Platform . OS === "ios" ? "" : "file://" ;
9
9
export type CacheHandler = ( path : string ) => void ;
10
10
11
+ export interface CachedImageURISource extends ImageURISource {
12
+ uri : string ;
13
+ }
14
+
11
15
type CacheEntry = {
16
+ source : CachedImageURISource ;
12
17
downloading : boolean ;
13
18
handlers : CacheHandler [ ] ;
14
19
path : string | undefined ;
@@ -40,20 +45,18 @@ export class ImageCache {
40
45
return ImageCache . instance ;
41
46
}
42
47
43
- static getCache ( ) : ImageCache {
44
- return ImageCache . get ( ) ;
45
- }
46
-
47
48
private cache : { [ uri : string ] : CacheEntry } = { } ;
48
49
49
50
clear ( ) {
50
51
this . cache = { } ;
51
52
return RNFetchBlob . fs . unlink ( BASE_DIR ) ;
52
53
}
53
54
54
- on ( uri : string , handler : CacheHandler , immutable ?: boolean ) {
55
+ on ( source : CachedImageURISource , handler : CacheHandler , immutable ?: boolean ) {
56
+ const { uri} = source ;
55
57
if ( ! this . cache [ uri ] ) {
56
58
this . cache [ uri ] = {
59
+ source,
57
60
downloading : false ,
58
61
handlers : [ handler ] ,
59
62
immutable : immutable === true ,
@@ -91,11 +94,14 @@ export class ImageCache {
91
94
}
92
95
}
93
96
94
- private download ( uri : string , cache : CacheEntry ) {
97
+ private download ( cache : CacheEntry ) {
98
+ const { source} = cache ;
99
+ const { uri} = source ;
95
100
if ( ! cache . downloading ) {
96
101
const path = this . getPath ( uri , cache . immutable ) ;
97
102
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 ) ;
99
105
cache . task . then ( ( ) => {
100
106
cache . downloading = false ;
101
107
cache . path = path ;
@@ -116,11 +122,11 @@ export class ImageCache {
116
122
if ( exists ) {
117
123
this . notify ( uri ) ;
118
124
} else {
119
- this . download ( uri , cache ) ;
125
+ this . download ( cache ) ;
120
126
}
121
127
} ) ;
122
128
} else {
123
- this . download ( uri , cache ) ;
129
+ this . download ( cache ) ;
124
130
}
125
131
126
132
}
@@ -165,11 +171,11 @@ export abstract class BaseCachedImage<P extends CachedImageProps> extends Compon
165
171
}
166
172
}
167
173
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 ) {
170
176
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 ) ;
173
179
}
174
180
}
175
181
@@ -185,15 +191,26 @@ export abstract class BaseCachedImage<P extends CachedImageProps> extends Compon
185
191
return props ;
186
192
}
187
193
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
+
188
206
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 ) ;
192
209
}
193
210
194
211
componentWillReceiveProps ( nextProps : P ) {
195
212
const { source, mutable} = nextProps ;
196
- this . observe ( ( source as ImageURISource ) . uri as string , mutable === true ) ;
213
+ this . observe ( this . checkSource ( source ) , mutable === true ) ;
197
214
}
198
215
199
216
componentWillUnmount ( ) {
0 commit comments