-
Notifications
You must be signed in to change notification settings - Fork 33
fix: DH-14657 Better disconnect handling #1261
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1adedd4
WIP debounce disconnect handling
mofojed 1683829
Fix up tests for useDebouncedValue
mofojed 94ed77d
Add the debounced modal
mofojed f0d2fbb
fix: DH-14657 Debounce the Reconnecting modal
mofojed 54e79c2
Update packages/react-hooks/src/useDebouncedValue.ts
mofojed 20eebfa
Clean up based on review
mofojed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,68 @@ | ||
| import React from 'react'; | ||
| import { DEFAULT_DEBOUNCE_MS } from '@deephaven/react-hooks'; | ||
| import { act, render, screen } from '@testing-library/react'; | ||
| import DebouncedModal from './DebouncedModal'; | ||
| import Modal from './Modal'; | ||
|
|
||
| const mockChildText = 'Mock Child'; | ||
| const children = ( | ||
| <Modal> | ||
| <div>{mockChildText}</div> | ||
| </Modal> | ||
| ); | ||
|
|
||
| beforeAll(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('display modal after debounce', () => { | ||
| it('should render the modal after the debounce time has passed', () => { | ||
| const { rerender } = render( | ||
| <DebouncedModal isOpen={false}>{children}</DebouncedModal> | ||
| ); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).toBeNull(); | ||
|
|
||
| act(() => { | ||
| rerender(<DebouncedModal isOpen>{children}</DebouncedModal>); | ||
| }); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).not.toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).toBeNull(); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS); | ||
| }); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).not.toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('should not block interaction if set to false', () => { | ||
| const { rerender } = render( | ||
| <DebouncedModal isOpen={false} blockInteraction={false}> | ||
| {children} | ||
| </DebouncedModal> | ||
| ); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).toBeNull(); | ||
|
|
||
| act(() => { | ||
| rerender( | ||
| <DebouncedModal isOpen blockInteraction={false}> | ||
| {children} | ||
| </DebouncedModal> | ||
| ); | ||
| }); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).toBeNull(); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS + 5); | ||
| }); | ||
| expect(screen.queryByTestId('debounced-modal-backdrop')).toBeNull(); | ||
| expect(screen.queryByText(mockChildText)).not.toBeNull(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,47 @@ | ||
| import React from 'react'; | ||
| import { DEFAULT_DEBOUNCE_MS, useDebouncedValue } from '@deephaven/react-hooks'; | ||
|
|
||
| export type DebouncedModalProps = { | ||
| /** Whether to block interaction immediately */ | ||
| blockInteraction?: boolean; | ||
|
|
||
| /** Children to render after the alloted debounce time */ | ||
| children: React.ReactElement; | ||
|
|
||
| /** Time to debounce */ | ||
| debounceMs?: number; | ||
|
|
||
| /** | ||
| * Will render the `children` `debounceMs` after `isOpen` is set to `true. | ||
| * Will stop rendering immediately after `isOpen` is set to `false`. | ||
| */ | ||
| isOpen?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Display a modal after a debounce time. Blocks the screen from interaction immediately, | ||
| * but then waits the set debounce time before rendering the modal. | ||
| */ | ||
| function DebouncedModal({ | ||
| blockInteraction = true, | ||
| children, | ||
| debounceMs = DEFAULT_DEBOUNCE_MS, | ||
| isOpen = false, | ||
| }: DebouncedModalProps) { | ||
| const debouncedIsOpen = useDebouncedValue(isOpen, debounceMs); | ||
|
|
||
| return ( | ||
| <> | ||
| {blockInteraction && isOpen && ( | ||
| <div | ||
| className="modal-backdrop" | ||
| style={{ backgroundColor: 'transparent' }} | ||
| data-testid="debounced-modal-backdrop" | ||
| /> | ||
| )} | ||
| {React.cloneElement(children, { isOpen: isOpen && debouncedIsOpen })} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default DebouncedModal; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| import { act, renderHook } from '@testing-library/react-hooks'; | ||
| import useDebouncedValue, { DEFAULT_DEBOUNCE_MS } from './useDebouncedValue'; | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('should return the initial value', () => { | ||
| const value = 'mock value'; | ||
| const { result } = renderHook(() => useDebouncedValue(value)); | ||
| expect(result.current).toBe(value); | ||
| }); | ||
|
|
||
| it('should return the initial value after the debounce time has elapsed', () => { | ||
| const value = 'mock value'; | ||
| const { result, rerender } = renderHook(() => useDebouncedValue(value)); | ||
| expect(result.current).toBe(value); | ||
| rerender(); | ||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS); | ||
| }); | ||
| expect(result.current).toBe(value); | ||
|
mofojed marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| it('should return the updated value after the debounce time has elapsed', () => { | ||
| const value = 'mock value'; | ||
| const newValue = 'mock new value'; | ||
| const { result, rerender } = renderHook((val = value) => | ||
| useDebouncedValue(val) | ||
| ); | ||
| expect(result.current).toBe(value); | ||
| rerender(newValue); | ||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS); | ||
| }); | ||
| expect(result.current).toBe(newValue); | ||
| }); | ||
|
|
||
| it('should not return an intermediate value if the debounce time has not elapsed', () => { | ||
| const value = 'mock value'; | ||
| const intermediateValue = 'mock intermediate value'; | ||
| const newValue = 'mock new value'; | ||
| const { result, rerender } = renderHook((val = value) => | ||
| useDebouncedValue(val) | ||
| ); | ||
| expect(result.current).toBe(value); | ||
| rerender(intermediateValue); | ||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS - 5); | ||
| }); | ||
| expect(result.current).toBe(value); | ||
| rerender(newValue); | ||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS - 5); | ||
| }); | ||
| expect(result.current).toBe(value); | ||
| act(() => { | ||
| jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS); | ||
| }); | ||
| expect(result.current).toBe(newValue); | ||
| }); | ||
This file contains hidden or 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,28 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| export const DEFAULT_DEBOUNCE_MS = 250; | ||
|
|
||
| /** | ||
| * Debounces a value. Returns the value after the debounce time has elapsed. | ||
|
mofojed marked this conversation as resolved.
Outdated
|
||
| * @param value Value to debounce | ||
| * @param debounceMs Amount of time to debounce | ||
| * @returns The debounced value | ||
| */ | ||
| export function useDebouncedValue<T>( | ||
| value: T, | ||
| debounceMs = DEFAULT_DEBOUNCE_MS | ||
|
mofojed marked this conversation as resolved.
Outdated
|
||
| ) { | ||
| const [debouncedValue, setDebouncedValue] = useState<T>(value); | ||
| useEffect(() => { | ||
| const timeoutId = setTimeout(() => { | ||
| setDebouncedValue(value); | ||
| }, debounceMs); | ||
| return () => { | ||
| clearTimeout(timeoutId); | ||
| }; | ||
| }, [value, debounceMs]); | ||
|
mofojed marked this conversation as resolved.
|
||
|
|
||
| return debouncedValue; | ||
| } | ||
|
|
||
| export default useDebouncedValue; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.