Skip to content

Commit 45ae090

Browse files
committed
Revert "Revert "refactor(ng-repeat): refactor the fix for dart-archive#1015""
This reverts commit 4421710.
1 parent 4421710 commit 45ae090

File tree

1 file changed

+62
-65
lines changed

1 file changed

+62
-65
lines changed

lib/directive/ng_repeat.dart

+62-65
Original file line numberDiff line numberDiff line change
@@ -132,90 +132,87 @@ class NgRepeat {
132132
_watch = _scope.watch(
133133
_listExpr,
134134
(changes, _) {
135-
_onChange((changes is CollectionChangeRecord) ? changes : null);
135+
if (changes is CollectionChangeRecord && changes != null) {
136+
_onCollectionChange(changes);
137+
} else if (_rows != null) {
138+
_rows.forEach((row) {
139+
row.scope.destroy();
140+
_viewPort.remove(row.view);
141+
});
142+
_rows = null;
143+
}
136144
},
137145
collection: true,
138146
formatters: formatters
139147
);
140148
}
141149

142150
// Computes and executes DOM changes when the item list changes
143-
void _onChange(CollectionChangeRecord changes) {
144-
final iterable = (changes == null) ? const [] : changes.iterable;
145-
final int length = (changes == null) ? 0 : changes.length;
151+
void _onCollectionChange(CollectionChangeRecord changes) {
152+
final int length = changes.length;
146153
final rows = new List<_Row>(length);
147154
final changeFunctions = new List<Function>(length);
148155
final removedIndexes = <int>[];
149156
final int domLength = _rows == null ? 0 : _rows.length;
150157
final leftInDom = new List.generate(domLength, (i) => domLength - 1 - i);
151158
var domIndex;
152159

153-
var addRow = (int index, value, View previousView) {
154-
var childContext = _updateContext(new PrototypeMap(_scope.context), index,
155-
length)..[_valueIdentifier] = value;
156-
var childScope = _scope.createChild(childContext);
157-
var view = _boundViewFactory(childScope);
158-
var nodes = view.nodes;
159-
rows[index] = new _Row(_generateId(index, value, index))
160-
..view = view
161-
..scope = childScope
162-
..nodes = nodes
163-
..startNode = nodes.first
164-
..endNode = nodes.last;
165-
_viewPort.insert(view, insertAfter: previousView);
166-
};
160+
Function addFn, moveFn, removeFn;
167161

168-
// todo(vicb) refactor once GH-774 gets fixed
169162
if (_rows == null) {
170-
_rows = new List<_Row>(length);
171-
for (var i = 0; i < length; i++) {
172-
changeFunctions[i] = (index, previousView) {
173-
addRow(index, iterable.elementAt(i), previousView);
174-
};
175-
}
163+
addFn = changes.forEachItem;
164+
moveFn = (_) {};
165+
removeFn = (_) {};
176166
} else {
177-
if (changes == null) {
178-
_rows.forEach((row) {
179-
row.scope.destroy();
180-
_viewPort.remove(row.view);
181-
});
182-
leftInDom.clear();
183-
} else {
184-
changes.forEachRemoval((CollectionChangeItem removal) {
185-
var index = removal.previousIndex;
186-
var row = _rows[index];
187-
row.scope.destroy();
188-
_viewPort.remove(row.view);
189-
leftInDom.removeAt(domLength - 1 - index);
190-
});
167+
addFn = changes.forEachAddition;
168+
moveFn = changes.forEachMove;
169+
removeFn = changes.forEachRemoval;
170+
}
191171

192-
changes.forEachAddition((CollectionChangeItem addition) {
193-
changeFunctions[addition.currentIndex] = (index, previousView) {
194-
addRow(index, addition.item, previousView);
195-
};
196-
});
172+
removeFn((CollectionChangeItem removal) {
173+
var index = removal.previousIndex;
174+
var row = _rows[index];
175+
row.scope.destroy();
176+
_viewPort.remove(row.view);
177+
leftInDom.removeAt(domLength - 1 - index);
178+
});
197179

198-
changes.forEachMove((CollectionChangeItem move) {
199-
var previousIndex = move.previousIndex;
200-
var value = move.item;
201-
changeFunctions[move.currentIndex] = (index, previousView) {
202-
var previousRow = _rows[previousIndex];
203-
var childScope = previousRow.scope;
204-
var childContext = _updateContext(childScope.context, index, length);
205-
if (!identical(childScope.context[_valueIdentifier], value)) {
206-
childContext[_valueIdentifier] = value;
207-
}
208-
rows[index] = _rows[previousIndex];
209-
// Only move the DOM node when required
210-
if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
211-
_viewPort.move(previousRow.view, moveAfter: previousView);
212-
leftInDom.remove(previousIndex);
213-
}
214-
domIndex--;
215-
};
216-
});
217-
}
218-
}
180+
addFn((CollectionChangeItem addition) {
181+
changeFunctions[addition.currentIndex] = (index, previousView) {
182+
var childContext = _updateContext(new PrototypeMap(_scope.context), index,length)
183+
..[_valueIdentifier] = addition.item;
184+
var childScope = _scope.createChild(childContext);
185+
var view = _boundViewFactory(childScope);
186+
var nodes = view.nodes;
187+
rows[index] = new _Row(_generateId(index, addition.item, index))
188+
..view = view
189+
..scope = childScope
190+
..nodes = nodes
191+
..startNode = nodes.first
192+
..endNode = nodes.last;
193+
_viewPort.insert(view, insertAfter: previousView);
194+
};
195+
});
196+
197+
moveFn((CollectionChangeItem move) {
198+
var previousIndex = move.previousIndex;
199+
var value = move.item;
200+
changeFunctions[move.currentIndex] = (index, previousView) {
201+
var previousRow = _rows[previousIndex];
202+
var childScope = previousRow.scope;
203+
var childContext = _updateContext(childScope.context, index, length);
204+
if (!identical(childScope.context[_valueIdentifier], value)) {
205+
childContext[_valueIdentifier] = value;
206+
}
207+
rows[index] = _rows[previousIndex];
208+
// Only move the DOM node when required
209+
if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
210+
_viewPort.move(previousRow.view, moveAfter: previousView);
211+
leftInDom.remove(previousIndex);
212+
}
213+
domIndex--;
214+
};
215+
});
219216

220217
var previousView = null;
221218
domIndex = leftInDom.length - 1;

0 commit comments

Comments
 (0)