OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library web_ui.observe.list; |
| 6 |
| 7 import 'dart:collection'; |
| 8 import 'package:web_ui/observe.dart'; |
| 9 |
| 10 // TODO(jmesserly): this should extend the real list implementation. |
| 11 // See http://dartbug.com/2600. The workaround was to copy+paste lots of code |
| 12 // from the VM. |
| 13 /** |
| 14 * Represents an observable list of model values. If any items are added, |
| 15 * removed, or replaced, then observers that are registered with |
| 16 * [observe] will be notified. |
| 17 */ |
| 18 class ObservableList<E> extends Collection<E> implements List<E> { |
| 19 /** The inner [List<E>] with the actual storage. */ |
| 20 final List<E> _list; |
| 21 |
| 22 final List<Object> _observeIndex; |
| 23 Object _observeLength; |
| 24 |
| 25 /** |
| 26 * Creates an observable list of the given [length]. |
| 27 * |
| 28 * If no [length] argument is supplied an extendable list of |
| 29 * length 0 is created. |
| 30 * |
| 31 * If a [length] argument is supplied, a fixed size list of that |
| 32 * length is created. |
| 33 */ |
| 34 ObservableList([int length = 0]) |
| 35 : _list = new List<E>(length), |
| 36 _observeIndex = new List<Object>(length); |
| 37 |
| 38 /** |
| 39 * Creates an observable list with the elements of [other]. The order in |
| 40 * the list will be the order provided by the iterator of [other]. |
| 41 */ |
| 42 factory ObservableList.from(Iterable<E> other) => |
| 43 new ObservableList<E>()..addAll(other); |
| 44 |
| 45 Iterator<E> get iterator => new ListIterator<E>(this); |
| 46 |
| 47 int get length { |
| 48 if (observeReads) _observeLength = notifyRead(_observeLength); |
| 49 return _list.length; |
| 50 } |
| 51 |
| 52 set length(int value) { |
| 53 if (length == value) return; |
| 54 |
| 55 if (_observeLength != null) _observeLength = notifyWrite(_observeLength); |
| 56 |
| 57 // If we are shrinking the list, explicitly null out items so we track |
| 58 // the change to those items. |
| 59 for (int i = value; i < _list.length; i++) { |
| 60 this[i] = null; |
| 61 } |
| 62 _observeIndex.length = value; |
| 63 _list.length = value; |
| 64 } |
| 65 |
| 66 E operator [](int index) { |
| 67 if (observeReads) _observeIndex[index] = notifyRead(_observeIndex[index]); |
| 68 return _list[index]; |
| 69 } |
| 70 |
| 71 operator []=(int index, E value) { |
| 72 var observer = _observeIndex[index]; |
| 73 var oldValue = _list[index]; |
| 74 if (observer != null && oldValue != value) { |
| 75 _observeIndex[index] = notifyWrite(observer); |
| 76 } |
| 77 _list[index] = value; |
| 78 } |
| 79 |
| 80 void add(E value) { |
| 81 if (_observeLength != null) _observeLength = notifyWrite(_observeLength); |
| 82 _list.add(value); |
| 83 _observeIndex.add(null); |
| 84 } |
| 85 |
| 86 // --------------------------------------------------------------------------- |
| 87 // Note: below this comment, methods are either: |
| 88 // * redirect to Arrays |
| 89 // * redirect to Collections |
| 90 // * copy+paste from VM GrowableObjectArray. |
| 91 // The general idea is to have these methods operate in terms of our primitive |
| 92 // methods above, so they correctly track reads/writes. |
| 93 // --------------------------------------------------------------------------- |
| 94 |
| 95 bool remove(E item) { |
| 96 int i = indexOf(item); |
| 97 if (i == -1) return false; |
| 98 removeAt(i); |
| 99 return true; |
| 100 } |
| 101 |
| 102 // TODO(jmesserly): This should be on List, to match removeAt. |
| 103 // See http://code.google.com/p/dart/issues/detail?id=5375 |
| 104 void insertAt(int index, E item) => insertRange(index, 1, item); |
| 105 |
| 106 bool contains(E item) => Collections.contains(_list, item); |
| 107 |
| 108 E get first => this[0]; |
| 109 |
| 110 E removeLast() { |
| 111 var len = length - 1; |
| 112 var elem = this[len]; |
| 113 length = len; |
| 114 return elem; |
| 115 } |
| 116 |
| 117 int indexOf(E element, [int start = 0]) => |
| 118 Arrays.indexOf(this, element, start, length); |
| 119 |
| 120 int lastIndexOf(E element, [int start]) => |
| 121 Arrays.lastIndexOf(this, element, start); |
| 122 |
| 123 ObservableList<E> getRange(int start, int length) { |
| 124 if (length == 0) return []; |
| 125 Arrays.rangeCheck(this, start, length); |
| 126 List list = new ObservableList<E>(length); |
| 127 Arrays.copy(this, start, list, 0, length); |
| 128 return list; |
| 129 } |
| 130 |
| 131 bool get isEmpty => length == 0; |
| 132 |
| 133 E get last => this[length - 1]; |
| 134 |
| 135 void addLast(E value) => add(value); |
| 136 |
| 137 void addAll(Iterable<E> collection) { |
| 138 for (E elem in collection) { |
| 139 add(elem); |
| 140 } |
| 141 } |
| 142 |
| 143 void sort([compare = Comparable.compare]) => |
| 144 IterableMixinWorkaround.sortList(this, compare); |
| 145 |
| 146 List<E> get reversed => IterableMixinWorkaround.reversedList(this); |
| 147 |
| 148 void clear() { |
| 149 this.length = 0; |
| 150 } |
| 151 |
| 152 E removeAt(int index) { |
| 153 if (index is! int) throw new ArgumentError(index); |
| 154 E result = this[index]; |
| 155 int newLength = this.length - 1; |
| 156 Arrays.copy(this, |
| 157 index + 1, |
| 158 this, |
| 159 index, |
| 160 newLength - index); |
| 161 this.length = newLength; |
| 162 return result; |
| 163 } |
| 164 |
| 165 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { |
| 166 Arrays.copy(from, startFrom, this, start, length); |
| 167 } |
| 168 |
| 169 void removeRange(int start, int length) { |
| 170 if (length == 0) { |
| 171 return; |
| 172 } |
| 173 Arrays.rangeCheck(this, start, length); |
| 174 Arrays.copy(this, |
| 175 start + length, |
| 176 this, |
| 177 start, |
| 178 this.length - length - start); |
| 179 this.length = this.length - length; |
| 180 } |
| 181 |
| 182 void insertRange(int start, int length, [E initialValue]) { |
| 183 if (length == 0) { |
| 184 return; |
| 185 } |
| 186 if ((length < 0) || (length is! int)) { |
| 187 throw new ArgumentError("invalid length specified $length"); |
| 188 } |
| 189 if (start < 0 || start > this.length) { |
| 190 throw new RangeError.value(start); |
| 191 } |
| 192 var oldLength = this.length; |
| 193 this.length = oldLength + length; // Will expand if needed. |
| 194 Arrays.copy(this, |
| 195 start, |
| 196 this, |
| 197 start + length, |
| 198 oldLength - start); |
| 199 for (int i = start; i < start + length; i++) { |
| 200 this[i] = initialValue; |
| 201 } |
| 202 } |
| 203 |
| 204 String toString() => Collections.collectionToString(this); |
| 205 } |
| 206 |
| 207 // TODO(jmesserly): copy+paste from collection-dev |
| 208 /** |
| 209 * Iterates over a [List] in growing index order. |
| 210 */ |
| 211 class ListIterator<E> implements Iterator<E> { |
| 212 final List<E> _list; |
| 213 final int _length; |
| 214 int _position; |
| 215 E _current; |
| 216 |
| 217 ListIterator(List<E> list) |
| 218 : _list = list, _position = -1, _length = list.length; |
| 219 |
| 220 bool moveNext() { |
| 221 if (_list.length != _length) { |
| 222 throw new ConcurrentModificationError(_list); |
| 223 } |
| 224 int nextPosition = _position + 1; |
| 225 if (nextPosition < _length) { |
| 226 _position = nextPosition; |
| 227 _current = _list[nextPosition]; |
| 228 return true; |
| 229 } |
| 230 _current = null; |
| 231 return false; |
| 232 } |
| 233 |
| 234 E get current => _current; |
| 235 } |
OLD | NEW |