Skip to content

Commit ead77aa

Browse files
committed
issue-171 use Remove inside Replace
1 parent 6dc1422 commit ead77aa

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/component/processes/adapter/remove.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ export default class Remove extends getBaseAdapterProcess(AdapterProcess.remove)
1111
return;
1212
}
1313

14+
const shouldRemove = Remove.removeItems(scroller, params);
15+
16+
scroller.workflow.call({
17+
process: Remove.process,
18+
status: shouldRemove ? ProcessStatus.next : ProcessStatus.done
19+
});
20+
}
21+
22+
static removeItems(scroller: Scroller, params: AdapterRemoveOptions, sequenceOnly = false): boolean {
1423
const shouldRemove = Remove.removeBufferedItems(scroller, params);
15-
const shouldRemoveVirtual = Remove.removeVirtualItems(scroller, params);
24+
const shouldRemoveVirtual = Remove.removeVirtualItems(scroller, params, sequenceOnly);
1625

1726
if (shouldRemove || shouldRemoveVirtual) {
1827
const { clip } = scroller.state;
@@ -25,10 +34,7 @@ export default class Remove extends getBaseAdapterProcess(AdapterProcess.remove)
2534
}
2635
}
2736

28-
scroller.workflow.call({
29-
process: Remove.process,
30-
status: shouldRemove || shouldRemoveVirtual ? ProcessStatus.next : ProcessStatus.done
31-
});
37+
return shouldRemove || shouldRemoveVirtual;
3238
}
3339

3440
static removeBufferedItems(scroller: Scroller, options: AdapterRemoveOptions): boolean {
@@ -68,26 +74,32 @@ export default class Remove extends getBaseAdapterProcess(AdapterProcess.remove)
6874
return result;
6975
}
7076

71-
static removeVirtualItems(scroller: Scroller, options: AdapterRemoveOptions): boolean {
72-
const { indexes } = options;
73-
let result = false;
77+
static removeVirtualItems(scroller: Scroller, { indexes }: AdapterRemoveOptions, sequenceOnly: boolean): boolean {
7478
if (!indexes) {
7579
return false;
7680
}
81+
let last = null;
7782
const { state: { clip } } = scroller;
7883
const { finiteAbsMinIndex, firstIndex, finiteAbsMaxIndex, lastIndex } = scroller.buffer;
79-
for (let i = indexes.length - 1; i >= 0; i--) {
84+
for (let i = 0, len = indexes.length; i < len; i++) {
85+
let dir = null;
8086
const index = indexes[i];
8187
if (index >= finiteAbsMinIndex && firstIndex !== null && index < firstIndex) {
82-
clip.virtual.backward.push(index);
83-
result = true;
88+
dir = Direction.backward;
8489
}
8590
if (index <= finiteAbsMaxIndex && lastIndex !== null && index > lastIndex) {
86-
clip.virtual.forward.push(index);
87-
result = true;
91+
dir = Direction.forward;
92+
}
93+
if (dir !== null) {
94+
if (sequenceOnly && last !== null && Math.abs(last - index) > 1) {
95+
// allow only first strict uninterrupted sequence
96+
break;
97+
}
98+
clip.virtual[dir].push(index);
99+
last = index;
88100
}
89101
}
90-
return result;
102+
return last !== null;
91103
}
92104

93105
}

src/component/processes/adapter/replace.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getBaseAdapterProcess } from './_base';
22
import { Scroller } from '../../scroller';
33
import { AdapterProcess, ProcessStatus, AdapterReplaceOptions } from '../../interfaces/index';
4+
import Remove from './remove';
45

56
export default class Replace extends getBaseAdapterProcess(AdapterProcess.replace) {
67

@@ -10,6 +11,15 @@ export default class Replace extends getBaseAdapterProcess(AdapterProcess.replac
1011
return;
1112
}
1213

14+
const shouldRemove = Remove.removeItems(scroller, params, true);
15+
if (!shouldRemove) {
16+
scroller.logger.log(() => 'no items to replace (not found)');
17+
return scroller.workflow.call({
18+
process: Replace.process,
19+
status: ProcessStatus.done
20+
});
21+
}
22+
1323
scroller.workflow.call({
1424
process: Replace.process,
1525
status: ProcessStatus.done

0 commit comments

Comments
 (0)