Skip to content

Commit

Permalink
chore: sync back
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jan 3, 2025
1 parent f12ff5f commit 174c2c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
rangeRef.current.start = start;
rangeRef.current.end = end;

React.useLayoutEffect(() => {
console.log('>>>> offsetTop', offsetTop);
console.log('>>>>', scrollHeight, start, end, heights.getRecord());

const changedRecord = heights.getRecord();
if (changedRecord.size === 1) {
const recordKey = Array.from(changedRecord)[0];
const startIndexKey = getKey(mergedData[start]);
if (startIndexKey === recordKey) {
const realStartHeight = heights.get(recordKey);
const diffHeight = realStartHeight - itemHeight;
syncScrollTop((ori) => {
console.log('-->', diffHeight, ori);
return ori + diffHeight;
});
}
}

heights.resetRecord();
}, [scrollHeight]);

// ================================= Size =================================
const [size, setSize] = React.useState({ width: 0, height });

Expand Down
15 changes: 15 additions & 0 deletions src/utils/CacheMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ class CacheMap {
// `useMemo` no need to update if `id` not change
id: number = 0;

diffKeys = new Set<React.Key>();

constructor() {
this.maps = Object.create(null);
}

set(key: React.Key, value: number) {
this.maps[key as string] = value;
this.id += 1;
this.diffKeys.add(key as string);
}

get(key: React.Key) {
return this.maps[key as string];
}

/**
* CacheMap will record the key changed.
* To help to know what's update in the next render.
*/
resetRecord() {
this.diffKeys.clear();
}

getRecord() {
return this.diffKeys;
}
}

export default CacheMap;

0 comments on commit 174c2c1

Please sign in to comment.