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 ) {
@@ -153,25 +153,22 @@ function resolveNumericSource(source: number): ImageSource {
153153
154154 const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
155155 const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
156+ const { height, width } = asset ;
156157
157- return {
158- uri,
159- width : asset . width ,
160- height : asset . height
161- } ;
158+ return { uri, height, width } ;
162159}
163160
164161function resolveStringSource ( source : string ) : ImageSource {
165162 return { uri : source } ;
166163}
167164
168- function resolveObjectSource ( source : Source ) : ImageSource {
169- return source ;
165+ function resolveObjectSource ( source : Object ) : ImageSource {
166+ return ( source : ImageSource ) ;
170167}
171168
172169function resolveSvgDataUriSource (
173- source : Source ,
174- match : RegExpMatchArray
170+ source : Object ,
171+ match : Array < string >
175172) : ImageSource {
176173 const [ , prefix , svg ] = match ;
177174 // inline SVG markup may contain characters (e.g., #, ") that need to be escaped
@@ -194,10 +191,10 @@ function resolveBlobUri(source: ImageSource): ImageSource {
194191function getSourceToDisplay ( main , fallback ) {
195192 if ( main . status === LOADED ) return main . source ;
196193
197- if ( main . satus === LOADING && ! fallback . source . uri ) {
194+ if ( main . status === LOADING && ! fallback . source . uri ) {
198195 // Most of the time it's safe to use the main URI as img.src before loading
199196 // But it should not be used when the image would be loaded using `fetch` with headers
200- if ( ! main . headers ) return main . source ;
197+ if ( ! main . source . headers ) return main . source ;
201198 }
202199
203200 return fallback . source ;
@@ -353,27 +350,25 @@ ImageWithStatics.queryCache = function (uris) {
353350 return ImageLoader . queryCache ( uris ) ;
354351} ;
355352
356- // Todo: see if we can just use `result` as `source` (width|height might cause problems)
357-
358353/**
359354 * Image loading/state management hook
360355 * @param callbacks
361356 * @param source
362- * @returns {{state : string, uri: string } }
357+ * @returns {{status : string, source: ImageSource } }
363358 */
364359const useSource = (
365360 callbacks : ImageLoadingProps ,
366361 source : ?Source
367- ) : { status : IDLE | LOADING | LOADED | ERRORED , source : ImageSource } = > {
368- const [ resolvedSource , setResolvedSource ] = React . useState ( ( ) =>
362+ ) : { status : string , source : ImageSource } => {
363+ const [ resolvedSource , setResolvedSource ] = React . useState < ImageSource > ( ( ) =>
369364 resolveSource ( source )
370365 ) ;
371366
372367 const [ status , setStatus ] = React . useState ( ( ) =>
373368 ImageLoader . has ( resolveSource . uri ) ? LOADED : IDLE
374369 ) ;
375370
376- const [ result , setResult ] = React . useState ( resolvedSource ) ;
371+ const [ result , setResult ] = React . useState < ImageSource > ( resolvedSource ) ;
377372
378373 // Trigger a resolved source change when necessary
379374 React . useEffect ( ( ) => {
@@ -406,7 +401,7 @@ const useSource = (
406401 if ( onLoadEnd ) onLoadEnd ( ) ;
407402
408403 setStatus ( LOADED ) ;
409- setResult ( result . source ) ;
404+ setResult ( { ... resolvedSource , ... result . source } ) ;
410405 }
411406
412407 function handleError ( ) {
0 commit comments