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 const _VALUE = const Symbol('value'); | |
8 | |
9 // Inspired by ArrayReduction at: | 7 // Inspired by ArrayReduction at: |
10 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js | 8 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js |
11 // 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. |
12 | 10 |
13 /** | 11 /** |
14 * Observes a path starting from each item in the list. | 12 * Observes a path starting from each item in the list. |
15 */ | 13 */ |
16 class ListPathObserver<E, P> extends ChangeNotifierBase { | 14 class ListPathObserver<E, P> extends ChangeNotifierBase { |
17 final ObservableList<E> list; | 15 final ObservableList<E> list; |
18 final String _itemPath; | 16 final String _itemPath; |
(...skipping 23 matching lines...) Expand all Loading... |
42 | 40 |
43 void dispose() { | 41 void dispose() { |
44 if (_sub != null) _sub.cancel(); | 42 if (_sub != null) _sub.cancel(); |
45 _subs.forEach((s) => s.cancel()); | 43 _subs.forEach((s) => s.cancel()); |
46 _subs.clear(); | 44 _subs.clear(); |
47 } | 45 } |
48 | 46 |
49 void _reduce() { | 47 void _reduce() { |
50 _scheduled = false; | 48 _scheduled = false; |
51 _value = _observers.map((o) => o.value); | 49 _value = _observers.map((o) => o.value); |
52 notifyChange(new PropertyChangeRecord(_VALUE)); | 50 notifyChange(new PropertyChangeRecord(#value)); |
53 } | 51 } |
54 | 52 |
55 void _scheduleReduce(_) { | 53 void _scheduleReduce(_) { |
56 if (_scheduled) return; | 54 if (_scheduled) return; |
57 _scheduled = true; | 55 _scheduled = true; |
58 runAsync(_reduce); | 56 runAsync(_reduce); |
59 } | 57 } |
60 | 58 |
61 void _observeItems(int lengthAdjust) { | 59 void _observeItems(int lengthAdjust) { |
62 if (lengthAdjust > 0) { | 60 if (lengthAdjust > 0) { |
63 for (int i = 0; i < lengthAdjust; i++) { | 61 for (int i = 0; i < lengthAdjust; i++) { |
64 int len = _observers.length; | 62 int len = _observers.length; |
65 var pathObs = new PathObserver(list, '$len.$_itemPath'); | 63 var pathObs = new PathObserver(list, '$len.$_itemPath'); |
66 _subs.add(pathObs.changes.listen(_scheduleReduce)); | 64 _subs.add(pathObs.changes.listen(_scheduleReduce)); |
67 _observers.add(pathObs); | 65 _observers.add(pathObs); |
68 } | 66 } |
69 } else if (lengthAdjust < 0) { | 67 } else if (lengthAdjust < 0) { |
70 for (int i = 0; i < -lengthAdjust; i++) { | 68 for (int i = 0; i < -lengthAdjust; i++) { |
71 _subs.removeLast().cancel(); | 69 _subs.removeLast().cancel(); |
72 } | 70 } |
73 int len = _observers.length; | 71 int len = _observers.length; |
74 _observers.removeRange(len + lengthAdjust, len); | 72 _observers.removeRange(len + lengthAdjust, len); |
75 } | 73 } |
76 } | 74 } |
77 } | 75 } |
OLD | NEW |