| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of observe; | 5 part of observe; |
| 6 | 6 |
| 7 // Inspired by ArrayReduction at: | 7 // Inspired by ArrayReduction at: |
| 8 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js | 8 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js |
| 9 // The main difference is we support anything on the rich Dart Iterable API. | 9 // The main difference is we support anything on the rich Dart Iterable API. |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Observes a path starting from each item in the list. | 12 * Observes a path starting from each item in the list. |
| 13 */ | 13 */ |
| 14 class ListPathObserver<E, P> extends ChangeNotifierBase { | 14 class ListPathObserver<E, P> extends ChangeNotifier { |
| 15 final ObservableList<E> list; | 15 final ObservableList<E> list; |
| 16 final String _itemPath; | 16 final String _itemPath; |
| 17 final List<PathObserver> _observers = <PathObserver>[]; | 17 final List<PathObserver> _observers = <PathObserver>[]; |
| 18 final List<StreamSubscription> _subs = <StreamSubscription>[]; | 18 final List<StreamSubscription> _subs = <StreamSubscription>[]; |
| 19 StreamSubscription _sub; | 19 StreamSubscription _sub; |
| 20 bool _scheduled = false; | 20 bool _scheduled = false; |
| 21 Iterable<P> _value; | 21 Iterable<P> _value; |
| 22 | 22 |
| 23 ListPathObserver(this.list, String path) | 23 ListPathObserver(this.list, String path) |
| 24 : _itemPath = path { | 24 : _itemPath = path { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 39 @reflectable Iterable<P> get value => _value; | 39 @reflectable Iterable<P> get value => _value; |
| 40 | 40 |
| 41 void dispose() { | 41 void dispose() { |
| 42 if (_sub != null) _sub.cancel(); | 42 if (_sub != null) _sub.cancel(); |
| 43 _subs.forEach((s) => s.cancel()); | 43 _subs.forEach((s) => s.cancel()); |
| 44 _subs.clear(); | 44 _subs.clear(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void _reduce() { | 47 void _reduce() { |
| 48 _scheduled = false; | 48 _scheduled = false; |
| 49 _value = _observers.map((o) => o.value); | 49 var newValue = _observers.map((o) => o.value); |
| 50 notifyChange(new PropertyChangeRecord(#value)); | 50 _value = notifyPropertyChange(#value, _value, newValue); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void _scheduleReduce(_) { | 53 void _scheduleReduce(_) { |
| 54 if (_scheduled) return; | 54 if (_scheduled) return; |
| 55 _scheduled = true; | 55 _scheduled = true; |
| 56 scheduleMicrotask(_reduce); | 56 scheduleMicrotask(_reduce); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void _observeItems(int lengthAdjust) { | 59 void _observeItems(int lengthAdjust) { |
| 60 if (lengthAdjust > 0) { | 60 if (lengthAdjust > 0) { |
| 61 for (int i = 0; i < lengthAdjust; i++) { | 61 for (int i = 0; i < lengthAdjust; i++) { |
| 62 int len = _observers.length; | 62 int len = _observers.length; |
| 63 var pathObs = new PathObserver(list, '$len.$_itemPath'); | 63 var pathObs = new PathObserver(list, '$len.$_itemPath'); |
| 64 _subs.add(pathObs.changes.listen(_scheduleReduce)); | 64 _subs.add(pathObs.changes.listen(_scheduleReduce)); |
| 65 _observers.add(pathObs); | 65 _observers.add(pathObs); |
| 66 } | 66 } |
| 67 } else if (lengthAdjust < 0) { | 67 } else if (lengthAdjust < 0) { |
| 68 for (int i = 0; i < -lengthAdjust; i++) { | 68 for (int i = 0; i < -lengthAdjust; i++) { |
| 69 _subs.removeLast().cancel(); | 69 _subs.removeLast().cancel(); |
| 70 } | 70 } |
| 71 int len = _observers.length; | 71 int len = _observers.length; |
| 72 _observers.removeRange(len + lengthAdjust, len); | 72 _observers.removeRange(len + lengthAdjust, len); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 } | 75 } |
| OLD | NEW |