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 ) {
@@ -135,6 +135,8 @@ function resolveSource(source: ?Source): Source {
135
135
// get the URI from the packager
136
136
function resolveNumericSource ( source : number ) : ImageSource {
137
137
const asset = getAssetByID ( source ) ;
138
+ if ( ! asset ) return { uri : '' } ;
139
+
138
140
let scale = asset . scales [ 0 ] ;
139
141
if ( asset . scales . length > 1 ) {
140
142
const preferredScale = PixelRatio . get ( ) ;
@@ -148,25 +150,22 @@ function resolveNumericSource(source: number): ImageSource {
148
150
149
151
const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
150
152
const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
153
+ const { height, width } = asset ;
151
154
152
- return {
153
- uri,
154
- width : asset . width ,
155
- height : asset . height
156
- } ;
155
+ return { uri, height, width } ;
157
156
}
158
157
159
158
function resolveStringSource ( source : string ) : ImageSource {
160
159
return { uri : source } ;
161
160
}
162
161
163
- function resolveObjectSource ( source : Source ) : ImageSource {
164
- return source ;
162
+ function resolveObjectSource ( source : Object ) : ImageSource {
163
+ return ( source : ImageSource ) ;
165
164
}
166
165
167
166
function resolveSvgDataUriSource (
168
- source : Source ,
169
- match : RegExpMatchArray
167
+ source : Object ,
168
+ match : Array < string >
170
169
) : ImageSource {
171
170
const [ , prefix , svg ] = match ;
172
171
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
@@ -189,10 +188,10 @@ function resolveBlobUri(source: ImageSource): ImageSource {
189
188
function getSourceToDisplay ( main , fallback ) {
190
189
if ( main . status === LOADED ) return main . source ;
191
190
192
- if ( main . satus === LOADING && ! fallback . source . uri ) {
191
+ if ( main . status === LOADING && ! fallback . source . uri ) {
193
192
// Most of the time it's safe to use the main URI as img.src before loading
194
193
// 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 ;
196
195
}
197
196
198
197
return fallback . source ;
@@ -348,27 +347,25 @@ ImageWithStatics.queryCache = function (uris) {
348
347
return ImageLoader . queryCache ( uris ) ;
349
348
} ;
350
349
351
- // Todo: see if we can just use `result` as `source` (width|height might cause problems)
352
-
353
350
/**
354
351
* Image loading/state management hook
355
352
* @param callbacks
356
353
* @param source
357
- * @returns {{state : string, uri: string } }
354
+ * @returns {{status : string, source: ImageSource } }
358
355
*/
359
356
const useSource = (
360
357
callbacks : ImageLoadingProps ,
361
358
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 > ( ( ) =>
364
361
resolveSource ( source )
365
362
) ;
366
363
367
364
const [ status , setStatus ] = React . useState ( ( ) =>
368
365
ImageLoader . has ( resolveSource . uri ) ? LOADED : IDLE
369
366
) ;
370
367
371
- const [ result , setResult ] = React . useState ( resolvedSource ) ;
368
+ const [ result , setResult ] = React . useState < ImageSource > ( resolvedSource ) ;
372
369
373
370
// Trigger a resolved source change when necessary
374
371
React . useEffect ( ( ) => {
@@ -401,7 +398,7 @@ const useSource = (
401
398
if ( onLoadEnd ) onLoadEnd ( ) ;
402
399
403
400
setStatus ( LOADED ) ;
404
- setResult ( result . source ) ;
401
+ setResult ( { ... resolvedSource , ... result . source } ) ;
405
402
}
406
403
407
404
function handleError ( ) {
0 commit comments