88 * @flow
99 */
1010
11- import type { ImageResult } from '../../modules/ImageLoader' ;
11+ import type { ImageResult , ImageSource } from '../../modules/ImageLoader' ;
1212import type { ImageProps , Source , ImageLoadingProps } from './types' ;
1313
1414import * as React from 'react' ;
@@ -100,7 +100,7 @@ function resolveAssetDimensions(source: ImageSource) {
100100 return { height, width } ;
101101}
102102
103- function resolveSource ( source : ?Source ) : Source {
103+ function resolveSource ( source : ?Source ) : ImageSource {
104104 let resolvedSource = { uri : '' } ;
105105
106106 if ( typeof source === 'number' ) {
@@ -117,7 +117,7 @@ function resolveSource(source: ?Source): Source {
117117
118118 return resolveSource ( source [ 0 ] ) ;
119119 } else if ( source && typeof source . uri === 'string' ) {
120- resolvedSource = resolveObjectSource ( ( source : Source ) ) ;
120+ resolvedSource = resolveObjectSource ( source ) ;
121121 }
122122
123123 if ( resolvedSource . uri ) {
@@ -135,6 +135,8 @@ function resolveSource(source: ?Source): Source {
135135// get the URI from the packager
136136function resolveNumericSource ( source : number ) : ImageSource {
137137 const asset = getAssetByID ( source ) ;
138+ if ( ! asset ) return { uri : '' } ;
139+
138140 let scale = asset . scales [ 0 ] ;
139141 if ( asset . scales . length > 1 ) {
140142 const preferredScale = PixelRatio . get ( ) ;
@@ -148,25 +150,22 @@ function resolveNumericSource(source: number): ImageSource {
148150
149151 const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
150152 const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
153+ const { height, width } = asset ;
151154
152- return {
153- uri,
154- width : asset . width ,
155- height : asset . height
156- } ;
155+ return { uri, height, width } ;
157156}
158157
159158function resolveStringSource ( source : string ) : ImageSource {
160159 return { uri : source } ;
161160}
162161
163- function resolveObjectSource ( source : Source ) : ImageSource {
164- return source ;
162+ function resolveObjectSource ( source : Object ) : ImageSource {
163+ return ( source : ImageSource ) ;
165164}
166165
167166function resolveSvgDataUriSource (
168- source : Source ,
169- match : RegExpMatchArray
167+ source : Object ,
168+ match : Array < string >
170169) : ImageSource {
171170 const [ , prefix , svg ] = match ;
172171 // inline SVG markup may contain characters (e.g., #, ") that need to be escaped
@@ -189,10 +188,10 @@ function resolveBlobUri(source: ImageSource): ImageSource {
189188function getSourceToDisplay ( main , fallback ) {
190189 if ( main . status === LOADED ) return main . source ;
191190
192- if ( main . satus === LOADING && ! fallback . source . uri ) {
191+ if ( main . status === LOADING && ! fallback . source . uri ) {
193192 // Most of the time it's safe to use the main URI as img.src before loading
194193 // But it should not be used when the image would be loaded using `fetch` with headers
195- if ( ! main . headers ) return main . source ;
194+ if ( ! main . source . headers ) return main . source ;
196195 }
197196
198197 return fallback . source ;
@@ -348,27 +347,25 @@ ImageWithStatics.queryCache = function (uris) {
348347 return ImageLoader . queryCache ( uris ) ;
349348} ;
350349
351- // Todo: see if we can just use `result` as `source` (width|height might cause problems)
352-
353350/**
354351 * Image loading/state management hook
355352 * @param callbacks
356353 * @param source
357- * @returns {{state : string, uri: string } }
354+ * @returns {{status : string, source: ImageSource } }
358355 */
359356const useSource = (
360357 callbacks : ImageLoadingProps ,
361358 source : ?Source
362- ) : { status : IDLE | LOADING | LOADED | ERRORED , source : ImageSource } = > {
363- const [ resolvedSource , setResolvedSource ] = React . useState ( ( ) =>
359+ ) : { status : string , source : ImageSource } => {
360+ const [ resolvedSource , setResolvedSource ] = React . useState < ImageSource > ( ( ) =>
364361 resolveSource ( source )
365362 ) ;
366363
367364 const [ status , setStatus ] = React . useState ( ( ) =>
368365 ImageLoader . has ( resolveSource . uri ) ? LOADED : IDLE
369366 ) ;
370367
371- const [ result , setResult ] = React . useState ( resolvedSource ) ;
368+ const [ result , setResult ] = React . useState < ImageSource > ( resolvedSource ) ;
372369
373370 // Trigger a resolved source change when necessary
374371 React . useEffect ( ( ) => {
@@ -401,7 +398,7 @@ const useSource = (
401398 if ( onLoadEnd ) onLoadEnd ( ) ;
402399
403400 setStatus ( LOADED ) ;
404- setResult ( result . source ) ;
401+ setResult ( { ... resolvedSource , ... result . source } ) ;
405402 }
406403
407404 function handleError ( ) {
0 commit comments