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 /** | 7 /** |
8 * Represents an observable list of model values. If any items are added, | 8 * Represents an observable list of model values. If any items are added, |
9 * removed, or replaced, then observers that are listening to [changes] | 9 * removed, or replaced, then observers that are listening to [changes] |
10 * will be notified. | 10 * will be notified. |
(...skipping 16 matching lines...) Expand all Loading... |
27 ObservableList([int length]) | 27 ObservableList([int length]) |
28 : _list = length != null ? new List<E>(length) : <E>[]; | 28 : _list = length != null ? new List<E>(length) : <E>[]; |
29 | 29 |
30 /** | 30 /** |
31 * Creates an observable list with the elements of [other]. The order in | 31 * Creates an observable list with the elements of [other]. The order in |
32 * the list will be the order provided by the iterator of [other]. | 32 * the list will be the order provided by the iterator of [other]. |
33 */ | 33 */ |
34 factory ObservableList.from(Iterable<E> other) => | 34 factory ObservableList.from(Iterable<E> other) => |
35 new ObservableList<E>()..addAll(other); | 35 new ObservableList<E>()..addAll(other); |
36 | 36 |
37 int get length => _list.length; | 37 @reflectable int get length => _list.length; |
38 | 38 |
39 set length(int value) { | 39 @reflectable set length(int value) { |
40 int len = _list.length; | 40 int len = _list.length; |
41 if (len == value) return; | 41 if (len == value) return; |
42 | 42 |
43 // Produce notifications if needed | 43 // Produce notifications if needed |
44 if (hasObservers) { | 44 if (hasObservers) { |
45 if (value < len) { | 45 if (value < len) { |
46 // Remove items, then adjust length. Note the reverse order. | 46 // Remove items, then adjust length. Note the reverse order. |
47 _recordChange(new ListChangeRecord(value, removedCount: len - value)); | 47 _recordChange(new ListChangeRecord(value, removedCount: len - value)); |
48 } else { | 48 } else { |
49 // Adjust length then add items | 49 // Adjust length then add items |
50 _recordChange(new ListChangeRecord(len, addedCount: value - len)); | 50 _recordChange(new ListChangeRecord(len, addedCount: value - len)); |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 _list.length = value; | 54 _list.length = value; |
55 } | 55 } |
56 | 56 |
57 E operator [](int index) => _list[index]; | 57 @reflectable E operator [](int index) => _list[index]; |
58 | 58 |
59 void operator []=(int index, E value) { | 59 @reflectable void operator []=(int index, E value) { |
60 var oldValue = _list[index]; | 60 var oldValue = _list[index]; |
61 if (hasObservers) { | 61 if (hasObservers) { |
62 _recordChange(new ListChangeRecord(index, addedCount: 1, | 62 _recordChange(new ListChangeRecord(index, addedCount: 1, |
63 removedCount: 1)); | 63 removedCount: 1)); |
64 } | 64 } |
65 _list[index] = value; | 65 _list[index] = value; |
66 } | 66 } |
67 | 67 |
68 // The following methods are here so that we can provide nice change events. | 68 // The following methods are here so that we can provide nice change events. |
69 | 69 |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 271 |
272 if (added > 0 || removed > 0) { | 272 if (added > 0 || removed > 0) { |
273 notifyChange(new ListChangeRecord(startIndex, addedCount: added, | 273 notifyChange(new ListChangeRecord(startIndex, addedCount: added, |
274 removedCount: removed)); | 274 removedCount: removed)); |
275 } | 275 } |
276 | 276 |
277 offset += removed - added; | 277 offset += removed - added; |
278 } | 278 } |
279 } | 279 } |
280 } | 280 } |
OLD | NEW |