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

Side by Side Diff: pkg/polymer_expressions/lib/src/globals.dart

Issue 71353012: Make sure 'enumerate' changes when the underlying collection changes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/polymer_expressions/test/globals_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * Contains functions that are included by default in [PolymerExpressions]. 6 * Contains functions that are included by default in [PolymerExpressions].
7 * 7 *
8 * - [enumerate]: a convenient way to iterate over items and the indexes. 8 * - [enumerate]: a convenient way to iterate over items and the indexes.
9 */ 9 */
10 // Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99 c648b29b135fc0b880 10 // Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99 c648b29b135fc0b880
Jennifer Messerly 2013/11/13 23:51:45 this comment seems wrong now :)
11 library polymer_expressions.src.globals; 11 library polymer_expressions.src.globals;
12 12
13 import 'dart:collection'; 13 import 'package:logging/logging.dart';
14 import 'package:observe/observe.dart' show reflectable; 14 import 'package:observe/observe.dart';
15 15
16 /** 16 /**
17 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth 17 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth
18 * element of [iterable] and its index. 18 * element of [iterable] and its index.
19 */ 19 */
20 Iterable<IndexedValue> enumerate(Iterable iterable) => 20 Iterable<IndexedValue> enumerate(Iterable iterable) {
21 new EnumerateIterable(iterable); 21 var i = 0;
22 var result = iterable.map((e) => new IndexedValue(i++, e));
Jennifer Messerly 2013/11/13 23:51:45 I don't think this works. It looks awfully like a
Jennifer Messerly 2013/11/13 23:53:42 original context: https://github.com/google/quiver
23 if (iterable is! ObservableList) return result;
22 24
23 @reflectable class IndexedValue<V> { 25 result = new ObservableList.from(result);
24 final int index; 26 (iterable as ObservableList).listChanges.listen(
25 final V value; 27 (changes) => _propagateChange(iterable, result, changes));
28 return result;
29 }
30
31 class IndexedValue<V> {
Jennifer Messerly 2013/11/13 23:51:45 should mixin Observable or probably ChangeNotifier
32 @observable int index;
Jennifer Messerly 2013/11/13 23:51:45 make this immutable? so people don't accidentally
33 @reflectable final V value;
26 34
27 IndexedValue(this.index, this.value); 35 IndexedValue(this.index, this.value);
28 } 36 }
29 37
30 /** 38 final _logger = new Logger('polymer_expressions');
31 * An [Iterable] of [IndexedValue]s where the nth value holds the nth
32 * element of [iterable] and its index. See [enumerate].
33 */
34 // This was inspired by MappedIterable internal to Dart collections.
35 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> {
36 final Iterable<V> _iterable;
37 39
38 EnumerateIterable(this._iterable); 40 void _propagateChange(ObservableList original, ObservableList target,
41 List<ListChangeRecord> changes) {
42 // TODO(sigmund): maybe we can expose ObservableList._hasListObservers so we
Jennifer Messerly 2013/11/13 23:51:45 problem is, you'd need the pair of observed/unobse
43 // can skip this work when nobody is listening?
39 44
40 Iterator<IndexedValue<V>> get iterator => 45 for (var change in changes) {
41 new EnumerateIterator<V>(_iterable.iterator); 46 var index = change.index;
47 var added = change.addedCount;
48 var removed = change.removed.length;
49 if (index < 0 || index > target.length) {
50 _logger.error('enumerate filter seems to be in an inconsistent state. '
51 'Please report an error at dartbug.com/new with some information '
52 'to help us reproduce the problem.');
53 return;
54 }
55
56 // Apply the same changes on our view of the original list:
57 target.removeRange(index, index + removed);
58 for (int i = added - 1; i >= 0; i--) {
59 target.insert(index, new IndexedValue(index + i, original[index + i]));
60 }
42 61
43 // Length related functions are independent of the mapping. 62 // Update indices of the later [IndexedValue]s, if necessary.
44 int get length => _iterable.length; 63 if (added == removed) continue;
45 bool get isEmpty => _iterable.isEmpty;
46 64
47 // Index based lookup can be done before transforming. 65 for (int i = index + added; i < target.length; i++) {
48 IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first); 66 target[i].index = i;
49 IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last);
50 IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single);
51 IndexedValue<V> elementAt(int index) =>
52 new IndexedValue<V>(index, _iterable.elementAt(index));
53 }
54
55 /** The [Iterator] returned by [EnumerateIterable.iterator]. */
56 class EnumerateIterator<V> extends Iterator<IndexedValue<V>> {
57 final Iterator<V> _iterator;
58 int _index = 0;
59 IndexedValue<V> _current;
60
61 EnumerateIterator(this._iterator);
62
63 IndexedValue<V> get current => _current;
64
65 bool moveNext() {
66 if (_iterator.moveNext()) {
67 _current = new IndexedValue(_index++, _iterator.current);
68 return true;
69 } 67 }
70 _current = null;
71 return false;
72 } 68 }
73 } 69 }
OLDNEW
« no previous file with comments | « no previous file | pkg/polymer_expressions/test/globals_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698