Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close #31, integerBufferSet get position method #33

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/data-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"@x-oasis/batchinator": "0.1.10",
"@x-oasis/capitalize": "0.1.10",
"@x-oasis/default-boolean-value": "^0.1.10",
"@x-oasis/integer-buffer-set": "0.1.10",
"@x-oasis/integer-buffer-set": "0.1.13",
"@x-oasis/is-clamped": "0.1.10",
"@x-oasis/layout-equal": "0.1.10",
"@x-oasis/layout-equal": "0.1.13",
"@x-oasis/noop": "0.1.10",
"@x-oasis/omit": "0.1.10",
"@x-oasis/prefix-interval-tree": "^0.1.10",
Expand Down
105 changes: 74 additions & 31 deletions packages/data-model/src/ListDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,13 +872,21 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
}
const _info = this.normalizeLengthInfo(info);

if (!layoutEqual(meta.getLayout(), _info as ItemLayout)) {
if (meta.getLayout()) {
console.warn(
'[infinite-list/data-model] override existing key item ',
`${+key}from value ${meta.getLayout()}to ${_info}`
);
}
if (
!layoutEqual(
meta.getLayout(),
_info as ItemLayout,
this.horizontal ? ['width'] : ['height']
)
) {
// if (meta.getLayout()) {
// console.warn(
// '[infinite-list/data-model] override existing key item ',
// `${key} from value ${JSON.stringify(
// meta.getLayout()
// )}to ${JSON.stringify(_info)}`
// );
// }

const currentLength = this._selectValue.selectLength(
meta.getLayout() || {}
Expand Down Expand Up @@ -1074,7 +1082,13 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
position = this._bufferSet.replaceFurthestValuePosition(
startIndex,
endIndex,
rowIndex
rowIndex,
(options) => {
const { bufferSetRange, currentIndex } = options;
const { maxValue } = bufferSetRange;
if (currentIndex > maxValue) return true;
return false;
}
);
}

Expand Down Expand Up @@ -1148,8 +1162,13 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
}

resolveRecycleRecycleState(state: ListState<ItemT>) {
const { visibleEndIndex, visibleStartIndex: _visibleStartIndex } = state;
const {
visibleEndIndex,
visibleStartIndex: _visibleStartIndex,
isEndReached,
} = state;
const targetIndices = this._bufferSet.indices.map((i) => parseInt(i));
const targetIndicesCopy = targetIndices.slice();
const recycleStateResult = [];
const velocity = this._scrollMetrics?.velocity || 0;
// const targetIndicesCopy = targetIndices.slice();
Expand All @@ -1175,31 +1194,35 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
}
}

const remainingPosition = Math.max(
this.recycleThreshold - (safeRange.endIndex - safeRange.startIndex + 1),
0
);
const remainingCount = Math.min(
this.recycleBufferedCount * 2,
remainingPosition
);
// const remainingPosition = Math.max(
// this.recycleThreshold - (safeRange.endIndex - safeRange.startIndex + 1),
// 0
// );
// const remainingCount = Math.min(
// this.recycleBufferedCount * 2,
// remainingPosition
// );

if (velocity > 0) {
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleEndIndex + 1,
maxCount: remainingCount,
step: 1,
});
if (isEndReached || Math.abs(velocity) < 0.5) {
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleEndIndex + 1,
maxCount: this.recycleBufferedCount,
step: 1,
});
}
} else if (velocity < 0) {
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleStartIndex - 1,
maxCount: remainingCount,
step: -1,
});
if (Math.abs(velocity) < 0.5) {
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleStartIndex - 1,
maxCount: this.recycleBufferedCount,
step: -1,
});
}
} else {
const part = Math.floor(remainingCount / 2);
const part = Math.floor(this.recycleBufferedCount / 2);
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleStartIndex - 1,
Expand All @@ -1209,11 +1232,31 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
this.updateIndices(targetIndices, {
safeRange,
startIndex: visibleEndIndex + 1,
maxCount: remainingCount - part,
maxCount: this.recycleBufferedCount - part,
step: 1,
});
}

// let changed = '';

// for (let index = 0; index < targetIndices.length; index++) {
// const next = targetIndices[index];
// const current = targetIndicesCopy[index];
// if (current && next !== current) {
// changed += `${index} occurs update, ${current} -> ${next}\n`;
// }
// }

// if (changed)
// console.warn(
// '[infinite-list] replace info ',
// `visibleStartIndex : ${visibleStartIndex}, visibleEndIndex: ${visibleEndIndex} \n`,
// changed,
// `velocity: ${velocity}\n`,
// `prev: ${JSON.stringify(targetIndicesCopy)}\n`,
// `next: ${JSON.stringify(targetIndices)}\n`
// );

const minValue = this._bufferSet.getMinValue();
const maxValue = this._bufferSet.getMaxValue();
const indexToOffsetMap = this.getIndexRangeOffsetMap(
Expand Down
24 changes: 12 additions & 12 deletions packages/data-model/src/__test__/ListDimensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ describe('lifecycle', () => {
recycleList.stateResult as RecycleStateResult<any>;

expect(recycleListStateResult.spaceState.length).toBe(10);
expect(recycleListStateResult.recycleState.length).toBe(4);
expect(recycleListStateResult.recycleState.length).toBe(2);

// expect(recycleListStateResult.spaceState.map((v) => v.viewable)).toEqual([
// true,
Expand Down Expand Up @@ -1534,7 +1534,7 @@ describe('lifecycle', () => {
);
expect(recycleListStateResult.spaceState[10].length).toBe(9000);

expect(recycleListStateResult.recycleState.length).toBe(4);
expect(recycleListStateResult.recycleState.length).toBe(2);

// @ts-ignore
recycleList.updateScrollMetrics({
Expand All @@ -1556,16 +1556,16 @@ describe('lifecycle', () => {

recycleListStateResult = recycleList.stateResult as RecycleStateResult<any>;

expect(recycleListStateResult.recycleState.length).toBe(4);
expect(recycleListStateResult.recycleState.length).toBe(2);

expect(recycleListStateResult.recycleState.map((v) => v.item.key)).toEqual([
10, 11, 12, 13,
10, 11,
]);
expect(recycleListStateResult.recycleState.map((v) => v.key)).toEqual([
'recycle_0',
'recycle_1',
'recycle_2',
'recycle_3',
// 'recycle_2',
// 'recycle_3',
]);
// expect(recycleListStateResult.recycleState.map((v) => v.viewable)).toEqual(
// []
Expand Down Expand Up @@ -1715,14 +1715,14 @@ describe('lifecycle', () => {
(recycleList.stateResult as RecycleStateResult<any>).recycleState.map(
(v) => v.item.key
)
).toEqual([38, 50, 51, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]);
).toEqual([50, 51, 26, 39, 40, 41, 42, 43, 44, 45, 46, 47, 37, 38]);
expect(
(recycleList.stateResult as RecycleStateResult<any>).recycleState.map(
(v) => v.offset
)
).toEqual([
3800, 5000, 5100, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700,
4800, 4900,
5000, 5100, 2600, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700,
3700, 3800,
]);

// @ts-ignore
Expand All @@ -1737,14 +1737,14 @@ describe('lifecycle', () => {
(recycleList.stateResult as RecycleStateResult<any>).recycleState.map(
(v) => v.item.key
)
).toEqual([38, 34, 33, 39, 40, 41, 42, 43, 44, 30, 32, 37, 36, 35]);
).toEqual([34, 33, 26, 39, 40, 41, 42, 43, 44, 45, 36, 35, 37, 38]);
expect(
(recycleList.stateResult as RecycleStateResult<any>).recycleState.map(
(v) => v.offset
)
).toEqual([
3800, 3400, 3300, 3900, 4000, 4100, 4200, 4300, 4400, 3000, 3200, 3700,
3600, 3500,
3400, 3300, 2600, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 3600, 3500,
3700, 3800,
]);
});
});
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.