Fixed race condition which could occur while requesting the file size for the file attachment previews

Этот коммит содержится в:
hmhealey
2015-07-23 18:56:35 -04:00
родитель 0f0a887205
Коммит 92c9ec71a0
2 изменённых файлов: 29 добавлений и 11 удалений

Просмотреть файл

@@ -11,6 +11,9 @@ module.exports = React.createClass({
modalId: React.PropTypes.string.isRequired, modalId: React.PropTypes.string.isRequired,
handleImageClick: React.PropTypes.func handleImageClick: React.PropTypes.func
}, },
getInitialState: function() {
return {fileSize: 0};
},
componentDidMount: function() { componentDidMount: function() {
var filename = this.props.filenames[this.props.index]; var filename = this.props.filenames[this.props.index];
@@ -47,6 +50,22 @@ module.exports = React.createClass({
} }
} }
}, },
shouldComponentUpdate: function(nextProps, nextState) {
// the only time this object should update is when it receives an updated file size which we can usually handle without re-rendering
if (nextState.fileSize != this.state.fileSize) {
if (this.refs.fileSize) {
// update the UI element to display the file size without re-rendering the whole component
this.refs.fileSize.getDOMNode().innerHTML = utils.fileSizeToString(nextState.fileSize);
return false;
} else {
// we can't find the element that should hold the file size so we must not have rendered yet
return true;
}
} else {
return true;
}
},
render: function() { render: function() {
var filenames = this.props.filenames; var filenames = this.props.filenames;
var filename = filenames[this.props.index]; var filename = filenames[this.props.index];
@@ -78,15 +97,14 @@ module.exports = React.createClass({
); );
} }
// TODO fix the race condition here where the file size may arrive before the rest of the page is rendered if (!this.state.fileSize) {
// asynchronously request the size of the file so that we can display it next to the thumbnail var self = this;
utils.getFileSize(fileInfo.path + "." + fileInfo.ext, function(self, _filename) {
return function(size) { // asynchronously request the size of the file so that we can display it next to the thumbnail
if ((_filename + "__size") in self.refs) { utils.getFileSize(fileInfo.path + "." + fileInfo.ext, function(fileSize) {
self.refs[_filename + "__size"].getDOMNode().innerHTML = " " + utils.fileSizeToString(size); self.setState({fileSize: fileSize});
} });
} }
}(this, filename));
return ( return (
<div className="post-image__column" key={filename}> <div className="post-image__column" key={filename}>
@@ -95,7 +113,7 @@ module.exports = React.createClass({
<div className="post-image__name">{fileInfo.name}</div> <div className="post-image__name">{fileInfo.name}</div>
<div> <div>
<span className="post-image__type">{fileInfo.ext.toUpperCase()}</span> <span className="post-image__type">{fileInfo.ext.toUpperCase()}</span>
<span className="post-image__size" ref={filename + "__size"}></span> <span className="post-image__size">{this.state.fileSize ? utils.fileSizeToString(this.state.fileSize) : ""}</span>
</div> </div>
</div> </div>
</div> </div>

Просмотреть файл

@@ -152,7 +152,7 @@
color: grey; color: grey;
} }
.post-image__size { .post-image__size {
border-left: 2px; margin-left: 4px;
color: grey; color: grey;
} }
} }