Skip to content

Commit a438a8e

Browse files
committed
split Adapter.applyUpdates method
1 parent 8919de5 commit a438a8e

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

src/modules/adapter.js

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,6 @@ class Adapter {
113113
return !this.buffer.length;
114114
}
115115

116-
applyUpdates(arg1, arg2) {
117-
if (angular.isFunction(arg1)) {
118-
// arg1 is the updater function, arg2 is ignored
119-
this.buffer.slice(0).forEach((wrapper) => {
120-
// we need to do it on the buffer clone, because buffer content
121-
// may change as we iterate through
122-
this.applyUpdate(wrapper, arg1(wrapper.item, wrapper.scope, wrapper.element));
123-
});
124-
} else {
125-
// arg1 is item index, arg2 is the newItems array
126-
if (arg1 % 1 !== 0) {// checking if it is an integer
127-
throw new Error('applyUpdates - ' + arg1 + ' is not a valid index');
128-
}
129-
130-
const index = arg1 - this.buffer.first;
131-
if ((index >= 0 && index < this.buffer.length)) {
132-
this.applyUpdate(this.buffer[index], arg2);
133-
}
134-
}
135-
136-
this.adjustBuffer();
137-
}
138-
139116
append(newItems) {
140117
this.buffer.append(newItems);
141118
this.adjustBuffer();
@@ -150,6 +127,54 @@ class Adapter {
150127
this.viewport.clipBottom();
151128
}
152129

130+
applyUpdates(arg1, arg2) {
131+
if (angular.isFunction(arg1)) {
132+
this.applyUpdatesFunc(arg1);
133+
} else {
134+
this.applyUpdatesIndex(arg1, arg2);
135+
}
136+
this.adjustBuffer();
137+
}
138+
139+
applyUpdatesFunc(cb) {
140+
this.buffer.slice(0).forEach((wrapper) => {
141+
// we need to do it on the buffer clone, because buffer content
142+
// may change as we iterate through
143+
this.applyUpdate(wrapper, cb(wrapper.item, wrapper.scope, wrapper.element));
144+
});
145+
}
146+
147+
applyUpdatesIndex(index, newItems) {
148+
if (index % 1 !== 0) { // checking if it is an integer
149+
throw new Error('applyUpdates - ' + index + ' is not a valid index');
150+
}
151+
index -= this.buffer.first;
152+
if ((index >= 0 && index < this.buffer.length)) {
153+
this.applyUpdate(this.buffer[index], newItems);
154+
}
155+
}
156+
157+
applyUpdate(wrapper, newItems) {
158+
if (!angular.isArray(newItems)) {
159+
return;
160+
}
161+
let position = (this.buffer.indexOf(wrapper));
162+
if (!newItems.reverse().some((newItem) => newItem === wrapper.item)) {
163+
wrapper.op = 'remove';
164+
if(position === 0 && !newItems.length) {
165+
wrapper._op = 'isTop'; // to catch "first" edge case on remove
166+
}
167+
}
168+
newItems.forEach((newItem) => {
169+
if (newItem === wrapper.item) {
170+
position--;
171+
} else {
172+
// 3 parameter (isTop) is to catch "first" edge case on insert
173+
this.buffer.insert(position + 1, newItem, position === -1);
174+
}
175+
});
176+
}
177+
153178
calculateProperties() {
154179
let rowTop = null, topHeight = 0;
155180
let topDone = false, bottomDone = false;
@@ -169,7 +194,6 @@ class Adapter {
169194
this['topVisibleElement'] = item.element;
170195
this['topVisibleScope'] = item.scope;
171196
}
172-
173197
if (!bottomDone && (top >= this.viewport.bottomVisiblePos() || (i === length - 1 && this.isEOF()))) {
174198
bottomDone = true;
175199
this['bottomVisible'] = item.item;
@@ -178,7 +202,6 @@ class Adapter {
178202
}
179203
topHeight += itemHeight;
180204
}
181-
182205
rowTop = itemTop;
183206

184207
if (topDone && bottomDone) {
@@ -187,29 +210,6 @@ class Adapter {
187210
}
188211
}
189212

190-
applyUpdate(wrapper, newItems) {
191-
if (!angular.isArray(newItems)) {
192-
return;
193-
}
194-
195-
let position = (this.buffer.indexOf(wrapper));
196-
if (!newItems.reverse().some((newItem) => newItem === wrapper.item)) {
197-
wrapper.op = 'remove';
198-
if(position === 0 && !newItems.length) {
199-
wrapper._op = 'isTop'; // to catch "first" edge case on remove
200-
}
201-
}
202-
203-
newItems.forEach((newItem) => {
204-
if (newItem === wrapper.item) {
205-
position--;
206-
} else {
207-
// 3 parameter (isTop) is to catch "first" edge case on insert
208-
this.buffer.insert(position + 1, newItem, position === -1);
209-
}
210-
});
211-
}
212-
213213
}
214214

215215
export default Adapter;

0 commit comments

Comments
 (0)