-
Notifications
You must be signed in to change notification settings - Fork 45
perf(svelte): reuse incremental parsing in streams #396
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
Open
onmax
wants to merge
6
commits into
comarkdown:main
Choose a base branch
from
onmax:fix/react-svelte-incremental-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20d32eb
perf: reuse incremental parsing in React and Svelte streams
onmax e336f65
refactor: keep Svelte streaming changes in this PR
onmax e49a6a4
fix(svelte): preserve streamed headings and reference links
onmax 9fabc1a
fix: reuse internal `createSerializedMarkdownParser`
farnabaz db3da78
Merge branch 'main' into fix/react-svelte-incremental-streaming
farnabaz 82638f3
fix(svelte): route parser errors to boundaries
onmax 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
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
173 changes: 173 additions & 0 deletions
173
packages/comark-svelte/test/incremental-streaming.svelte.test.ts
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,173 @@ | ||
| import type { ComarkPlugin } from 'comark' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { render } from 'vitest-browser-svelte' | ||
| import Markdown from '../src/components/Markdown.svelte' | ||
| import MarkdownAsync from '../src/async/MarkdownAsync.svelte' | ||
| import MarkdownBoundary from './test-components/MarkdownBoundary.svelte' | ||
|
|
||
| for (const [name, component] of [ | ||
| ['Markdown', Markdown], | ||
| ['MarkdownAsync', MarkdownAsync], | ||
| ] as const) { | ||
| describe(`${name} incremental parsing`, () => { | ||
| it('parses only the open tail and reparses the full value when streaming ends', async () => { | ||
| const inputs: string[] = [] | ||
| const plugin: ComarkPlugin = { | ||
| name: 'inputs', | ||
| pre: (state) => { | ||
| inputs.push(state.markdown) | ||
| }, | ||
| } | ||
| const initial = 'First\n\nSecond\n\nThird' | ||
| const screen = await render(MarkdownBoundary, { | ||
| component, | ||
| value: initial, | ||
| plugins: [plugin], | ||
| streaming: true, | ||
| }) | ||
| await expect.element(screen.getByText('Third')).toBeInTheDocument() | ||
|
|
||
| const value = `${initial} grows` | ||
| await screen.rerender({ value }) | ||
| await expect.element(screen.getByText('Third grows')).toBeInTheDocument() | ||
| expect(inputs).toHaveLength(2) | ||
| expect(inputs[1]).not.toContain('First') | ||
| expect(inputs[1]!.length).toBeLessThan(value.length) | ||
| await expect.element(screen.getByText('First')).toBeInTheDocument() | ||
|
|
||
| await screen.rerender({ streaming: false }) | ||
| await expect.poll(() => inputs.length).toBe(3) | ||
| expect(inputs[2]).toBe(value) | ||
|
|
||
| await screen.rerender({ value: 'Replacement' }) | ||
| await expect.element(screen.getByText('Replacement')).toBeInTheDocument() | ||
| expect(screen.container.textContent).not.toContain('First') | ||
| }) | ||
|
|
||
| it('recreates the parser when options, plugins, or unwrap change', async () => { | ||
| const value = 'https://example.com\n\nTail' | ||
| const screen = await render(MarkdownBoundary, { | ||
| component, | ||
| value, | ||
| streaming: true, | ||
| options: { linkify: false }, | ||
| }) | ||
| await expect.element(screen.getByText('Tail')).toBeInTheDocument() | ||
| expect(screen.container.querySelector('a')).toBeNull() | ||
|
|
||
| await screen.rerender({ options: { linkify: true } }) | ||
| await expect.element(screen.getByRole('link')).toHaveAttribute('href', 'https://example.com') | ||
|
|
||
| const plugin: ComarkPlugin = { | ||
| name: 'replace', | ||
| pre: (state) => { | ||
| state.markdown = state.markdown.replace('Tail', 'Changed') | ||
| }, | ||
| } | ||
| await screen.rerender({ plugins: [plugin] }) | ||
| await expect.element(screen.getByText('Changed')).toBeInTheDocument() | ||
|
|
||
| await screen.rerender({ unwrap: true }) | ||
| await expect.poll(() => screen.container.querySelector('p')).toBeNull() | ||
| expect(screen.container.textContent).toContain('Changed') | ||
| }) | ||
|
|
||
| it('ignores old plugin results after a configuration change', async () => { | ||
| const { promise: gate, resolve: release } = Promise.withResolvers<void>() | ||
| let finished = false | ||
| const screen = await render(MarkdownBoundary, { | ||
| component, | ||
| value: 'Old', | ||
| streaming: true, | ||
| plugins: [ | ||
| { | ||
| name: 'slow', | ||
| async pre() { | ||
| await gate | ||
| finished = true | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| await screen.rerender({ value: 'New', plugins: [] }) | ||
| await expect.element(screen.getByText('New')).toBeInTheDocument() | ||
| release() | ||
| await expect.poll(() => finished).toBe(true) | ||
| await expect.element(screen.getByText('New')).toBeInTheDocument() | ||
| expect(screen.container.textContent).not.toContain('Old') | ||
| }) | ||
|
|
||
| it('serializes overlapping plugin work and applies the newest update', async () => { | ||
| const { promise: gate, resolve: release } = Promise.withResolvers<void>() | ||
| let active = 0 | ||
| let maxActive = 0 | ||
| const inputs: string[] = [] | ||
| const plugin: ComarkPlugin = { | ||
| name: 'deferred', | ||
| async pre(state) { | ||
| active++ | ||
| maxActive = Math.max(maxActive, active) | ||
| inputs.push(state.markdown) | ||
| if (state.markdown.includes('Slow')) await gate | ||
| active-- | ||
| }, | ||
| } | ||
| const screen = await render(MarkdownBoundary, { component, value: 'Ready', streaming: true, plugins: [plugin] }) | ||
| await expect.element(screen.getByText('Ready')).toBeInTheDocument() | ||
| await screen.rerender({ value: 'Slow' }) | ||
| await expect.poll(() => inputs).toContain('Slow') | ||
| await screen.rerender({ value: 'Latest' }) | ||
| release() | ||
| await expect.element(screen.getByText('Latest')).toBeInTheDocument() | ||
| expect(maxActive).toBe(1) | ||
| expect(screen.container.textContent).not.toContain('Slow') | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| it('passes parser errors to the Svelte boundary', async () => { | ||
| const screen = await render(MarkdownBoundary, { | ||
| component: Markdown, | ||
| value: 'Failure', | ||
| streaming: true, | ||
| plugins: [ | ||
| { | ||
| name: 'fail', | ||
| async pre() { | ||
| throw new Error('Plugin failed') | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| await expect.element(screen.getByRole('alert')).toHaveTextContent('Plugin failed') | ||
| }) | ||
|
|
||
| it('shows completed updates while the next update is still parsing', async () => { | ||
| const { promise: first, resolve: releaseFirst } = Promise.withResolvers<void>() | ||
| const { promise: last, resolve: releaseLast } = Promise.withResolvers<void>() | ||
| const started: string[] = [] | ||
| const screen = await render(MarkdownBoundary, { | ||
| component: Markdown, | ||
| value: 'Ready', | ||
| streaming: true, | ||
| plugins: [ | ||
| { | ||
| name: 'slow', | ||
| async pre(state) { | ||
| started.push(state.markdown) | ||
| if (state.markdown === 'First') await first | ||
| if (state.markdown === 'Last') await last | ||
| }, | ||
| } satisfies ComarkPlugin, | ||
| ], | ||
| }) | ||
| await expect.element(screen.getByText('Ready')).toBeInTheDocument() | ||
| await screen.rerender({ value: 'First' }) | ||
| await expect.poll(() => started).toContain('First') | ||
| await screen.rerender({ value: 'Last' }) | ||
| releaseFirst() | ||
| await expect.poll(() => started).toContain('Last') | ||
| await expect.element(screen.getByText('First')).toBeInTheDocument() | ||
| releaseLast() | ||
| await expect.element(screen.getByText('Last')).toBeInTheDocument() | ||
| }) |
12 changes: 12 additions & 0 deletions
12
packages/comark-svelte/test/test-components/MarkdownBoundary.svelte
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,12 @@ | ||
| <script lang="ts"> | ||
| import type { ComponentProps } from 'svelte' | ||
| import type Markdown from '../../src/components/Markdown.svelte' | ||
|
|
||
| let { component: Component, ...props }: ComponentProps<typeof Markdown> & { component: typeof Markdown } = $props() | ||
| </script> | ||
|
|
||
| <svelte:boundary> | ||
| <Component {...props} /> | ||
| {#snippet pending()}<p>Loading</p>{/snippet} | ||
| {#snippet failed(error)}<p role="alert">{error instanceof Error ? error.message : String(error)}</p>{/snippet} | ||
| </svelte:boundary> |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.