Files
iD/modules/util/get_set_value.js
2016-08-21 15:19:27 -04:00

27 lines
826 B
JavaScript

// Like selection.property('value', ...), but avoids no-op value sets,
// which can result in layout/repaint thrashing in some situations.
export function getSetValue (target, value) {
function d3_selection_value(value) {
function valueNull() {
delete target.value;
}
function valueConstant() {
if (target.value !== value) target.value = value;
}
function valueFunction() {
var x = value.apply(target, arguments);
if (x == null) delete target.value;
else if (target.value !== x) target.value = x;
}
return value == null
? valueNull : (typeof value === "function"
? valueFunction : valueConstant);
}
if (!arguments.length) return target.property('value');
return target.each(d3_selection_value(value));
}