Permalink
Browse files

feature: Optimized ModelObserver - avoids unnecessary updates

  • Loading branch information...
MikeMitterer committed Dec 15, 2016
1 parent 30dfc34 commit c7ece40381bea8749015dee82fb477b1a97907c4
Showing with 28 additions and 9 deletions.
  1. +1 −3 lib/src/components/MaterialTextfield.dart
  2. +27 −6 lib/src/directive/components/model/ModelObserver.dart
@@ -191,18 +191,16 @@ class MaterialTextfield extends MdlComponent with FallbackFormatter {
}
_kickInFormatter();
final bool isInvalid = element.classes.contains(_cssClasses.IS_INVALID);
// If field is invalid but empty we reset the invalid-class during this initialization
// phase.
//
// E.g. - solves the problem of showing an empty Login-Form empty fields marked as error.
// We turn this flag on after the user keys something in or field looses focus
// (Form becomes dirty)
final bool isInvalid = element.classes.contains(_cssClasses.IS_INVALID);
if (isInvalid && value.isEmpty) {
element.classes.remove(_cssClasses.IS_INVALID);
}
element.classes.add(_cssClasses.IS_UPGRADED);
}
}
@@ -60,11 +60,17 @@ class _TextFieldObserver implements ModelObserver<MaterialTextfield> {
_subscriptions.add(
prop.onChange.listen( (final PropertyChangeEvent event) => _textfield.value = prop.value.toString()));
_textfield.value = prop.value.toString();
// Changes the value in the _textfield only if prop.value is different
// Avoids unnecessary _textfield-updates
if(_textfield.value != prop.value.toString()) {
_textfield.value = prop.value.toString();
}
} else if(val != null) {
_textfield.value = val.toString();
if(_textfield.value != val.toString()) {
_textfield.value = val.toString();
}
_logger.warning("${fieldname} is not Observable, MaterialTextfield will not be able to set its value!");
} else {
@@ -105,7 +111,11 @@ class _CheckBoxObserver implements ModelObserver<MaterialCheckbox> {
prop.onChange.listen( (final PropertyChangeEvent event) =>
_checkbox.value == prop.value.toString() || prop.toBool() ? _checkbox.checked = true : _checkbox.checked = false));
_checkbox.checked = _checkbox.value == prop.value.toString() || prop.toBool();
// Check avoids unnecessary updates
final bool newState = _checkbox.value == prop.value.toString() || prop.toBool();
if(_checkbox.checked != newState) {
_checkbox.checked = newState;
}
} else if(val != null) {
@@ -150,7 +160,10 @@ class _RadioObserver implements ModelObserver<MaterialRadioGroup> {
_subscriptions.add(
prop.onChange.listen( (final PropertyChangeEvent event) => _radioGroup.value = prop.value.toString()));
_radioGroup.value = prop.value.toString();
// Avoids unnecessary updates
if(_radioGroup.value != prop.value.toString()) {
_radioGroup.value = prop.value.toString();
}
} else if(val != null) {
@@ -196,7 +209,11 @@ class _SwitchObserver implements ModelObserver<MaterialSwitch> {
prop.onChange.listen( (final PropertyChangeEvent event) =>
_switch.value == prop.value.toString() || prop.toBool() ? _switch.checked = true : _switch.checked = false));
_switch.checked = _switch.value.toString() == prop.value || prop.toBool();
// Avoids unnecessary updates
final bool newState = _switch.value.toString() == prop.value || prop.toBool();
if(_switch.checked != newState) {
_switch.checked = newState;
}
} else if(val != null) {
@@ -240,7 +257,11 @@ class _SliderObserver implements ModelObserver<MaterialSlider> {
_subscriptions.add(
prop.onChange.listen( (final PropertyChangeEvent event) => _slider.value = ModelObserver.toInt(prop.value)));
_slider.value = ModelObserver.toInt(prop.value);
// Avoids unnecessary updates
final int newValue = ModelObserver.toInt(prop.value);
if(_slider.value != newValue) {
_slider.value = newValue;
}
} else if(val != null) {

0 comments on commit c7ece40

Please sign in to comment.