X Tutup
Skip to content

Commit d24df79

Browse files
committed
Revert "feat(iterable_differ): support immutable lists"
In Dart, ImmutableLists are just a projection of an underlying list. I.e. if the underlying list changes, the ImmutableList also changes. So we can't make optimizations based on checking whether a collection is an ImmutableList. This reverts commit a10c02c. Closes angular#8023
1 parent 01e6b8c commit d24df79

File tree

4 files changed

+20
-64
lines changed

4 files changed

+20
-64
lines changed

modules/angular2/src/core/change_detection/differs/default_iterable_differ.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export class DefaultIterableDiffer implements IterableDiffer {
109109

110110
onDestroy() {}
111111

112+
// todo(vicb): optim for UnmodifiableListView (frozen arrays)
112113
check(collection: any): boolean {
113114
this._reset();
114115

@@ -118,27 +119,24 @@ export class DefaultIterableDiffer implements IterableDiffer {
118119
var item;
119120
var itemTrackBy;
120121
if (isArray(collection)) {
121-
if (collection !== this._collection || !ListWrapper.isImmutable(collection)) {
122-
var list = collection;
123-
this._length = collection.length;
124-
125-
for (index = 0; index < this._length; index++) {
126-
item = list[index];
127-
itemTrackBy = this._trackByFn(index, item);
128-
if (record === null || !looseIdentical(record.trackById, itemTrackBy)) {
129-
record = this._mismatch(record, item, itemTrackBy, index);
130-
mayBeDirty = true;
131-
} else {
132-
if (mayBeDirty) {
133-
// TODO(misko): can we limit this to duplicates only?
134-
record = this._verifyReinsertion(record, item, itemTrackBy, index);
135-
}
136-
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
137-
}
122+
var list = collection;
123+
this._length = collection.length;
138124

139-
record = record._next;
125+
for (index = 0; index < this._length; index++) {
126+
item = list[index];
127+
itemTrackBy = this._trackByFn(index, item);
128+
if (record === null || !looseIdentical(record.trackById, itemTrackBy)) {
129+
record = this._mismatch(record, item, itemTrackBy, index);
130+
mayBeDirty = true;
131+
} else {
132+
if (mayBeDirty) {
133+
// TODO(misko): can we limit this to duplicates only?
134+
record = this._verifyReinsertion(record, item, itemTrackBy, index);
135+
}
136+
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
140137
}
141-
this._truncate(record);
138+
139+
record = record._next;
142140
}
143141
} else {
144142
index = 0;
@@ -158,9 +156,9 @@ export class DefaultIterableDiffer implements IterableDiffer {
158156
index++;
159157
});
160158
this._length = index;
161-
this._truncate(record);
162159
}
163160

161+
this._truncate(record);
164162
this._collection = collection;
165163
return this.isDirty;
166164
}

modules/angular2/src/facade/collection.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library facade.collection;
22

3-
import 'dart:collection' show IterableBase, UnmodifiableListView;
3+
import 'dart:collection' show IterableBase;
44
import 'dart:convert' show JsonEncoder;
55
export 'dart:core' show Iterator, Map, List, Set;
66
import 'dart:math' show max, min;
@@ -110,9 +110,6 @@ class ListWrapper {
110110
static List/*<T>*/ createFixedSize/*<T>*/(int size) => new List(size);
111111
static List/*<T>*/ createGrowableSize/*<T>*/(int size) =>
112112
new List.generate(size, (_) => null, growable: true);
113-
static UnmodifiableListView createImmutable(List input) {
114-
return new UnmodifiableListView(input);
115-
}
116113

117114
static bool contains(List m, k) => m.contains(k);
118115
static int indexOf(List list, value, [int startIndex = 0]) =>
@@ -229,10 +226,6 @@ class ListWrapper {
229226
return solution;
230227
}
231228

232-
static bool isImmutable(List l) {
233-
return l is UnmodifiableListView;
234-
}
235-
236229
static List flatten(List l) {
237230
final res = [];
238231
l.forEach((item) {

modules/angular2/src/facade/collection.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ export class ListWrapper {
184184
static createFixedSize(size: number): any[] { return new Array(size); }
185185
static createGrowableSize(size: number): any[] { return new Array(size); }
186186
static clone<T>(array: T[]): T[] { return array.slice(0); }
187-
static createImmutable<T>(array: T[]): T[] {
188-
var result = ListWrapper.clone(array);
189-
Object.seal(result);
190-
return result;
191-
}
192187
static forEachWithIndex<T>(array: T[], fn: (t: T, n: number) => void) {
193188
for (var i = 0; i < array.length; i++) {
194189
fn(array[i], i);
@@ -277,7 +272,6 @@ export class ListWrapper {
277272
return solution;
278273
}
279274

280-
static isImmutable(list: any[]): boolean { return Object.isSealed(list); }
281275
static flatten<T>(array: T[][]): T[] {
282276
let res = [];
283277
array.forEach((a) => res = res.concat(a));

modules/angular2/test/core/change_detection/differs/default_iterable_differ_spec.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ComplexItem {
3131
toString() { return `{id: ${this.id}, color: ${this.color}}` }
3232
}
3333

34+
// todo(vicb): UnmodifiableListView / frozen object when implemented
3435
export function main() {
3536
describe('iterable differ', function() {
3637
describe('DefaultIterableDiffer', function() {
@@ -313,36 +314,6 @@ export function main() {
313314
}));
314315
});
315316

316-
it('should not diff immutable collections if they are the same', () => {
317-
// Note: Use trackBy to know if diffing happened
318-
var trackByCount = 0;
319-
var trackBy = (index: number, item: any): any => {
320-
trackByCount++;
321-
return item;
322-
};
323-
var differ = new DefaultIterableDiffer(trackBy);
324-
var l1 = ListWrapper.createImmutable([1]);
325-
326-
differ.check(l1);
327-
expect(trackByCount).toBe(1);
328-
expect(differ.toString())
329-
.toEqual(
330-
iterableChangesAsString({collection: ['1[null->0]'], additions: ['1[null->0]']}));
331-
332-
333-
trackByCount = 0;
334-
differ.check(l1);
335-
expect(trackByCount).toBe(0);
336-
expect(differ.toString())
337-
.toEqual(iterableChangesAsString({collection: ['1'], previous: ['1']}));
338-
339-
trackByCount = 0;
340-
differ.check(l1);
341-
expect(trackByCount).toBe(0);
342-
expect(differ.toString())
343-
.toEqual(iterableChangesAsString({collection: ['1'], previous: ['1']}));
344-
});
345-
346317
describe('diff', () => {
347318
it('should return self when there is a change',
348319
() => { expect(differ.diff(['a', 'b'])).toBe(differ); });

0 commit comments

Comments
 (0)
X Tutup