Skip to content

Commit dacf946

Browse files
committed
refactor(ng-repeat): refactor fix for dart-archive#1015
1 parent 46b4c0e commit dacf946

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

lib/directive/ng_repeat.dart

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,23 @@ class NgRepeat {
132132
_watch = _scope.watch(
133133
_listExpr,
134134
(CollectionChangeRecord changes, _) {
135-
_onChange((changes is CollectionChangeRecord) ? changes : null);
135+
if (changes is CollectionChangeRecord) {
136+
_onCollectionChange(changes);
137+
} else if (_rows != null) {
138+
_rows.forEach((row) {
139+
row.scope.destroy();
140+
_viewPort.remove(row.view);
141+
});
142+
}
136143
},
137144
collection: true,
138145
formatters: formatters
139146
);
140147
}
141148

142149
// 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;
150+
void _onCollectionChange(CollectionChangeRecord changes) {
151+
final int length = changes.length;
146152
final rows = new List<_Row>(length);
147153
final changeFunctions = new List<Function>(length);
148154
final removedIndexes = <int>[];
@@ -170,51 +176,43 @@ class NgRepeat {
170176
_rows = new List<_Row>(length);
171177
for (var i = 0; i < length; i++) {
172178
changeFunctions[i] = (index, previousView) {
173-
addRow(index, iterable.elementAt(i), previousView);
179+
addRow(index, changes.iterable.elementAt(i), previousView);
174180
};
175181
}
176182
} 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-
});
183+
changes.forEachRemoval((CollectionChangeItem removal) {
184+
var index = removal.previousIndex;
185+
var row = _rows[index];
186+
row.scope.destroy();
187+
_viewPort.remove(row.view);
188+
leftInDom.removeAt(domLength - 1 - index);
189+
});
191190

192-
changes.forEachAddition((CollectionChangeItem addition) {
193-
changeFunctions[addition.currentIndex] = (index, previousView) {
194-
addRow(index, addition.item, previousView);
195-
};
196-
});
191+
changes.forEachAddition((CollectionChangeItem addition) {
192+
changeFunctions[addition.currentIndex] = (index, previousView) {
193+
addRow(index, addition.item, previousView);
194+
};
195+
});
197196

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-
}
197+
changes.forEachMove((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+
});
218216
}
219217

220218
var previousView = null;

0 commit comments

Comments
 (0)