Skip to content

Commit c562176

Browse files
committed
settings: Avoid redundant updates in setBool
1 parent 0adf54b commit c562176

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/model/settings.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,13 @@ class GlobalSettingsStore extends ChangeNotifier {
376376
/// A value of null means the setting will revert to following
377377
/// the app's default.
378378
///
379+
/// If [value] equals the setting's current value, the database operation
380+
/// and [notifyListeners] are skipped.
381+
///
379382
/// See also [getBool].
380383
Future<void> setBool(BoolGlobalSetting setting, bool? value) async {
384+
if (value == _boolData[setting]) return;
385+
381386
await _backend.doSetBoolGlobalSetting(setting, value);
382387
if (value == null) {
383388
_boolData.remove(setting);

test/model/settings_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,43 @@ void main() {
125125
.isFalse();
126126
assert(!BoolGlobalSetting.placeholderIgnore.default_);
127127
});
128+
129+
group('set avoids redundant updates', () {
130+
void checkUpdated(bool? old, bool? new_, {required bool expected}) async {
131+
final globalSettings = eg.globalStore(boolGlobalSettings: {
132+
if (old != null) BoolGlobalSetting.placeholderIgnore: old,
133+
}).settings;
134+
135+
bool updated = false;
136+
globalSettings.addListener(() => updated = true);
137+
138+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, new_);
139+
check(updated).equals(expected);
140+
}
141+
142+
test('null to null -> no update', () async {
143+
checkUpdated(null, null, expected: false);
144+
});
145+
146+
test('true to true -> no update', () async {
147+
checkUpdated(true, true, expected: false);
148+
});
149+
150+
test('false to false -> no update', () async {
151+
checkUpdated(false, false, expected: false);
152+
});
153+
154+
test('null to false -> does the update', () async {
155+
checkUpdated(null, false, expected: true);
156+
});
157+
158+
test('true to null -> does the update', () async {
159+
checkUpdated(true, null, expected: true);
160+
});
161+
162+
test('false to true -> does the update', () async {
163+
checkUpdated(false, true, expected: true);
164+
});
165+
});
128166
});
129167
}

0 commit comments

Comments
 (0)