Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit cf2671a

Browse files
committed
fix(ng-repeat): don't use iterable.length
1 parent ae15983 commit cf2671a

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

lib/change_detection/change_detection.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ abstract class ChangedKeyValue<K, V> extends MapKeyValue<K, V> {
168168
abstract class CollectionChangeRecord<V> {
169169
/** The underlying iterable object */
170170
Iterable get iterable;
171+
int get length;
171172

172173
/** A list of [CollectionItem]s which are in the iteration order. */
173174
CollectionItem<V> get collectionHead;

lib/change_detection/dirty_checking_change_detector.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ class KeyValueRecord<K, V> implements KeyValue<K, V>, AddedKeyValue<K, V>,
795795

796796
class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
797797
Iterable _iterable;
798+
int _length;
799+
798800
/** Used to keep track of items during moves. */
799801
DuplicateMap _items = new DuplicateMap();
800802

@@ -836,6 +838,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
836838
}
837839

838840
Iterable get iterable => _iterable;
841+
int get length => _length;
839842

840843
bool _check(Iterable collection) {
841844
_reset();
@@ -849,6 +852,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
849852

850853
if (collection is List) {
851854
List list = collection;
855+
_length = list.length;
852856
for (int index = 0; index < list.length; index++) {
853857
var item = list[index];
854858
if (record == null || !identical(item, record.item)) {
@@ -873,6 +877,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
873877
record = record._nextRec;
874878
index++;
875879
}
880+
_length = index;
876881
}
877882

878883
_truncate(record);

lib/directive/ng_repeat.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class NgRepeatDirective {
142142

143143
// Computes and executes DOM changes when the item list changes
144144
void _onChange(CollectionChangeRecord changes) {
145-
final int length = changes.iterable.length;
145+
final int length = changes.length;
146146
final rows = new List<_Row>(length);
147147
final changeFunctions = new List<Function>(length);
148148
final removedIndexes = <int>[];

test/change_detection/dirty_checking_change_detector_spec.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,11 @@ class CollectionRecordMatcher extends Matcher {
701701

702702
bool checkCollection(CollectionChangeRecord changeRecord, List diffs) {
703703
var equals = true;
704+
int count = 0;
704705
if (collection != null) {
705706
CollectionItem collectionItem = changeRecord.collectionHead;
706707
for (var item in collection) {
708+
count++;
707709
if (collectionItem == null) {
708710
equals = false;
709711
diffs.add('collection too short: $item');
@@ -720,6 +722,11 @@ class CollectionRecordMatcher extends Matcher {
720722
equals = false;
721723
}
722724
}
725+
var iterableLength = changeRecord.iterable.toList().length;
726+
if (iterableLength != count) {
727+
diffs.add('collection length mismatched: $iterableLength != $count');
728+
equals = false;
729+
}
723730
return equals;
724731
}
725732

test/directive/ng_repeat_spec.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ main() {
4242
});
4343

4444

45+
it(r'should set create a list of items', (Scope scope, Compiler compiler, Injector injector) {
46+
scope.context['items'] = [];
47+
scope.watch('1', (_, __) {
48+
scope.context['items'].add('a');
49+
scope.context['items'].add('b');
50+
});
51+
var element = es('<div><div ng-repeat="item in items">{{item}}</div></div>');
52+
ViewFactory viewFactory = compiler(element, directives);
53+
View view = viewFactory(injector, element);
54+
scope.apply();
55+
expect(element).toHaveText('ab');
56+
});
57+
58+
4559
it(r'should set create a list of items from iterable',
4660
(Scope scope, Compiler compiler, Injector injector) {
4761
var element = es('<div><div ng-repeat="item in items">{{item}}</div></div>');

0 commit comments

Comments
 (0)