Skip to content

Commit 94b9b39

Browse files
committed
remove single item (which is out of buffer) by index
1 parent a438a8e commit 94b9b39

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/modules/adapter.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,31 @@ class Adapter {
148148
if (index % 1 !== 0) { // checking if it is an integer
149149
throw new Error('applyUpdates - ' + index + ' is not a valid index');
150150
}
151-
index -= this.buffer.first;
152-
if ((index >= 0 && index < this.buffer.length)) {
153-
this.applyUpdate(this.buffer[index], newItems);
151+
const _index = index - this.buffer.first;
152+
if (_index >= 0 && _index < this.buffer.length) {
153+
this.applyUpdate(this.buffer[_index], newItems);
154+
}
155+
else if(index >= this.buffer.minIndex && index <= this.buffer.maxIndex) {
156+
this.applyUpdateBuffer(index, newItems);
157+
}
158+
}
159+
160+
applyUpdateBuffer(index, newItems) {
161+
if (!angular.isArray(newItems)) {
162+
return;
163+
}
164+
// remove single item
165+
if(!newItems.length) {
166+
this.viewport.removeCacheItem(index, true)
154167
}
155168
}
156169

157170
applyUpdate(wrapper, newItems) {
158171
if (!angular.isArray(newItems)) {
159172
return;
160173
}
161-
let position = (this.buffer.indexOf(wrapper));
162-
if (!newItems.reverse().some((newItem) => newItem === wrapper.item)) {
174+
let position = this.buffer.indexOf(wrapper);
175+
if (!newItems.reverse().some(newItem => newItem === wrapper.item)) {
163176
wrapper.op = 'remove';
164177
if(position === 0 && !newItems.length) {
165178
wrapper._op = 'isTop'; // to catch "first" edge case on remove

src/modules/padding.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ class CacheProto {
1515
this.sort((a, b) => ((a.index < b.index) ? -1 : ((a.index > b.index) ? 1 : 0)));
1616
}
1717

18-
remove(itemToRemove) {
18+
remove(argument) {
19+
const index = argument % 1 === 0 ? argument : argument.scope.$index;
20+
const isTop = argument % 1 === 0 ? false : argument._op === 'isTop';
1921
for (let i = this.length - 1; i >= 0; i--) {
20-
if (this[i].index === itemToRemove.scope.$index) {
22+
if (this[i].index === index) {
2123
this.splice(i, 1);
2224
break;
2325
}
2426
}
25-
if(itemToRemove._op !== 'isTop') {
27+
if(!isTop) {
2628
for (let i = this.length - 1; i >= 0; i--) {
27-
if (this[i].index > itemToRemove.scope.$index) {
29+
if (this[i].index > index) {
2830
this[i].index--;
2931
}
3032
}

src/modules/viewport.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,13 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
182182
bottomPadding.cache.clear();
183183
},
184184

185-
removeItem(item) {
185+
removeCacheItem(item) {
186186
topPadding.cache.remove(item);
187187
bottomPadding.cache.remove(item);
188+
},
189+
190+
removeItem(item) {
191+
this.removeCacheItem(item);
188192
return buffer.remove(item);
189193
}
190194
});

0 commit comments

Comments
 (0)