generalize implementation to skip input value update

when contents are "equivalent" in a given context, e.g. for numeric values with potentially different formatting in number fields
This commit is contained in:
Martin Raifer
2023-05-26 19:24:10 +02:00
parent ee89f6ae66
commit 2b64d70352
2 changed files with 32 additions and 28 deletions
+9 -5
View File
@@ -1,14 +1,14 @@
// Like selection.property('value', ...), but avoids no-op value sets,
// which can result in layout/repaint thrashing in some situations.
/** @returns {string} */
export function utilGetSetValue(selection, value) {
function setValue(value) {
export function utilGetSetValue(selection, value, shouldUpdate) {
function setValue(value, shouldUpdate) {
function valueNull() {
delete this.value;
}
function valueConstant() {
if (this.value !== value) {
if (shouldUpdate(this.value, value)) {
this.value = value;
}
}
@@ -17,7 +17,7 @@ export function utilGetSetValue(selection, value) {
var x = value.apply(this, arguments);
if (x === null || x === undefined) {
delete this.value;
} else if (this.value !== x) {
} else if (shouldUpdate(this.value, x)) {
this.value = x;
}
}
@@ -39,5 +39,9 @@ export function utilGetSetValue(selection, value) {
return selection.property('value');
}
return selection.each(stickyCursor(setValue(value)));
if (shouldUpdate === undefined) {
shouldUpdate = (a, b) => a !== b;
}
return selection.each(stickyCursor(setValue(value, shouldUpdate)));
}