Skip to content

Commit 4468f21

Browse files
committed
Adapter.clip method options
1 parent cd5fa61 commit 4468f21

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

src/component/classes/state/clip.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Direction } from '../../interfaces/index';
2-
31
export class ClipModel {
42
noClip: boolean;
53
doClip: boolean;

src/component/processes/preClip.ts

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,72 @@ export default class PreClip {
1414
}
1515

1616
static prepareClip(scroller: Scroller) {
17-
const { buffer, state, state: { fetch, fetch: { direction } } } = scroller;
18-
if (!buffer.size) {
19-
scroller.logger.log(`skipping clip [empty buffer]`);
20-
return;
21-
}
22-
if (state.isInitialWorkflowCycle && !state.scrollState.scroll) {
23-
scroller.logger.log(`skipping clip [initial cycle, no scroll]`);
17+
const { state: { fetch, fetch: { direction } } } = scroller;
18+
if (PreClip.shouldNotClip(scroller)) {
2419
return;
2520
}
2621
const firstIndex = <number>fetch.firstIndexBuffer;
2722
const lastIndex = <number>fetch.lastIndexBuffer;
2823
scroller.logger.log(() =>
2924
`looking for ${direction ? 'anti-' + direction + ' ' : ''}items ` +
3025
`that are out of [${firstIndex}..${lastIndex}] range`);
31-
if (!direction || direction === Direction.forward) {
32-
if (firstIndex - 1 >= buffer.absMinIndex) {
33-
PreClip.prepareClipByDirection(scroller, Direction.forward, firstIndex);
34-
}
26+
if (PreClip.isBackward(scroller, firstIndex)) {
27+
PreClip.prepareClipByDirection(scroller, Direction.backward, firstIndex);
3528
}
36-
if (!direction || direction === Direction.backward) {
37-
if (lastIndex + 1 <= buffer.absMaxIndex) {
38-
PreClip.prepareClipByDirection(scroller, Direction.backward, lastIndex);
39-
}
29+
if (PreClip.isForward(scroller, lastIndex)) {
30+
PreClip.prepareClipByDirection(scroller, Direction.forward, lastIndex);
4031
}
4132
if (!scroller.state.clip.doClip) {
4233
scroller.logger.log(`skipping clip [no items to clip]`);
4334
}
4435
return;
4536
}
4637

38+
static shouldNotClip(scroller: Scroller): boolean {
39+
const { buffer, state } = scroller;
40+
if (!buffer.size) {
41+
scroller.logger.log(`skipping clip [empty buffer]`);
42+
return true;
43+
}
44+
if (state.isInitialWorkflowCycle && !state.scrollState.scroll) {
45+
scroller.logger.log(`skipping clip [initial cycle, no scroll]`);
46+
return true;
47+
}
48+
return false;
49+
}
50+
51+
static isBackward(scroller: Scroller, firstIndex: number): boolean {
52+
const { buffer, state: { clip, fetch } } = scroller;
53+
if (clip.force) {
54+
return clip.forceBackward;
55+
}
56+
if (fetch.direction !== Direction.backward) {
57+
if (firstIndex - 1 >= buffer.absMinIndex) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
static isForward(scroller: Scroller, lastIndex: number): boolean {
65+
const { buffer, state: { clip, fetch } } = scroller;
66+
if (clip.force) {
67+
return clip.forceForward;
68+
}
69+
if (fetch.direction !== Direction.forward) {
70+
if (lastIndex + 1 <= buffer.absMaxIndex) {
71+
return true;
72+
}
73+
}
74+
return false;
75+
}
76+
4777
static prepareClipByDirection(scroller: Scroller, direction: Direction, edgeIndex: number) {
4878
const forward = direction === Direction.forward;
4979
scroller.buffer.items.forEach(item => {
5080
if (
51-
(forward && item.$index < edgeIndex) ||
52-
(!forward && item.$index > edgeIndex)
81+
(!forward && item.$index < edgeIndex) ||
82+
(forward && item.$index > edgeIndex)
5383
) {
5484
item.toRemove = true;
5585
item.removeDirection = direction;

src/component/processes/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class Start {
1010
if (!fetch.simulate) {
1111
fetch.reset();
1212
}
13-
if (!clip.simulate) {
13+
if (!clip.simulate && !clip.force) {
1414
clip.reset();
1515
}
1616
scrollState.scroll = workflowOptions.scroll || false;

0 commit comments

Comments
 (0)