Skip to content

Commit

Permalink
Merge pull request #392 from preactjs/tree-view-indent
Browse files Browse the repository at this point in the history
Improve layout measurement for virtualized lists
  • Loading branch information
marvinhagemeister committed Jun 6, 2022
2 parents 03a70ee + fcfedd9 commit 93f2b3b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/shells/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export function throttle<T extends any[]>(
if (!running) {
callback(...args);
running = true;
setTimeout(() => (running = false), wait);
setTimeout(() => {
running = false;
callback(...args);
}, wait);
}
};
}
Expand Down
21 changes: 9 additions & 12 deletions src/view/components/elements/VirtualizedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { RefObject, VNode } from "preact";
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
Expand Down Expand Up @@ -83,12 +82,6 @@ export function useVirtualizedList<T>({
[rowHeight],
);

useLayoutEffect(() => {
if (container.current) {
setHeight(container.current.clientHeight);
}
}, []);

useEffect(() => {
const scrollFn = (e: Event) => {
const top = (e.target as Element).scrollTop;
Expand All @@ -109,11 +102,15 @@ export function useVirtualizedList<T>({
};
}, [container.current]);

useResize(() => {
if (container.current) {
setHeight(container.current.clientHeight);
}
}, []);
useResize(
() => {
if (container.current) {
setHeight(container.current.clientHeight);
}
},
[],
true,
);

const vnodes = useMemo(() => {
const vnodes: VNode[] = [];
Expand Down
8 changes: 3 additions & 5 deletions src/view/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useContext, useLayoutEffect } from "preact/hooks";
import { useContext, useLayoutEffect } from "preact/hooks";
import { WindowCtx } from "../store/react-bindings";
import { throttle } from "../../shells/shared/utils";

Expand All @@ -11,15 +11,13 @@ export function useResize(fn: () => void, args: any[], init = false) {
// the global window object instead.
const win = useContext(WindowCtx) || window;

useEffect(() => {
useLayoutEffect(() => {
if (init) fn();
}, []);

useLayoutEffect(() => {
const fn2 = throttle(fn, 60);
win.addEventListener("resize", fn2);
return () => {
win.removeEventListener("resize", fn2);
};
}, args);
}, [...args, init]);
}
2 changes: 1 addition & 1 deletion test-e2e/tests/profiler/ranked/profiler-ranked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function run(config: any) {
await clickRecordButton(devtools);

const nodes = await devtools.$$(
'[data-type="ranked"] > *:not([data-weight])',
'[data-type="ranked"] [data-id]:not([data-weight])',
);
expect(nodes.length).to.equal(0);

Expand Down

0 comments on commit 93f2b3b

Please sign in to comment.