Skip to content

Commit 2054e0d

Browse files
committed
Cleanup
1 parent 16984f8 commit 2054e0d

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

packages/react-native-web/src/exports/Image/__tests__/__snapshots__/index-test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ exports[`components/Image prop "style" removes other unsupported View styles 1`]
329329
>
330330
<div
331331
class="css-view-175oi2r r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw r-backgroundSize-4gszlv"
332-
style="filter: url(#tint-57);"
332+
style="filter: url(#tint-56);"
333333
/>
334334
<svg
335335
style="position: absolute; height: 0px; visibility: hidden; width: 0px;"
336336
>
337337
<defs>
338338
<filter
339-
id="tint-57"
339+
id="tint-56"
340340
>
341341
<feflood
342342
flood-color="blue"
@@ -378,7 +378,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
378378
>
379379
<div
380380
class="css-view-175oi2r r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw r-backgroundSize-4gszlv"
381-
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-56);"
381+
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-55);"
382382
/>
383383
<img
384384
alt=""
@@ -391,7 +391,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
391391
>
392392
<defs>
393393
<filter
394-
id="tint-56"
394+
id="tint-55"
395395
>
396396
<feflood
397397
flood-color="red"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const originalImage = window.Image;
2020
describe('components/Image', () => {
2121
beforeEach(() => {
2222
ImageUriCache._entries = {};
23-
ImageLoader.load = jest.fn((_, onLoad, onError) => {
23+
ImageLoader.load = jest.fn((source, onLoad, onError) => {
2424
onLoad();
25-
return { cancel: jest.fn() };
25+
return { source, cancel: jest.fn() };
2626
});
2727
window.Image = jest.fn(() => ({}));
2828
});
@@ -304,9 +304,9 @@ describe('components/Image', () => {
304304
let loadCallback;
305305
ImageLoader.load = jest
306306
.fn()
307-
.mockImplementationOnce((_, onLoad, onError) => {
307+
.mockImplementationOnce((source, onLoad, onError) => {
308308
loadCallback = onLoad;
309-
return { cancel: jest.fn() };
309+
return { source, cancel: jest.fn() };
310310
});
311311
const { container } = render(
312312
<Image defaultSource={{ uri: defaultUri }} source={{ uri }} />

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ function resolveAssetUri(source): ?string {
148148
}
149149

150150
function hasSourceDiff(a, b) {
151-
return (
152-
a.uri !== b.uri || JSON.stringify(a.headers) !== JSON.stringify(b.headers)
153-
);
151+
if (a.uri !== b.uri) return true;
152+
153+
const headersA = a.headers || {};
154+
const headersB = b.headers || {};
155+
156+
return JSON.stringify(headersA) !== JSON.stringify(headersB);
154157
}
155158

156159
interface ImageStatics {
@@ -193,7 +196,6 @@ const Image: React.AbstractComponent<
193196
}
194197
}
195198

196-
const lastLoadedSource = React.useRef({});
197199
const [state, updateState] = React.useState(() => {
198200
const uri = resolveAssetUri(source);
199201
if (uri != null) {
@@ -210,6 +212,7 @@ const Image: React.AbstractComponent<
210212
const hiddenImageRef = React.useRef(null);
211213
const filterRef = React.useRef(_filterId++);
212214
const requestRef = React.useRef<LoadRequest>({
215+
source: { uri: '' },
213216
cancel: () => {},
214217
requestId: -1
215218
});
@@ -270,12 +273,12 @@ const Image: React.AbstractComponent<
270273
const uri = resolveAssetUri(source);
271274
React.useEffect(() => {
272275
if (uri != null) {
273-
const nextSource = {
274-
// $FlowFixMe
275-
headers: source.headers,
276-
uri
277-
};
278-
if (!hasSourceDiff(nextSource, lastLoadedSource.current)) return;
276+
const nextSource = { uri, headers: undefined };
277+
if (source && source.headers != null) {
278+
nextSource.headers = source.headers;
279+
}
280+
281+
if (!hasSourceDiff(nextSource, requestRef.current.source)) return;
279282

280283
requestRef.current.cancel();
281284
updateState(LOADING);
@@ -313,8 +316,11 @@ const Image: React.AbstractComponent<
313316
}
314317
}
315318
);
319+
} else {
320+
requestRef.current.source = { uri: '' };
321+
requestRef.current.cancel();
316322
}
317-
}, [uri, requestRef, updateState, onError, onLoad, onLoadEnd, onLoadStart]);
323+
}, [uri, updateState, onError, onLoad, onLoadEnd, onLoadStart, source]);
318324

319325
// Run the cleanup function on unmount
320326
React.useEffect(() => requestRef.current.cancel, []);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const ImageLoader = {
129129
image.onload = (nativeEvent) => {
130130
// avoid blocking the main thread
131131
const onDecode = () => {
132+
// Append `source` to match RN's ImageLoadEvent interface
132133
nativeEvent.source = {
133134
uri: image.src,
134135
width: image.naturalWidth,
@@ -150,6 +151,7 @@ const ImageLoader = {
150151
requests[`${id}`] = image;
151152

152153
return {
154+
source,
153155
cancel: () => ImageLoader.abort(id),
154156
requestId: id
155157
};
@@ -176,11 +178,12 @@ const ImageLoader = {
176178
})
177179
.catch((error) => {
178180
if (error.name !== 'AbortError' && onError) {
179-
onError({ nativeEvent: error.message });
181+
onError(error);
180182
}
181183
});
182184

183185
return {
186+
source,
184187
get requestId() {
185188
if (loadRequest) return loadRequest.requestId;
186189
return -1;
@@ -218,10 +221,11 @@ const ImageLoader = {
218221
}
219222
};
220223

221-
export type LoadRequest = {
222-
cancel: Function,
223-
requestId: number
224-
};
224+
export type LoadRequest = {|
225+
requestId: number,
226+
source: ImageSource | { uri: string },
227+
cancel: Function
228+
|};
225229

226230
type ImageSource = {
227231
uri: string,

0 commit comments

Comments
 (0)