Skip to content

Commit 04a0a11

Browse files
committed
Debouncing the update of the diffs
1 parent 0fb0371 commit 04a0a11

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

diff-area.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* globals React, ReactDOM, Draft, diff_match_patch */
1+
/* globals React, ReactDOM, Draft, diff_match_patch, lodash */
22

33
var DMP = new diff_match_patch();
44

@@ -9,6 +9,11 @@ var DIFF = {
99
EQUAL: 0
1010
};
1111

12+
var DEBOUNCE_WAIT = 600; // ms
13+
var DEBOUNCE_OPTS = {
14+
trailing: true // We want to update after the delay only
15+
};
16+
1217
var DiffArea = React.createClass({
1318

1419
propTypes: {
@@ -17,6 +22,8 @@ var DiffArea = React.createClass({
1722
},
1823

1924
getInitialState: function () {
25+
// Make a debounced diff update
26+
this.debouncedUpdateDiffs = _.debounce(this.updateDiffs, DEBOUNCE_WAIT, DEBOUNCE_OPTS);
2027
var left = this.props.left;
2128
var right = this.props.right;
2229

@@ -37,32 +44,37 @@ var DiffArea = React.createClass({
3744
},
3845

3946
onChange: function (rightState) {
40-
var newState = {};
41-
4247
// Text changed ?
4348
var contentChanged = this.state.rightState.getCurrentContent()
4449
!== rightState.getCurrentContent();
4550

4651
// Update diffs
4752
if (contentChanged) {
48-
var left = this.props.left;
49-
var right = rightState.getCurrentContent().getPlainText();
50-
51-
var diffs = computeDiff(left, right);
52-
53-
// Update the decorators
54-
newState.leftState = Draft.EditorState.set(this.state.leftState, {
55-
decorator: createDiffsDecorator(diffs, DIFF.REMOVED)
56-
});
57-
newState.rightState = Draft.EditorState.set(rightState, {
58-
decorator: createDiffsDecorator(diffs, DIFF.INSERTED)
59-
});
60-
} else {
61-
// Just update the EditorState
62-
newState.leftState = this.state.leftState;
63-
newState.rightState = rightState;
53+
this.debouncedUpdateDiffs();
6454
}
6555

56+
// Update the EditorState
57+
this.setState({
58+
leftState: this.state.leftState,
59+
rightState: rightState
60+
});
61+
},
62+
63+
// We use a debounced version of it
64+
updateDiffs: function () {
65+
var newState = {};
66+
var left = this.props.left;
67+
var right = this.state.rightState.getCurrentContent().getPlainText();
68+
69+
var diffs = computeDiff(left, right);
70+
71+
// Update the decorators
72+
newState.leftState = Draft.EditorState.set(this.state.leftState, {
73+
decorator: createDiffsDecorator(diffs, DIFF.REMOVED)
74+
});
75+
newState.rightState = Draft.EditorState.set(this.state.rightState, {
76+
decorator: createDiffsDecorator(diffs, DIFF.INSERTED)
77+
});
6678
this.setState(newState);
6779
},
6880

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4+
<!-- lodash -->
5+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
46
<!-- Immutable -->
57
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
68
<!-- React -->

0 commit comments

Comments
 (0)