-
-
Notifications
You must be signed in to change notification settings - Fork 244
fix(markdown): align streaming animation timing #490
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
Closed
+192
−18
Closed
Changes from all commits
Commits
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
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,23 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { useStreamQueue } from './useStreamQueue'; | ||
|
|
||
| describe('useStreamQueue', () => { | ||
| it('accelerates the active block when the preferred char delay drops', () => { | ||
| const blocks = [{ content: 'streaming block', startOffset: 0 }]; | ||
|
|
||
| const { result, rerender } = renderHook( | ||
| ({ preferredCharDelay }) => useStreamQueue(blocks, { preferredCharDelay }), | ||
| { initialProps: { preferredCharDelay: 24 } }, | ||
| ); | ||
|
|
||
| expect(result.current.charDelay).toBe(24); | ||
|
|
||
| rerender({ preferredCharDelay: 8 }); | ||
| expect(result.current.charDelay).toBe(8); | ||
|
|
||
| rerender({ preferredCharDelay: 30 }); | ||
| expect(result.current.charDelay).toBe(8); | ||
| }); | ||
| }); |
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,40 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { act } from 'react'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { useDelayedAnimated } from './useDelayedAnimated'; | ||
|
|
||
| const Demo = ({ animated, delayMs }: { animated?: boolean; delayMs?: number }) => { | ||
| const value = useDelayedAnimated(animated, delayMs); | ||
|
|
||
| return <div data-testid="value">{String(value)}</div>; | ||
| }; | ||
|
|
||
| describe('useDelayedAnimated', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('keeps animation enabled until the configured delay completes', () => { | ||
| const { rerender } = render(<Demo animated delayMs={1600} />); | ||
|
|
||
| expect(screen.getByTestId('value').textContent).toBe('true'); | ||
|
|
||
| rerender(<Demo animated={false} delayMs={1600} />); | ||
| expect(screen.getByTestId('value').textContent).toBe('true'); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(1599); | ||
| }); | ||
| expect(screen.getByTestId('value').textContent).toBe('true'); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(1); | ||
| }); | ||
| expect(screen.getByTestId('value').textContent).toBe('false'); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,23 +1,23 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| export const useDelayedAnimated = (animated?: boolean) => { | ||
| export const useDelayedAnimated = (animated?: boolean, delayMs = 1000) => { | ||
| const [delayedAnimated, setDelayedAnimated] = useState(animated); | ||
|
|
||
| // Watch for changes in animated prop | ||
| useEffect(() => { | ||
| if (animated === undefined) return; | ||
| // If animated changes from true to false, delay the update by 1 second | ||
| // Keep stream rendering alive long enough for the tail animation to finish. | ||
| if (animated === false && delayedAnimated === true) { | ||
| const timer = setTimeout(() => { | ||
| setDelayedAnimated(false); | ||
| }, 1000); | ||
| }, delayMs); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| } else { | ||
| // For any other changes, update immediately | ||
| setDelayedAnimated(animated); | ||
| } | ||
| }, [animated, delayedAnimated]); | ||
| }, [animated, delayedAnimated, delayMs]); | ||
|
|
||
| return delayedAnimated; | ||
| }; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating
frozenRef.current.delayfor the current active block makescharDelaychange in-flight, but the timeout that advancesrevealedCountis still recreated from the full block duration each time (setTimeoutis recomputed from(animatingCharCount - 1) * charDelay). During bursts where preferred delay keeps changing, this repeatedly restarts completion from “now” instead of using elapsed progress, so a block can stayanimatinglonger than intended and subsequent blocks remainqueued/hidden until updates settle.Useful? React with 👍 / 👎.