Skip to content

Commit

Permalink
Address flow type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed Nov 25, 2022
1 parent fe3a953 commit f0e6cc8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
37 changes: 16 additions & 21 deletions packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import type { ImageResult } from '../../modules/ImageLoader';
import type { ImageResult, ImageSource } from '../../modules/ImageLoader';
import type { ImageProps, Source, ImageLoadingProps } from './types';

import * as React from 'react';
Expand Down Expand Up @@ -100,7 +100,7 @@ function resolveAssetDimensions(source: ImageSource) {
return { height, width };
}

function resolveSource(source: ?Source): Source {
function resolveSource(source: ?Source): ImageSource {
let resolvedSource = { uri: '' };

if (typeof source === 'number') {
Expand All @@ -117,7 +117,7 @@ function resolveSource(source: ?Source): Source {

return resolveSource(source[0]);
} else if (source && typeof source.uri === 'string') {
resolvedSource = resolveObjectSource((source: Source));
resolvedSource = resolveObjectSource(source);
}

if (resolvedSource.uri) {
Expand Down Expand Up @@ -153,25 +153,22 @@ function resolveNumericSource(source: number): ImageSource {

const scaleSuffix = scale !== 1 ? `@${scale}x` : '';
const uri = `${asset.httpServerLocation}/${asset.name}${scaleSuffix}.${asset.type}`;
const { height, width } = asset;

return {
uri,
width: asset.width,
height: asset.height
};
return { uri, height, width };
}

function resolveStringSource(source: string): ImageSource {
return { uri: source };
}

function resolveObjectSource(source: Source): ImageSource {
return source;
function resolveObjectSource(source: Object): ImageSource {
return (source: ImageSource);
}

function resolveSvgDataUriSource(
source: Source,
match: RegExpMatchArray
source: Object,
match: Array<string>
): ImageSource {
const [, prefix, svg] = match;
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
Expand All @@ -194,10 +191,10 @@ function resolveBlobUri(source: ImageSource): ImageSource {
function getSourceToDisplay(main, fallback) {
if (main.status === LOADED) return main.source;

if (main.satus === LOADING && !fallback.source.uri) {
if (main.status === LOADING && !fallback.source.uri) {
// Most of the time it's safe to use the main URI as img.src before loading
// But it should not be used when the image would be loaded using `fetch` with headers
if (!main.headers) return main.source;
if (!main.source.headers) return main.source;
}

return fallback.source;
Expand Down Expand Up @@ -353,27 +350,25 @@ ImageWithStatics.queryCache = function (uris) {
return ImageLoader.queryCache(uris);
};

// Todo: see if we can just use `result` as `source` (width|height might cause problems)

/**
* Image loading/state management hook
* @param callbacks
* @param source
* @returns {{state: string, uri: string}}
* @returns {{status: string, source: ImageSource}}
*/
const useSource = (
callbacks: ImageLoadingProps,
source: ?Source
): { status: IDLE | LOADING | LOADED | ERRORED, source: ImageSource } => {
const [resolvedSource, setResolvedSource] = React.useState(() =>
): { status: string, source: ImageSource } => {
const [resolvedSource, setResolvedSource] = React.useState<ImageSource>(() =>
resolveSource(source)
);

const [status, setStatus] = React.useState(() =>
ImageLoader.has(resolveSource.uri) ? LOADED : IDLE
);

const [result, setResult] = React.useState(resolvedSource);
const [result, setResult] = React.useState<ImageSource>(resolvedSource);

// Trigger a resolved source change when necessary
React.useEffect(() => {
Expand Down Expand Up @@ -406,7 +401,7 @@ const useSource = (
if (onLoadEnd) onLoadEnd();

setStatus(LOADED);
setResult(result.source);
setResult({ ...resolvedSource, ...result.source });
}

function handleError() {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-web/src/exports/Image/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export type ImageStyle = {
};

export type ImageLoadingProps = {|
onLoad?: (e: ImageResult) => void,
onLoad?: (e: { nativeEvent: ImageResult }) => void,
onLoadStart?: () => void,
onLoadEnd?: () => void,
onError?: ({ nativeEvent: { error: string } }) => void,
Expand Down
10 changes: 5 additions & 5 deletions packages/react-native-web/src/modules/ImageLoader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ const ImageLoader = {
return Promise.resolve(result);
},
resolveBlobUri(uri: string): string {
const request = Object.values(requests).find(
({ source }) => source.uri === uri
);
if (request) {
return request.image.src;
for (const key in requests) {
const request = requests[key];
if (request.source.uri === uri) {
return request.image.src;
}
}

return uri;
Expand Down
8 changes: 5 additions & 3 deletions packages/react-native-web/src/modules/ImageLoader/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

export type ImageSource = {|
uri: string,
headers?: { [key: string]: string }
headers?: { [key: string]: string },
width?: ?number,
height?: ?number
|};

export type ImageResult = {|
source: {
source: {|
uri: string,
width: number,
height: number
}
|}
|};

0 comments on commit f0e6cc8

Please sign in to comment.