forked from necolas/react-native-web
-
Notifications
You must be signed in to change notification settings - Fork 14
[add] Image source headers handling #11
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
Merged
Beamanator
merged 8 commits into
Expensify:master
from
kidroca:image-loader-headers-alt
Jan 27, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
059ab04
[add] Image source headers handling
kidroca 4e6daca
[add] Image: Append source to `nativeEvent`
kidroca 8f4d952
[change] Image code comments
kidroca 1e54c64
[add] ImageLoader.loadUsingHeaders
kidroca 93df02a
Cleanup
kidroca 7353183
Apply suggestions from code review
kidroca bb25c15
Image - clean up comments
kidroca 1f393c4
Apply suggestions from code review
kidroca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* @flow | ||
*/ | ||
|
||
import type { ImageSource, LoadRequest } from '../../modules/ImageLoader'; | ||
import type { ImageProps } from './types'; | ||
|
||
import * as React from 'react'; | ||
|
@@ -146,6 +147,23 @@ function resolveAssetUri(source): ?string { | |
return uri; | ||
} | ||
|
||
function raiseOnErrorEvent(uri, { onError, onLoadEnd }) { | ||
if (onError) { | ||
onError({ | ||
nativeEvent: { | ||
error: `Failed to load resource ${uri} (404)` | ||
} | ||
}); | ||
} | ||
if (onLoadEnd) onLoadEnd(); | ||
} | ||
|
||
function hasSourceDiff(a: ImageSource, b: ImageSource) { | ||
return ( | ||
a.uri !== b.uri || JSON.stringify(a.headers) !== JSON.stringify(b.headers) | ||
Beamanator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
|
||
interface ImageStatics { | ||
getSize: ( | ||
uri: string, | ||
|
@@ -158,10 +176,12 @@ interface ImageStatics { | |
) => Promise<{| [uri: string]: 'disk/memory' |}>; | ||
} | ||
|
||
const Image: React.AbstractComponent< | ||
type ImageComponent = React.AbstractComponent< | ||
ImageProps, | ||
React.ElementRef<typeof View> | ||
> = React.forwardRef((props, ref) => { | ||
>; | ||
|
||
const BaseImage: ImageComponent = React.forwardRef((props, ref) => { | ||
const { | ||
accessibilityLabel, | ||
blurRadius, | ||
|
@@ -279,16 +299,7 @@ const Image: React.AbstractComponent< | |
}, | ||
function error() { | ||
updateState(ERRORED); | ||
if (onError) { | ||
onError({ | ||
nativeEvent: { | ||
error: `Failed to load resource ${uri} (404)` | ||
} | ||
}); | ||
} | ||
if (onLoadEnd) { | ||
onLoadEnd(); | ||
} | ||
Beamanator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raiseOnErrorEvent(uri, { onError, onLoadEnd }); | ||
} | ||
); | ||
} | ||
|
@@ -332,14 +343,76 @@ const Image: React.AbstractComponent< | |
); | ||
}); | ||
|
||
Image.displayName = 'Image'; | ||
BaseImage.displayName = 'Image'; | ||
|
||
/** | ||
* This component handles specifically loading an image source with headers | ||
* default source is never loaded using headers | ||
*/ | ||
const ImageWithHeaders: ImageComponent = React.forwardRef((props, ref) => { | ||
// $FlowIgnore: This component would only be rendered when `source` matches `ImageSource` | ||
Beamanator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const nextSource: ImageSource = props.source; | ||
const [blobUri, setBlobUri] = React.useState(''); | ||
const request = React.useRef<LoadRequest>({ | ||
cancel: () => {}, | ||
source: { uri: '', headers: {} }, | ||
promise: Promise.resolve('') | ||
}); | ||
Comment on lines
+356
to
+360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const { onError, onLoadStart, onLoadEnd } = props; | ||
|
||
React.useEffect(() => { | ||
if (!hasSourceDiff(nextSource, request.current.source)) { | ||
return; | ||
} | ||
|
||
// When source changes we want to clean up any old/running requests | ||
request.current.cancel(); | ||
|
||
if (onLoadStart) { | ||
onLoadStart(); | ||
} | ||
|
||
// Store a ref for the current load request so we know what's the last loaded source, | ||
// and so we can cancel it if a different source is passed through props | ||
request.current = ImageLoader.loadWithHeaders(nextSource); | ||
Beamanator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
request.current.promise | ||
.then((uri) => setBlobUri(uri)) | ||
.catch(() => | ||
raiseOnErrorEvent(request.current.source.uri, { onError, onLoadEnd }) | ||
kidroca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
}, [nextSource, onLoadStart, onError, onLoadEnd]); | ||
|
||
// Cancel any request on unmount | ||
React.useEffect(() => request.current.cancel, []); | ||
|
||
const propsToPass = { | ||
...props, | ||
|
||
// `onLoadStart` is called from the current component | ||
// We skip passing it down to prevent BaseImage raising it a 2nd time | ||
kidroca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onLoadStart: undefined, | ||
|
||
// Until the current component resolves the request (using headers) | ||
kidroca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// we skip forwarding the source so the base component doesn't attempt | ||
// to load the original source | ||
source: blobUri ? { ...nextSource, uri: blobUri } : undefined | ||
}; | ||
|
||
return <BaseImage ref={ref} {...propsToPass} />; | ||
}); | ||
|
||
// $FlowIgnore: This is the correct type, but casting makes it unhappy since the variables aren't defined yet | ||
const ImageWithStatics = (Image: React.AbstractComponent< | ||
ImageProps, | ||
React.ElementRef<typeof View> | ||
> & | ||
ImageStatics); | ||
const ImageWithStatics: ImageComponent & ImageStatics = React.forwardRef( | ||
(props, ref) => { | ||
if (props.source && props.source.headers) { | ||
return <ImageWithHeaders ref={ref} {...props} />; | ||
} | ||
|
||
return <BaseImage ref={ref} {...props} />; | ||
} | ||
); | ||
|
||
ImageWithStatics.getSize = function (uri, success, failure) { | ||
ImageLoader.getSize(uri, success, failure); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.