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

Improve layout measurement for virtualized lists #392

Merged
merged 2 commits into from
Jun 6, 2022
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
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