Skip to content

Commit

Permalink
[fix] ImageLoader: ImageUriCache is not kept up to date with loaded i…
Browse files Browse the repository at this point in the history
…mages

Close #2493
  • Loading branch information
kidroca authored and necolas committed Apr 12, 2023
1 parent 36f46b3 commit e722282
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {

function abortPendingRequest() {
if (requestRef.current != null) {
ImageLoader.abort(requestRef.current);
ImageLoader.clear(requestRef.current);
requestRef.current = null;
}
}
Expand Down
17 changes: 9 additions & 8 deletions packages/react-native-web/src/modules/ImageLoader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ let id = 0;
const requests = {};

const ImageLoader = {
abort(requestId: number) {
let image = requests[`${requestId}`];
clear(requestId: number) {
const image = requests[`${requestId}`];
if (image) {
image.onerror = null;
image.onload = null;
image = null;
ImageUriCache.remove(image.src);
image.src = '';
delete requests[`${requestId}`];
}
},
Expand All @@ -102,7 +103,7 @@ const ImageLoader = {
}
}
if (complete) {
ImageLoader.abort(requestId);
ImageLoader.clear(requestId);
clearInterval(interval);
}
}
Expand All @@ -111,7 +112,7 @@ const ImageLoader = {
if (typeof failure === 'function') {
failure();
}
ImageLoader.abort(requestId);
ImageLoader.clear(requestId);
clearInterval(interval);
}
},
Expand All @@ -123,6 +124,7 @@ const ImageLoader = {
const image = new window.Image();
image.onerror = onError;
image.onload = (nativeEvent) => {
ImageUriCache.add(uri);
// avoid blocking the main thread
const onDecode = () => {
// Append `source` to match RN's ImageLoadEvent interface
Expand Down Expand Up @@ -185,9 +187,8 @@ const ImageLoader = {
ImageLoader.load(
uri,
() => {
// Add the uri to the cache so it can be immediately displayed when used
// but also immediately remove it to correctly reflect that it has no active references
ImageUriCache.add(uri);
// load() adds the uri to the cache so it can be immediately displayed when used,
// but we also immediately remove it to correctly reflect that it has no active references
ImageUriCache.remove(uri);
resolve();
},
Expand Down

0 comments on commit e722282

Please sign in to comment.