-
Notifications
You must be signed in to change notification settings - Fork 70
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
Merges in both debugging and ensuring fetch is cancelled #22
base: master
Are you sure you want to change the base?
Changes from all commits
2dac115
96dfc09
472f50e
0091cdf
82437bc
d38be1b
fe99c77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
yarn.lock | ||
node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,82 @@ | ||
// @flow | ||
|
||
import PropTypes from 'prop-types'; | ||
import React, { Component } from 'react'; | ||
import { View, WebView } from 'react-native'; | ||
import { View, WebView, StyleSheet } from 'react-native'; | ||
const fetchingDebug = require("debug")("react-native-remote-svg:SvgImage:fetch"); | ||
const fetchResultDebug = require("debug")("react-native-remote-svg:SvgImage:fetch:result"); | ||
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. Hey, it looks like you used the |
||
import Promise from 'bluebird'; | ||
Promise.config({ cancellation: true }); // Need to explicitly enable this feature | ||
|
||
const firstHtml = | ||
'<html><head><style>html, body { margin:0; padding:0; overflow:hidden; background-color: transparent; } svg { position:fixed; top:0; left:0; height:100%; width:100% }</style></head><body>'; | ||
const lastHtml = '</body></html>'; | ||
|
||
class SvgImage extends Component { | ||
state = { fetchingUrl: null, svgContent: null }; | ||
|
||
static propTypes = { | ||
source: PropTypes.shape({ | ||
uri: PropTypes.string.isRequired | ||
}).isRequired, | ||
containerStyle: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(StyleSheet).isRequired, | ||
PropTypes.object.isRequired, | ||
]), | ||
style: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(StyleSheet).isRequired, | ||
PropTypes.object.isRequired, | ||
]), | ||
} | ||
|
||
state = { | ||
svgContent: null, | ||
fetchingPromise: null, | ||
}; | ||
|
||
componentDidMount() { | ||
this.doFetch(this.props); | ||
this.doFetch(); | ||
} | ||
|
||
componentWillUnmount() { | ||
const { fetchingPromise } = this.state || {}; | ||
if(fetchingPromise) fetchingPromise.cancel(); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
this.doFetch(nextProps); | ||
|
||
componentDidUpdate(prevProps) { | ||
const { source:oldSource } = prevProps || {}; | ||
const { uri:oldUri } = oldSource || {}; | ||
const { source:newSource } = this.props || {}; | ||
const { uri:newUri } = newSource || {}; | ||
if(oldUri !== newUri) this.doFetch(); | ||
} | ||
doFetch = props => { | ||
let uri = props.source && props.source.uri; | ||
|
||
doFetch() { | ||
const { source } = this.props || {}; | ||
const { uri } = source || {}; | ||
if (uri) { | ||
if (uri.match(/^data:image\/svg/)) { | ||
const index = uri.indexOf('<svg'); | ||
this.setState({ fetchingUrl: uri, svgContent: uri.slice(index) }); | ||
this.setState({ svgContent: uri.slice(index) }); | ||
} else { | ||
console.log('fetching', uri); | ||
fetch(uri) | ||
.then(res => res.text()) | ||
.then(text => { | ||
this.setState({ fetchingUrl: uri, svgContent: text }); | ||
}) | ||
.catch(err => { | ||
console.error('got error', err); | ||
}); | ||
fetchingDebug('Fetching SVG from %s', uri); | ||
this.setState(({fetchingPromise:previousFetch}) => ({ fetchingPromise: | ||
Promise.resolve(fetch(uri)) | ||
.call("text") | ||
.then(text => this.setState({ svgContent: text })) | ||
.catch(e => console.error(`Error fetching SVG URI: ${e.message||e}`, {uri, e})) | ||
.return((previousFetch && previousFetch.isPending()) ? previousFetch : null) // Ensure we resolve/cancel previous fetch | ||
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. Shouldn't we cancel the previousFetch if the uri has changed? |
||
})) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
render() { | ||
const props = this.props; | ||
const { svgContent } = this.state; | ||
if (svgContent) { | ||
const props = this.props || {}; | ||
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. this.props should always be an object in react. Have you seen any situation where it might be falsy? It shouldn't be |
||
const { svgContent } = this.state || {}; | ||
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. We are initializing |
||
const hasSvgContent = Boolean(svgContent); | ||
return ( | ||
<View pointerEvents="none" style={[props.style, props.containerStyle]}> | ||
<WebView | ||
<View pointerEvents="none" style={[props.containerStyle, hasSvgContent ? {} : props.style]}> | ||
{ hasSvgContent && <WebView | ||
originWhitelist={['*']} | ||
scalesPageToFit={true} | ||
style={[ | ||
|
@@ -53,17 +89,10 @@ class SvgImage extends Component { | |
]} | ||
scrollEnabled={false} | ||
source={{ html: `${firstHtml}${svgContent}${lastHtml}` }} | ||
/> | ||
/> } | ||
</View> | ||
); | ||
} else { | ||
return ( | ||
<View | ||
pointerEvents="none" | ||
style={[props.containerStyle, props.style]} | ||
/> | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default SvgImage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,13 @@ | |
"react-native", | ||
"svg", | ||
"image" | ||
] | ||
], | ||
"dependencies": { | ||
"bluebird": "^3.5.1", | ||
"debug": "^3.1.0", | ||
"prop-types": "^15.6.2" | ||
}, | ||
"devDependencies": { | ||
"supports-color": "^5.4.0" | ||
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. This package doesn't seem to be used anywhere. Was this required for the use of |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is actually recommended for yarn.lock to be committed, since multiple people developing the project would all be on the same locked version.