Skip to content

Commit 38634e0

Browse files
committed
Reducing changes
1 parent 33cb405 commit 38634e0

File tree

6 files changed

+54
-140
lines changed

6 files changed

+54
-140
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports[`components/Image prop "defaultSource" does not override "height" and "w
5656
exports[`components/Image prop "defaultSource" sets "height" and "width" styles if missing 1`] = `
5757
<div
5858
class="css-view-175oi2r r-flexBasis-1mlwlqe r-overflow-1udh08x r-zIndex-417010"
59-
style="height: 10px; width: 20px;"
59+
style="width: 20px; height: 10px;"
6060
>
6161
<div
6262
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"
@@ -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-98);"
332+
style="filter: url(#tint-97);"
333333
/>
334334
<svg
335335
style="position: absolute; height: 0px; visibility: hidden; width: 0px;"
336336
>
337337
<defs>
338338
<filter
339-
id="tint-98"
339+
id="tint-97"
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-96);"
381+
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-95);"
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-96"
394+
id="tint-95"
395395
>
396396
<feflood
397397
flood-color="red"

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ describe('components/Image', () => {
174174
});
175175

176176
test('is called on update if "headers" are modified', () => {
177-
const onLoadStartStub = jest.fn();
178177
const onLoadStub = jest.fn();
179178
const onLoadEndStub = jest.fn();
180179
const { rerender } = render(
181180
<Image
182181
onLoad={onLoadStub}
183182
onLoadEnd={onLoadEndStub}
184-
onLoadStart={onLoadStartStub}
185183
source={{
186184
uri: 'https://test.com/img.jpg',
187185
headers: { 'x-custom-header': 'abc123' }
@@ -193,7 +191,6 @@ describe('components/Image', () => {
193191
<Image
194192
onLoad={onLoadStub}
195193
onLoadEnd={onLoadEndStub}
196-
onLoadStart={onLoadStartStub}
197194
source={{
198195
uri: 'https://test.com/img.jpg',
199196
headers: { 'x-custom-header': '123abc' }
@@ -260,14 +257,12 @@ describe('components/Image', () => {
260257
// This test verifies that when source is declared in-line and the parent component
261258
// re-renders we aren't restarting the load process because the source is structurally equal
262259
test('is not called on update when "headers" and "uri" are not modified', () => {
263-
const onLoadStartStub = jest.fn();
264260
const onLoadStub = jest.fn();
265261
const onLoadEndStub = jest.fn();
266262
const { rerender } = render(
267263
<Image
268264
onLoad={onLoadStub}
269265
onLoadEnd={onLoadEndStub}
270-
onLoadStart={onLoadStartStub}
271266
source={{
272267
uri: 'https://test.com/img.jpg',
273268
width: 1,
@@ -281,7 +276,6 @@ describe('components/Image', () => {
281276
<Image
282277
onLoad={onLoadStub}
283278
onLoadEnd={onLoadEndStub}
284-
onLoadStart={onLoadStartStub}
285279
source={{
286280
uri: 'https://test.com/img.jpg',
287281
width: 1,
@@ -297,15 +291,11 @@ describe('components/Image', () => {
297291

298292
test('is not called for default source', () => {
299293
jest.useFakeTimers();
300-
const onLoadStartStub = jest.fn();
301294
const onLoadStub = jest.fn();
302-
const onLoadEndStub = jest.fn();
303295
render(
304296
<Image
305297
defaultSource="https://test.com/img-2.jpg"
306298
onLoad={onLoadStub}
307-
onLoadEnd={onLoadEndStub}
308-
onLoadStart={onLoadStartStub}
309299
source="https://test.com/img.jpg"
310300
/>
311301
);
@@ -340,7 +330,6 @@ describe('components/Image', () => {
340330
null,
341331
'',
342332
{},
343-
[],
344333
{ uri: '' },
345334
{ uri: 'https://google.com' },
346335
{ uri: 'https://google.com', headers: { 'x-custom-header': 'abc123' } }
@@ -484,7 +473,7 @@ describe('components/Image', () => {
484473
test('it still loads the image if source object is changed', () => {
485474
ImageLoader.load.mockImplementation(() => {});
486475

487-
const releaseSpy = jest.spyOn(ImageLoader, 'release');
476+
const releaseSpy = jest.spyOn(ImageLoader, 'abort');
488477

489478
const uri = 'https://google.com/favicon.ico';
490479
const { rerender } = render(<Image source={{ uri }} />);

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

+16-45
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
* @flow
99
*/
1010

11-
import type { ImageResult, ImageSource } from '../../modules/ImageLoader';
12-
import type {
13-
ImageLoadingProps,
14-
ImageProps,
15-
Source,
16-
SourceState
17-
} from './types';
11+
import type { ImageSource } from '../../modules/ImageLoader';
12+
import type { ImageProps, Source } from './types';
1813

1914
import * as React from 'react';
2015
import createElement from '../createElement';
@@ -100,11 +95,6 @@ function getFlatStyle(style, blurRadius, filterId) {
10095
return [flatStyle, resizeMode, _filter, tintColor];
10196
}
10297

103-
function resolveAssetDimensions(source: ImageSource) {
104-
const { height, width } = source;
105-
return { height, width };
106-
}
107-
10898
function resolveSource(source: ?Source): ImageSource {
10999
let resolvedSource = { uri: '' };
110100

@@ -132,16 +122,8 @@ function resolveSource(source: ?Source): ImageSource {
132122
resolvedSource = { uri, width: asset.width, height: asset.height };
133123
} else if (typeof source === 'string') {
134124
resolvedSource.uri = source;
135-
} else if (Array.isArray(source)) {
136-
if (process.env.NODE_ENV !== 'production') {
137-
console.warn(
138-
'The <Image> component does not support multiple sources passed as array, falling back to the first source in the list',
139-
{ source }
140-
);
141-
}
142-
143-
return resolveSource(source[0]);
144125
} else if (source && typeof source.uri === 'string') {
126+
// $FlowFixMe
145127
const { uri, width, height, headers } = source;
146128
resolvedSource = { uri, width, height, headers };
147129
}
@@ -159,19 +141,6 @@ function resolveSource(source: ?Source): ImageSource {
159141
return resolvedSource;
160142
}
161143

162-
function getSourceToDisplay(main: SourceState, fallback: SourceState) {
163-
if (main.status === LOADED) return main.source;
164-
165-
// If there's no fallback URI, it's safe to use the main source URI
166-
if (main.status === LOADING && !fallback.source.uri) {
167-
// But it should not be used when the image would be loaded with custom headers
168-
// Because the actual URI is only set (as a local blob url) after loading
169-
if (!main.source.headers) return main.source;
170-
}
171-
172-
return fallback.source;
173-
}
174-
175144
interface ImageStatics {
176145
getSize: (
177146
uri: string,
@@ -212,7 +181,9 @@ const Image: React.AbstractComponent<
212181
}
213182
}
214183

215-
// Don't raise load events for the fallback source
184+
// Only the main source is supposed to trigger onLoad/start/end events
185+
// It would be ambiguous to trigger the same `onLoad` event when default source loads
186+
// That's why we don't pass `onLoad` props for the fallback source hook
216187
const fallbackSource = useSource({ onError }, defaultSource);
217188
const mainSource = useSource(
218189
{ onLoad, onLoadStart, onLoadEnd, onError },
@@ -222,15 +193,17 @@ const Image: React.AbstractComponent<
222193
const hasTextAncestor = React.useContext(TextAncestorContext);
223194
const hiddenImageRef = React.useRef(null);
224195
const filterRef = React.useRef(_filterId++);
196+
const shouldDisplaySource =
197+
mainSource.status === LOADED ||
198+
(mainSource.status === LOADING && defaultSource == null);
225199
const [flatStyle, _resizeMode, filter, tintColor] = getFlatStyle(
226200
style,
227201
blurRadius,
228202
filterRef.current
229203
);
230204
const resizeMode = props.resizeMode || _resizeMode || 'cover';
231-
const availableSource = getSourceToDisplay(mainSource, fallbackSource);
232-
const displayImageUri = ImageLoader.resolveUri(availableSource.uri);
233-
const imageSizeStyle = resolveAssetDimensions(availableSource);
205+
const selected = shouldDisplaySource ? mainSource : fallbackSource;
206+
const displayImageUri = selected.source.uri;
234207
const backgroundImage = displayImageUri ? `url("${displayImageUri}")` : null;
235208
const backgroundSize = getBackgroundSize();
236209

@@ -283,7 +256,7 @@ const Image: React.AbstractComponent<
283256
style={[
284257
styles.root,
285258
hasTextAncestor && styles.inline,
286-
imageSizeStyle,
259+
{ width: selected.source.width, height: selected.source.height },
287260
flatStyle
288261
]}
289262
>
@@ -326,10 +299,7 @@ ImageWithStatics.queryCache = function (uris) {
326299
/**
327300
* Image loading/state management hook
328301
*/
329-
const useSource = (
330-
callbacks: ImageLoadingProps,
331-
source: ?Source
332-
): SourceState => {
302+
const useSource = (callbacks, source: ?Source) => {
333303
const [resolvedSource, setResolvedSource] = React.useState<ImageSource>(() =>
334304
resolveSource(source)
335305
);
@@ -369,8 +339,9 @@ const useSource = (
369339
return;
370340
}
371341

342+
// $FlowFixMe
372343
const { onLoad, onLoadStart, onLoadEnd, onError } = callbackRefs.current;
373-
function handleLoad(result: ImageResult) {
344+
function handleLoad(result) {
374345
if (onLoad) onLoad({ nativeEvent: result });
375346
if (onLoadEnd) onLoadEnd();
376347

@@ -398,7 +369,7 @@ const useSource = (
398369
const requestId = ImageLoader.load(resolvedSource, handleLoad, handleError);
399370

400371
// Release resources on unmount or after starting a new request
401-
return () => ImageLoader.release(requestId);
372+
return () => ImageLoader.abort(requestId);
402373
}, [resolvedSource]);
403374

404375
return { status, source: result };

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* @flow
99
*/
1010

11-
import type { ImageResult, ImageSource } from '../../modules/ImageLoader';
1211
import type { ColorValue, GenericStyleProp } from '../../types';
1312
import type { ViewProps } from '../View/types';
1413

@@ -103,26 +102,17 @@ export type ImageStyle = {
103102
tintColor?: ColorValue
104103
};
105104

106-
export type SourceState = {
107-
status: 'IDLE' | 'LOADING' | 'LOADED' | 'ERRORED',
108-
source: ImageSource
109-
};
110-
111-
export type ImageLoadingProps = {|
112-
onLoad?: (e: { nativeEvent: ImageResult }) => void,
113-
onLoadStart?: () => void,
114-
onLoadEnd?: () => void,
115-
onError?: ({ nativeEvent: { error: string } }) => void,
116-
onProgress?: (e: any) => void
117-
|};
118-
119105
export type ImageProps = {
120-
...$Exact<ViewProps>,
121-
...ImageLoadingProps,
106+
...ViewProps,
122107
blurRadius?: number,
123108
defaultSource?: Source,
124109
draggable?: boolean,
110+
onError?: (e: any) => void,
125111
onLayout?: (e: any) => void,
112+
onLoad?: (e: any) => void,
113+
onLoadEnd?: (e: any) => void,
114+
onLoadStart?: (e: any) => void,
115+
onProgress?: (e: any) => void,
126116
resizeMode?: ResizeMode,
127117
source?: Source,
128118
style?: GenericStyleProp<ImageStyle>

0 commit comments

Comments
 (0)