Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Unified Diff: pkg/observe/lib/src/observable_list.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: comment tweaks Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/observe/lib/src/observable_box.dart ('k') | pkg/observe/lib/src/observable_map.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/observe/lib/src/observable_list.dart
diff --git a/pkg/observe/lib/src/observable_list.dart b/pkg/observe/lib/src/observable_list.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5fa4972cb0a5be3f6e0395fafc456ceefe4b0743
--- /dev/null
+++ b/pkg/observe/lib/src/observable_list.dart
@@ -0,0 +1,196 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of observe;
+
+/**
+ * Represents an observable list of model values. If any items are added,
+ * removed, or replaced, then observers that are listening to [changes]
+ * will be notified.
+ */
+// TODO(jmesserly): remove implements List<E> once we can extend ListBase<E>
+class ObservableList<E> extends _ListBaseWorkaround with ObservableMixin
+ implements List<E> {
+
+ static const _LENGTH = const Symbol('length');
+
+ /** The inner [List<E>] with the actual storage. */
+ final List<E> _list;
+
+ /**
+ * Creates an observable list of the given [length].
+ *
+ * If no [length] argument is supplied an extendable list of
+ * length 0 is created.
+ *
+ * If a [length] argument is supplied, a fixed size list of that
+ * length is created.
+ */
+ ObservableList([int length])
+ : _list = length != null ? new List<E>(length) : <E>[];
+
+ /**
+ * Creates an observable list with the elements of [other]. The order in
+ * the list will be the order provided by the iterator of [other].
+ */
+ factory ObservableList.from(Iterable<E> other) =>
+ new ObservableList<E>()..addAll(other);
+
+ // TODO(jmesserly): remove once we have mirrors
+ getValueWorkaround(key) => identical(key, _LENGTH) ? length : null;
+
+ setValueWorkaround(key, value) {
+ if (identical(key, _LENGTH)) length = value;
+ }
+
+ int get length => _list.length;
+
+ set length(int value) {
+ int len = _list.length;
+ if (len == value) return;
+
+ // Produce notifications if needed
+ if (hasObservers) {
+ if (value < len) {
+ // Remove items, then adjust length. Note the reverse order.
+ notifyChange(new ListChangeRecord(value, removedCount: len - value));
+ notifyField(_LENGTH, len, value);
+ } else {
+ // Adjust length then add items
+ notifyField(_LENGTH, len, value);
+ notifyChange(new ListChangeRecord(len, addedCount: value - len));
+ }
+ }
+
+ _list.length = value;
+ }
+
+ E operator [](int index) => _list[index];
+
+ void operator []=(int index, E value) {
+ var oldValue = _list[index];
+ if (hasObservers) {
+ notifyChange(new ListChangeRecord(index, addedCount: 1, removedCount: 1));
+ }
+ _list[index] = value;
+ }
+
+ // The following methods are here so that we can provide nice change events.
+
+ void setAll(int index, Iterable<E> iterable) {
+ if (iterable is! List && iterable is! Set) {
+ iterable = iterable.toList();
+ }
+ var len = iterable.length;
+ _list.setAll(index, iterable);
+ if (hasObservers) {
+ notifyChange(
+ new ListChangeRecord(index, addedCount: len, removedCount: len));
+ }
+ }
+
+ void add(E value) {
+ int len = _list.length;
+ if (hasObservers) {
+ notifyField(_LENGTH, len, len + 1);
+ notifyChange(new ListChangeRecord(len, addedCount: 1));
+ }
+
+ _list.add(value);
+ }
+
+ void addAll(Iterable<E> iterable) {
+ int len = _list.length;
+ _list.addAll(iterable);
+ noitifyChange(new ListChangeRecord(len, addedCount: _list.length - len));
+ notifyField(_LENGTH, len, _list.length);
+ }
+
+ bool remove(Object element) {
+ for (int i = 0; i < this.length; i++) {
+ if (this[i] == element) {
+ removeRange(i, 1);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ void removeRange(int start, int end) {
+ _rangeCheck(start, end);
+ int length = end - start;
+ _list.setRange(start, this.length - length, this, end);
+
+ int len = _list.length;
+ _list.length -= length;
+ if (hasObservers) {
+ notifyChange(new ListChangeRecord(len, removedCount: length));
+ notifyField(_LENGTH, len, _list.length);
+ }
+ }
+
+ void insertAll(int index, Iterable<E> iterable) {
floitsch 2013/05/07 14:46:48 Why is this not just: var oldLength = _list.length
+ if (index < 0 || index > length) {
+ throw new RangeError.range(index, 0, length);
+ }
+ // TODO(floitsch): we can probably detect more cases.
+ if (iterable is! List && iterable is! Set) {
+ iterable = iterable.toList();
+ }
+ int insertionLength = iterable.length;
+ // There might be errors after the length change, in which case the list
+ // will end up being modified but the operation not complete. Unless we
+ // always go through a "toList" we can't really avoid that.
+ int len = _list.length;
+ _list.length += insertionLength;
+
+ _list.setRange(index + insertionLength, this.length, this, index);
+
+ if (hasObservers) {
+ notifyField(_LENGTH, len, _list.length);
+ notifyChange(new ListChangeRecord(index, addedCount: insertionLength);
+ } else {
+ setAll(index, iterable);
+ }
+ }
+
+ void insert(int index, E element) {
+ if (index < 0 || index > length) {
+ throw new RangeError.range(index, 0, length);
+ }
+ if (index == this.length) {
+ add(element);
+ return;
+ }
+ // We are modifying the length just below the is-check. Without the check
+ // Array.copy could throw an exception, leaving the list in a bad state
+ // (with a length that has been increased, but without a new element).
+ if (index is! int) throw new ArgumentError(index);
+ this.length++;
+ _list.setRange(index + 1, this.length, this, index);
+ notifyChange(new ListChangeRecord(index, addedCount: 1);
+ _list[index] = element;
+ }
+
+
+ E removeAt(int index) {
+ E result = this[index];
+ removeRange(index, index + 1);
+ return result;
+ }
+
+ void _rangeCheck(int start, int end) {
+ if (start < 0 || start > this.length) {
+ throw new RangeError.range(start, 0, this.length);
+ }
+ if (end < start || end > this.length) {
+ throw new RangeError.range(end, start, this.length);
+ }
+ }
+}
+
+
+// TODO(jmesserly): bogus type to workaround spurious VM bug with generic base
+// class and mixins.
+abstract class _ListBaseWorkaround extends ListBase<dynamic> {}
« no previous file with comments | « pkg/observe/lib/src/observable_box.dart ('k') | pkg/observe/lib/src/observable_map.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698