Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn.lock
Copy link
Owner

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.

node_modules
93 changes: 61 additions & 32 deletions SvgImage.js
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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, it looks like you used the debug package to debug this issue. Does it make sense to commit it onto master?

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
Copy link
Owner

Choose a reason for hiding this comment

The 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 || {};
Copy link
Owner

Choose a reason for hiding this comment

The 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 || {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are initializing state for this component. So this || {} wouldn't be necessary.

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={[
Expand All @@ -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;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The 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 debug package? Can this be removed?

}
}