@@ -145,51 +145,73 @@ export default function imageCacheHoc(Image, options = {}) {
145
145
// Track component mount status to avoid calling setState() on unmounted component.
146
146
this . _isMounted = true ;
147
147
148
+ // Set url from source prop
149
+ const url = traverse ( this . props ) . get ( [ 'source' , 'uri' ] ) ;
150
+
148
151
// Add a cache lock to file with this name (prevents concurrent <CacheableImage> components from pruning a file with this name from cache).
149
- const fileName = await this . fileSystem . getFileNameFromUrl ( traverse ( this . props ) . get ( [ 'source' , 'uri' ] ) ) ;
152
+ const fileName = await this . fileSystem . getFileNameFromUrl ( url ) ;
150
153
FileSystem . lockCacheFile ( fileName , this . componentId ) ;
151
154
152
- // Check local fs for file, fallback to network and write file to disk if local file not found.
153
- const permanent = this . props . permanent ? true : false ;
154
- let localFilePath = null ;
155
- try {
156
- localFilePath = await this . fileSystem . getLocalFilePathFromUrl ( traverse ( this . props ) . get ( [ 'source' , 'uri' ] ) , permanent ) ;
157
- } catch ( error ) {
158
- console . warn ( error ) ; // eslint-disable-line no-console
159
- }
160
-
161
- // Check component is still mounted to avoid calling setState() on components that were quickly
162
- // mounted then unmounted before componentDidMount() finishes.
163
- // See: https://github.com/billmalarky/react-native-image-cache-hoc/issues/6#issuecomment-354490597
164
- if ( this . _isMounted && localFilePath ) {
165
- this . setState ( { localFilePath } ) ;
166
- }
155
+ // Init the image cache logic
156
+ await this . _loadImage ( url ) ;
167
157
168
158
}
169
159
160
+ /**
161
+ *
162
+ * Enables caching logic to work if component source prop is updated (that is, the image url changes without mounting a new component).
163
+ * See: https://github.com/billmalarky/react-native-image-cache-hoc/pull/15
164
+ *
165
+ * @param nextProps {Object} - Props that will be passed to component.
166
+ */
170
167
async componentWillReceiveProps ( nextProps ) {
171
- const uri = traverse ( this . props ) . get ( [ 'source' , 'uri' ] ) ;
172
- const nextUri = traverse ( nextProps ) . get ( [ 'source' , 'uri' ] ) ;
173
- if ( uri === nextUri ) return ;
174
168
175
- const fileName = await this . fileSystem . getFileNameFromUrl ( uri ) ;
176
- const nextFileName = await this . fileSystem . getFileNameFromUrl ( nextUri ) ;
169
+ // Set urls from source prop data
170
+ const url = traverse ( this . props ) . get ( [ 'source' , 'uri' ] ) ;
171
+ const nextUrl = traverse ( nextProps ) . get ( [ 'source' , 'uri' ] ) ;
172
+
173
+ // Do nothing if url has not changed.
174
+ if ( url === nextUrl ) return ;
175
+
176
+ // Remove component cache lock on old image file, and add cache lock to new image file.
177
+ const fileName = await this . fileSystem . getFileNameFromUrl ( url ) ;
178
+ const nextFileName = await this . fileSystem . getFileNameFromUrl ( nextUrl ) ;
177
179
178
180
FileSystem . unlockCacheFile ( fileName , this . componentId ) ;
179
181
FileSystem . lockCacheFile ( nextFileName , this . componentId ) ;
180
182
181
- const { permanent } = this . props ;
183
+ // Init the image cache logic
184
+ await this . _loadImage ( nextUrl ) ;
185
+
186
+ }
187
+
188
+ /**
189
+ *
190
+ * Executes the image download/cache logic and calls setState() with to re-render
191
+ * component using local file path on completion.
192
+ *
193
+ * @param url {String} - The remote image url.
194
+ * @private
195
+ */
196
+ async _loadImage ( url ) {
197
+
198
+ // Check local fs for file, fallback to network and write file to disk if local file not found.
199
+ const permanent = this . props . permanent ? true : false ;
182
200
let localFilePath = null ;
183
201
try {
184
- localFilePath = await this . fileSystem . getLocalFilePathFromUrl ( nextUri , ! ! permanent ) ;
202
+ localFilePath = await this . fileSystem . getLocalFilePathFromUrl ( url , permanent ) ;
185
203
} catch ( error ) {
186
204
console . warn ( error ) ; // eslint-disable-line no-console
187
205
}
188
206
207
+ // Check component is still mounted to avoid calling setState() on components that were quickly
208
+ // mounted then unmounted before componentDidMount() finishes.
209
+ // See: https://github.com/billmalarky/react-native-image-cache-hoc/issues/6#issuecomment-354490597
189
210
if ( this . _isMounted && localFilePath ) {
190
211
this . setState ( { localFilePath } ) ;
191
212
}
192
- }
213
+
214
+ }
193
215
194
216
async componentWillUnmount ( ) {
195
217
0 commit comments