Unverified Commit 7b5ec588 authored by Nate's avatar Nate Committed by GitHub

Allow `Listenable.merge()` to use any iterable (#143675)

This is a very small change that fixes #143664.
parent c82ca469
...@@ -62,11 +62,11 @@ abstract class Listenable { ...@@ -62,11 +62,11 @@ abstract class Listenable {
/// Return a [Listenable] that triggers when any of the given [Listenable]s /// Return a [Listenable] that triggers when any of the given [Listenable]s
/// themselves trigger. /// themselves trigger.
/// ///
/// The list must not be changed after this method has been called. Doing so /// Once the factory is called, items must not be added or removed from the iterable.
/// will lead to memory leaks or exceptions. /// Doing so will lead to memory leaks or exceptions.
/// ///
/// The list may contain nulls; they are ignored. /// The iterable may contain nulls; they are ignored.
factory Listenable.merge(List<Listenable?> listenables) = _MergingListenable; factory Listenable.merge(Iterable<Listenable?> listenables) = _MergingListenable;
/// Register a closure to be called when the object notifies its listeners. /// Register a closure to be called when the object notifies its listeners.
void addListener(VoidCallback listener); void addListener(VoidCallback listener);
...@@ -491,7 +491,7 @@ mixin class ChangeNotifier implements Listenable { ...@@ -491,7 +491,7 @@ mixin class ChangeNotifier implements Listenable {
class _MergingListenable extends Listenable { class _MergingListenable extends Listenable {
_MergingListenable(this._children); _MergingListenable(this._children);
final List<Listenable?> _children; final Iterable<Listenable?> _children;
@override @override
void addListener(VoidCallback listener) { void addListener(VoidCallback listener) {
......
...@@ -314,6 +314,21 @@ void main() { ...@@ -314,6 +314,21 @@ void main() {
log.clear(); log.clear();
}); });
test('Merging change notifiers supports any iterable', () {
final TestNotifier source1 = TestNotifier();
final TestNotifier source2 = TestNotifier();
final List<String> log = <String>[];
final Listenable merged = Listenable.merge(<Listenable?>{source1, source2});
void listener() => log.add('listener');
merged.addListener(listener);
source1.notify();
source2.notify();
expect(log, <String>['listener', 'listener']);
log.clear();
});
test('Merging change notifiers ignores null', () { test('Merging change notifiers ignores null', () {
final TestNotifier source1 = TestNotifier(); final TestNotifier source1 = TestNotifier();
final TestNotifier source2 = TestNotifier(); final TestNotifier source2 = TestNotifier();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment