Skip to content

Commit

Permalink
debounce rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokt33r committed Oct 26, 2016
1 parent 1245394 commit 0445c68
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions browser/components/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class MarkdownEditor extends React.Component {
super(props)

this.state = {
status: 'PREVIEW'
status: 'PREVIEW',
renderValue: props.value
}
}

Expand All @@ -21,6 +22,33 @@ class MarkdownEditor extends React.Component {
this.value = this.refs.code.value
}

componentWillReceiveProps (props) {
if (props.value !== this.props.value) {
this.queueRendering(props.value)
}
}

componentWillUnmount () {
this.cancelQueue()
}

queueRendering (value) {
clearTimeout(this.renderTimer)
this.renderTimer = setTimeout(() => {
this.renderPreview(value)
}, 500)
}

cancelQueue () {
clearTimeout(this.renderTimer)
}

renderPreview (value) {
this.setState({
renderValue: value
})
}

handleChange (e) {
this.value = this.refs.code.value
this.props.onChange(e)
Expand Down Expand Up @@ -110,6 +138,8 @@ class MarkdownEditor extends React.Component {

reload () {
this.refs.code.reload()
this.cancelQueue()
this.renderPreview(this.props.value)
}

render () {
Expand Down Expand Up @@ -158,7 +188,7 @@ class MarkdownEditor extends React.Component {
ref='preview'
onContextMenu={(e) => this.handleContextMenu(e)}
tabIndex='0'
value={value}
value={this.state.renderValue}
onMouseUp={(e) => this.handlePreviewMouseUp(e)}
onMouseDown={(e) => this.handlePreviewMouseDown(e)}
onCheckboxClick={(e) => this.handleCheckboxClick(e)}
Expand Down

0 comments on commit 0445c68

Please sign in to comment.