Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Update Image.getSize/getSizeWithHeaders methods to return a promise #42895

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module.exports = {
files: ['**/*.d.ts'],
plugins: ['redundant-undefined'],
rules: {
'no-dupe-class-members': 'off',
'redundant-undefined/redundant-undefined': [
'error',
{followExactOptionalPropertyTypes: true},
Expand Down
32 changes: 19 additions & 13 deletions packages/react-native/Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
import {getImageSourcesFromImageProps} from './ImageSourceUtils';
import {convertObjectFitToResizeMode} from './ImageUtils';
import ImageViewNativeComponent from './ImageViewNativeComponent';
import NativeImageLoaderAndroid from './NativeImageLoaderAndroid';
import NativeImageLoaderAndroid, {
type ImageSize,
} from './NativeImageLoaderAndroid';
import resolveAssetSource from './resolveAssetSource';
import TextInlineImageNativeComponent from './TextInlineImageNativeComponent';
import * as React from 'react';
Expand All @@ -40,13 +42,15 @@ function generateRequestId() {
*/
function getSize(
url: string,
success: (width: number, height: number) => void,
success?: (width: number, height: number) => void,
failure?: (error: mixed) => void,
): void {
NativeImageLoaderAndroid.getSize(url)
.then(function (sizes) {
success(sizes.width, sizes.height);
})
): void | Promise<ImageSize> {
const promise = NativeImageLoaderAndroid.getSize(url);
if (typeof success !== 'function') {
return promise;
}
promise
.then(sizes => success(sizes.width, sizes.height))
.catch(
failure ||
function () {
Expand All @@ -64,13 +68,15 @@ function getSize(
function getSizeWithHeaders(
url: string,
headers: {[string]: string, ...},
success: (width: number, height: number) => void,
success?: (width: number, height: number) => void,
failure?: (error: mixed) => void,
): void {
NativeImageLoaderAndroid.getSizeWithHeaders(url, headers)
.then(function (sizes) {
success(sizes.width, sizes.height);
})
): void | Promise<ImageSize> {
const promise = NativeImageLoaderAndroid.getSizeWithHeaders(url, headers);
if (typeof success !== 'function') {
return promise;
}
promise
.then(sizes => success(sizes.width, sizes.height))
.catch(
failure ||
function () {
Expand Down
15 changes: 13 additions & 2 deletions packages/react-native/Libraries/Image/Image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,20 +325,31 @@ export interface ImageProps extends ImagePropsBase {
style?: StyleProp<ImageStyle> | undefined;
}

export interface ImageSize {
width: number;
height: number;
}

declare class ImageComponent extends React.Component<ImageProps> {}
declare const ImageBase: Constructor<NativeMethods> & typeof ImageComponent;
export class Image extends ImageBase {
static getSize(uri: string): Promise<ImageSize>;
static getSize(
uri: string,
success: (width: number, height: number) => void,
failure?: (error: any) => void,
): any;
): void;

static getSizeWithHeaders(
uri: string,
headers: {[index: string]: string},
): Promise<ImageSize>;
static getSizeWithHeaders(
uri: string,
headers: {[index: string]: string},
success: (width: number, height: number) => void,
failure?: (error: any) => void,
): any;
): void;
static prefetch(url: string): Promise<boolean>;
static prefetchWithMetadata(
url: string,
Expand Down
32 changes: 21 additions & 11 deletions packages/react-native/Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {ImageStyle, ImageStyleProp} from '../StyleSheet/StyleSheet';
import type {RootTag} from '../Types/RootTagTypes';
import type {AbstractImageIOS, ImageIOS} from './ImageTypes.flow';
import type {ImageSize} from './NativeImageLoaderAndroid';

import {createRootTag} from '../ReactNative/RootTag';
import flattenStyle from '../StyleSheet/flattenStyle';
Expand All @@ -29,29 +30,38 @@ import * as React from 'react';

function getSize(
uri: string,
success: (width: number, height: number) => void,
success?: (width: number, height: number) => void,
failure?: (error: mixed) => void,
): void {
NativeImageLoaderIOS.getSize(uri)
.then(([width, height]) => success(width, height))
): void | Promise<ImageSize> {
const promise = NativeImageLoaderIOS.getSize(uri).then(([width, height]) => ({
width,
height,
}));
if (typeof success !== 'function') {
return promise;
}
promise
.then(sizes => success(sizes.width, sizes.height))
.catch(
failure ||
function () {
console.warn('Failed to get size for image ' + uri);
console.warn('Failed to get size for image: ' + uri);
},
);
}

function getSizeWithHeaders(
uri: string,
headers: {[string]: string, ...},
success: (width: number, height: number) => void,
success?: (width: number, height: number) => void,
failure?: (error: mixed) => void,
): void {
NativeImageLoaderIOS.getSizeWithHeaders(uri, headers)
.then(function (sizes) {
success(sizes.width, sizes.height);
})
): void | Promise<ImageSize> {
const promise = NativeImageLoaderIOS.getSizeWithHeaders(uri, headers);
if (typeof success !== 'function') {
return promise;
}
promise
.then(sizes => success(sizes.width, sizes.height))
.catch(
failure ||
function () {
Expand Down
9 changes: 7 additions & 2 deletions packages/react-native/Libraries/Image/ImageTypes.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ import typeof TextInlineImageNativeComponent from './TextInlineImageNativeCompon
import * as React from 'react';

type ImageComponentStaticsIOS = $ReadOnly<{
getSize: (
getSize(uri: string): Promise<{width: number, height: number}>,
getSize(
uri: string,
success: (width: number, height: number) => void,
failure?: (error: mixed) => void,
) => void,
): void,

getSizeWithHeaders(
uri: string,
headers: {[string]: string, ...},
): Promise<{width: number, height: number}>,
getSizeWithHeaders(
uri: string,
headers: {[string]: string, ...},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4478,11 +4478,16 @@ exports[`public API should not change unintentionally Libraries/Image/ImageSourc

exports[`public API should not change unintentionally Libraries/Image/ImageTypes.flow.js 1`] = `
"type ImageComponentStaticsIOS = $ReadOnly<{
getSize: (
getSize(uri: string): Promise<{ width: number, height: number }>,
getSize(
uri: string,
success: (width: number, height: number) => void,
failure?: (error: mixed) => void
) => void,
): void,
getSizeWithHeaders(
uri: string,
headers: { [string]: string, ... }
): Promise<{ width: number, height: number }>,
getSizeWithHeaders(
uri: string,
headers: { [string]: string, ... },
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/types/__typetests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,18 @@ export class ImageTest extends React.Component {
}
});

const promise1: Promise<any> = Image.getSize(uri).then(({width, height}) =>
console.log(width, height),
);
Image.getSize(uri, (width, height) => console.log(width, height));
Image.getSize(
uri,
(width, height) => console.log(width, height),
error => console.error(error),
);
const promise2: Promise<any> = Image.getSizeWithHeaders(uri, headers).then(
({width, height}) => console.log(width, height),
);
Image.getSizeWithHeaders(uri, headers, (width, height) =>
console.log(width, height),
);
Expand Down
Loading