property_change_notifier
property_change_notifier copied to clipboard
notifierListeners that takes an array of properties for Batching?
One of the great things about C# is that it has Batch processing where you stop notifications, do some work and then notify for the entire batch.
I'm trying to create an abstract class using this library that enables this that all view models can inherit from and has beginBatch, endBatch.
What I'd like to be able to do is pass all of the properties that changed within the batch as an array on notifyListeners instead of one field at a time. Then any listener that had multiple fields would get a single notification instead of one.
Are you interested in this? I can do a pull request to add this if you're interested.
It would look something like this I think:
/// Notifies the appropriate listeners that [properties] was changed.
/// Implementers should ideally provide a [properties] parameter.
/// Global listeners will be notified every time, even if a property is null.
/// Listeners for specific properties will only be notified
/// if [properties] is equal (operator==) to one of those properties.
/// If [properties] is not null, must be an instance of Iterable<[T]> (typically a [String]).
@protected
@visibleForTesting
void notifyListenersBatch(Iterable<T> properties) {
assert(_debugAssertNotDisposed());
// All global listeners should be notified.
final List<Function> listenersToNotify =
List<Function>.from(_globalListeners!);
// All matching property listeners should be notified as well.
listenersToNotify.addAll(_propertyListeners!.entries
.where((e) => properties.contains(e.key))
.map((e) => e.value)
.expand((e) => e));
for (final listener in listenersToNotify) {
if (listener is PropertyCallback<Iterable<T>>) {
listener(properties);
} else {
listener();
}
}
}