Skip to content

Commit e1e28a7

Browse files
committed
Add peterprvy's update optimisations - #21
1 parent 2803a18 commit e1e28a7

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

code-input.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ var codeInput = {
6666
}
6767
}
6868

69-
/* Syntax-highlighting functions */
70-
update(text) {
71-
if(this.value != text) this.value = text; // Change value attribute if necessary.
72-
if(this.querySelector("textarea").value != text) this.querySelector("textarea").value = text;
69+
/* Syntax-highlighting functions */
70+
update(text) {
71+
console.log("Update", text);
72+
73+
// Prevent this from running multiple times on the same input when "value" attribute is changed,
74+
// by saving the value currently updated as _inUpdateNewText. Thank you to peterprvy for this.
75+
if(this.value != text) {
76+
this._inUpdateNewText = text;
77+
this.value = text; // Change value attribute if necessary.
78+
this._inUpdateNewText = undefined;
79+
}
80+
if(this.querySelector("textarea").value != text) this.querySelector("textarea").value = text;
7381

7482

7583
let result_element = this.querySelector("pre code");
@@ -199,8 +207,10 @@ var codeInput = {
199207

200208
case "value":
201209

202-
// Update code
203-
this.update(newValue);
210+
// Update code, only if this text has not already been updated.
211+
if(this._inUpdateNewText === undefined || newValue !== this._inUpdateNewText) {
212+
this.update(newValue);
213+
}
204214

205215
break;
206216

0 commit comments

Comments
 (0)