-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app): fix ODD IntersectionObserver reference cycle (#16743)
Works toward EXEC-807 We have this code in the ODD that runs every render cycle on a lot of the "idle" views that manages the scrollbar. We instantiate a new observer on every render, but the old observer is never cleaned up. This commit ensures that we only ever instantiate one observer, and we properly remove it on component derender.
- Loading branch information
Showing
12 changed files
with
139 additions
and
89 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/src/local-resources/dom-utils/hooks/__tests__/useScrollPosition.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { renderHook, act } from '@testing-library/react' | ||
import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
|
||
import { useScrollPosition } from '../useScrollPosition' | ||
|
||
describe('useScrollPosition', () => { | ||
const mockObserve = vi.fn() | ||
const mockDisconnect = vi.fn() | ||
let intersectionCallback: (entries: IntersectionObserverEntry[]) => void | ||
|
||
beforeEach(() => { | ||
vi.stubGlobal( | ||
'IntersectionObserver', | ||
vi.fn(callback => { | ||
intersectionCallback = callback | ||
return { | ||
observe: mockObserve, | ||
disconnect: mockDisconnect, | ||
unobserve: vi.fn(), | ||
} | ||
}) | ||
) | ||
}) | ||
|
||
it('should return initial state and ref', () => { | ||
const { result } = renderHook(() => useScrollPosition()) | ||
|
||
expect(result.current.isScrolled).toBe(false) | ||
expect(result.current.scrollRef).toBeDefined() | ||
expect(result.current.scrollRef.current).toBe(null) | ||
}) | ||
|
||
it('should observe when ref is set', async () => { | ||
const { result } = renderHook(() => useScrollPosition()) | ||
|
||
const div = document.createElement('div') | ||
|
||
await act(async () => { | ||
// @ts-expect-error we're forcibly setting readonly ref | ||
result.current.scrollRef.current = div | ||
|
||
const observer = new IntersectionObserver(intersectionCallback) | ||
observer.observe(div) | ||
}) | ||
|
||
expect(mockObserve).toHaveBeenCalledWith(div) | ||
}) | ||
|
||
it('should update isScrolled when intersection changes for both scrolled and unscrolled cases', () => { | ||
const { result } = renderHook(() => useScrollPosition()) | ||
|
||
act(() => { | ||
intersectionCallback([ | ||
{ isIntersecting: false } as IntersectionObserverEntry, | ||
]) | ||
}) | ||
|
||
expect(result.current.isScrolled).toBe(true) | ||
|
||
act(() => { | ||
intersectionCallback([ | ||
{ isIntersecting: true } as IntersectionObserverEntry, | ||
]) | ||
}) | ||
|
||
expect(result.current.isScrolled).toBe(false) | ||
}) | ||
|
||
it('should disconnect observer on unmount', () => { | ||
const { unmount } = renderHook(() => useScrollPosition()) | ||
|
||
unmount() | ||
|
||
expect(mockDisconnect).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useScrollPosition' |
27 changes: 27 additions & 0 deletions
27
app/src/local-resources/dom-utils/hooks/useScrollPosition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useRef, useState, useEffect } from 'react' | ||
|
||
import type { RefObject } from 'react' | ||
|
||
export function useScrollPosition(): { | ||
scrollRef: RefObject<HTMLDivElement> | ||
isScrolled: boolean | ||
} { | ||
const scrollRef = useRef<HTMLDivElement>(null) | ||
const [isScrolled, setIsScrolled] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver(([entry]) => { | ||
setIsScrolled(!entry.isIntersecting) | ||
}) | ||
|
||
if (scrollRef.current != null) { | ||
observer.observe(scrollRef.current) | ||
} | ||
|
||
return () => { | ||
observer.disconnect() | ||
} | ||
}, []) | ||
|
||
return { scrollRef, isScrolled } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './hooks' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters