8
8
* @flow
9
9
*/
10
10
11
- import type { ImageResult } from '../../modules/ImageLoader' ;
11
+ import type { ImageResult , ImageSource } from '../../modules/ImageLoader' ;
12
12
import type { ImageProps , Source , ImageLoadingProps } from './types' ;
13
13
14
14
import * as React from 'react' ;
@@ -100,7 +100,7 @@ function resolveAssetDimensions(source: ImageSource) {
100
100
return { height, width } ;
101
101
}
102
102
103
- function resolveSource ( source : ?Source ) : Source {
103
+ function resolveSource ( source : ?Source ) : ImageSource {
104
104
let resolvedSource = { uri : '' } ;
105
105
106
106
if ( typeof source === 'number' ) {
@@ -117,7 +117,7 @@ function resolveSource(source: ?Source): Source {
117
117
118
118
return resolveSource ( source [ 0 ] ) ;
119
119
} else if ( source && typeof source . uri === 'string' ) {
120
- resolvedSource = resolveObjectSource ( ( source : Source ) ) ;
120
+ resolvedSource = resolveObjectSource ( source ) ;
121
121
}
122
122
123
123
if ( resolvedSource . uri ) {
@@ -153,25 +153,22 @@ function resolveNumericSource(source: number): ImageSource {
153
153
154
154
const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
155
155
const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
156
+ const { height, width } = asset ;
156
157
157
- return {
158
- uri,
159
- width : asset . width ,
160
- height : asset . height
161
- } ;
158
+ return { uri, height, width } ;
162
159
}
163
160
164
161
function resolveStringSource ( source : string ) : ImageSource {
165
162
return { uri : source } ;
166
163
}
167
164
168
- function resolveObjectSource ( source : Source ) : ImageSource {
169
- return source ;
165
+ function resolveObjectSource ( source : Object ) : ImageSource {
166
+ return ( source : ImageSource ) ;
170
167
}
171
168
172
169
function resolveSvgDataUriSource (
173
- source : Source ,
174
- match : RegExpMatchArray
170
+ source : Object ,
171
+ match : Array < string >
175
172
) : ImageSource {
176
173
const [ , prefix , svg ] = match ;
177
174
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
@@ -194,10 +191,10 @@ function resolveBlobUri(source: ImageSource): ImageSource {
194
191
function getSourceToDisplay ( main , fallback ) {
195
192
if ( main . status === LOADED ) return main . source ;
196
193
197
- if ( main . satus === LOADING && ! fallback . source . uri ) {
194
+ if ( main . status === LOADING && ! fallback . source . uri ) {
198
195
// Most of the time it's safe to use the main URI as img.src before loading
199
196
// 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 ;
201
198
}
202
199
203
200
return fallback . source ;
@@ -353,27 +350,25 @@ ImageWithStatics.queryCache = function (uris) {
353
350
return ImageLoader . queryCache ( uris ) ;
354
351
} ;
355
352
356
- // Todo: see if we can just use `result` as `source` (width|height might cause problems)
357
-
358
353
/**
359
354
* Image loading/state management hook
360
355
* @param callbacks
361
356
* @param source
362
- * @returns {{state : string, uri: string } }
357
+ * @returns {{status : string, source: ImageSource } }
363
358
*/
364
359
const useSource = (
365
360
callbacks : ImageLoadingProps ,
366
361
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 > ( ( ) =>
369
364
resolveSource ( source )
370
365
) ;
371
366
372
367
const [ status , setStatus ] = React . useState ( ( ) =>
373
368
ImageLoader . has ( resolveSource . uri ) ? LOADED : IDLE
374
369
) ;
375
370
376
- const [ result , setResult ] = React . useState ( resolvedSource ) ;
371
+ const [ result , setResult ] = React . useState < ImageSource > ( resolvedSource ) ;
377
372
378
373
// Trigger a resolved source change when necessary
379
374
React . useEffect ( ( ) => {
@@ -406,7 +401,7 @@ const useSource = (
406
401
if ( onLoadEnd ) onLoadEnd ( ) ;
407
402
408
403
setStatus ( LOADED ) ;
409
- setResult ( result . source ) ;
404
+ setResult ( { ... resolvedSource , ... result . source } ) ;
410
405
}
411
406
412
407
function handleError ( ) {
0 commit comments