Revert "Revert "[Refactoring] Intersection Observer: Revert redundant workaround for freezing""

This reverts commit 55d7648698917aa9c51d4a4e5ca18b8433a968bd.
This commit is contained in:
Alexander Zinchuk 2021-12-10 18:33:31 +01:00
parent 6afcae9512
commit 560299e129
2 changed files with 4 additions and 31 deletions

View File

@ -31,7 +31,6 @@ export default function useMessageObservers(
} = useIntersectionObserver({ } = useIntersectionObserver({
rootRef: containerRef, rootRef: containerRef,
throttleMs: INTERSECTION_THROTTLE_FOR_READING, throttleMs: INTERSECTION_THROTTLE_FOR_READING,
noAutoFreeze: true,
}, (entries) => { }, (entries) => {
if (type !== 'thread') { if (type !== 'thread') {
return; return;

View File

@ -22,8 +22,6 @@ interface Response {
unfreeze: NoneToVoidFunction; unfreeze: NoneToVoidFunction;
} }
const AUTO_UNFREEZE_TIMEOUT = 2000;
export function useIntersectionObserver({ export function useIntersectionObserver({
rootRef, rootRef,
throttleMs, throttleMs,
@ -32,7 +30,6 @@ export function useIntersectionObserver({
margin, margin,
threshold, threshold,
isDisabled, isDisabled,
noAutoFreeze = false,
}: { }: {
rootRef: RefObject<HTMLDivElement>; rootRef: RefObject<HTMLDivElement>;
throttleMs?: number; throttleMs?: number;
@ -41,16 +38,18 @@ export function useIntersectionObserver({
margin?: number; margin?: number;
threshold?: number | number[]; threshold?: number | number[];
isDisabled?: boolean; isDisabled?: boolean;
noAutoFreeze?: boolean;
}, rootCallback?: RootCallback): Response { }, rootCallback?: RootCallback): Response {
const controllerRef = useRef<IntersectionController>(); const controllerRef = useRef<IntersectionController>();
const rootCallbackRef = useRef<RootCallback>(); const rootCallbackRef = useRef<RootCallback>();
const freezeFlagsRef = useRef(0); const freezeFlagsRef = useRef(0);
const autoUnfreezeTimeoutRef = useRef<number>();
const onUnfreezeRef = useRef<NoneToVoidFunction>(); const onUnfreezeRef = useRef<NoneToVoidFunction>();
rootCallbackRef.current = rootCallback; rootCallbackRef.current = rootCallback;
const freeze = useCallback(() => {
freezeFlagsRef.current++;
}, []);
const unfreeze = useCallback(() => { const unfreeze = useCallback(() => {
if (!freezeFlagsRef.current) { if (!freezeFlagsRef.current) {
return; return;
@ -64,31 +63,6 @@ export function useIntersectionObserver({
} }
}, []); }, []);
const freeze = useCallback(() => {
freezeFlagsRef.current++;
if (noAutoFreeze) {
return;
}
if (autoUnfreezeTimeoutRef.current) {
clearTimeout(autoUnfreezeTimeoutRef.current);
autoUnfreezeTimeoutRef.current = undefined;
}
// Make sure to unfreeze even if unfreeze callback was not called (which was some hardly-reproducible bug)
autoUnfreezeTimeoutRef.current = window.setTimeout(() => {
autoUnfreezeTimeoutRef.current = undefined;
if (!freezeFlagsRef.current) {
return;
}
freezeFlagsRef.current = 1;
unfreeze();
}, AUTO_UNFREEZE_TIMEOUT);
}, [noAutoFreeze, unfreeze]);
useHeavyAnimationCheck(freeze, unfreeze); useHeavyAnimationCheck(freeze, unfreeze);
useEffect(() => { useEffect(() => {