Skip to content

Commit 7ceed18

Browse files
authored
Merge pull request #15 from kidroca/kidroca/fix/image-caching-expensify
Fix Image Caching
2 parents e9b7517 + 37264cb commit 7ceed18

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

packages/react-native-web/src/exports/Image/index.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,18 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
211211
}
212212
}
213213

214-
const [state, updateState] = React.useState(() => {
215-
const uri = resolveAssetUri(source);
216-
if (uri != null) {
217-
const isLoaded = ImageLoader.has(uri);
218-
if (isLoaded) {
219-
return LOADED;
220-
}
221-
}
222-
return IDLE;
223-
});
224-
214+
const [state, updateState] = React.useState(IDLE);
225215
const [layout, updateLayout] = React.useState({});
226216
const hasTextAncestor = React.useContext(TextAncestorContext);
227217
const hiddenImageRef = React.useRef(null);
228218
const filterRef = React.useRef(_filterId++);
229219
const requestRef = React.useRef(null);
220+
const uri = resolveAssetUri(source);
221+
const isCached = uri != null && ImageLoader.has(uri);
230222
const shouldDisplaySource =
231-
state === LOADED || (state === LOADING && defaultSource == null);
223+
state === LOADED ||
224+
isCached ||
225+
(state === LOADING && defaultSource == null);
232226
const [flatStyle, _resizeMode, filter, tintColor] = getFlatStyle(
233227
style,
234228
blurRadius,
@@ -281,7 +275,6 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
281275
}
282276

283277
// Image loading
284-
const uri = resolveAssetUri(source);
285278
React.useEffect(() => {
286279
abortPendingRequest();
287280

@@ -311,7 +304,7 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
311304

312305
function abortPendingRequest() {
313306
if (requestRef.current != null) {
314-
ImageLoader.abort(requestRef.current);
307+
ImageLoader.clear(requestRef.current);
315308
requestRef.current = null;
316309
}
317310
}

packages/react-native-web/src/modules/ImageLoader/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ let id = 0;
7474
const requests = {};
7575

7676
const ImageLoader = {
77-
abort(requestId: number) {
78-
let image = requests[`${requestId}`];
77+
clear(requestId: number) {
78+
const image = requests[`${requestId}`];
7979
if (image) {
8080
image.onerror = null;
8181
image.onload = null;
82-
image = null;
82+
ImageUriCache.remove(image.src);
83+
image.src = '';
8384
delete requests[`${requestId}`];
8485
}
8586
},
@@ -102,7 +103,7 @@ const ImageLoader = {
102103
}
103104
}
104105
if (complete) {
105-
ImageLoader.abort(requestId);
106+
ImageLoader.clear(requestId);
106107
clearInterval(interval);
107108
}
108109
}
@@ -111,7 +112,7 @@ const ImageLoader = {
111112
if (typeof failure === 'function') {
112113
failure();
113114
}
114-
ImageLoader.abort(requestId);
115+
ImageLoader.clear(requestId);
115116
clearInterval(interval);
116117
}
117118
},
@@ -123,6 +124,7 @@ const ImageLoader = {
123124
const image = new window.Image();
124125
image.onerror = onError;
125126
image.onload = (nativeEvent) => {
127+
ImageUriCache.add(uri);
126128
// avoid blocking the main thread
127129
const onDecode = () => {
128130
// Append `source` to match RN's ImageLoadEvent interface
@@ -185,9 +187,8 @@ const ImageLoader = {
185187
ImageLoader.load(
186188
uri,
187189
() => {
188-
// Add the uri to the cache so it can be immediately displayed when used
189-
// but also immediately remove it to correctly reflect that it has no active references
190-
ImageUriCache.add(uri);
190+
// load() adds the uri to the cache so it can be immediately displayed when used,
191+
// but we also immediately remove it to correctly reflect that it has no active references
191192
ImageUriCache.remove(uri);
192193
resolve();
193194
},

0 commit comments

Comments
 (0)