Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions src/__tests__/useStreamdownMarkdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,132 @@ describe('useStreamdownMarkdown', () => {
});
expect(processRemendInWorklet).toHaveBeenCalledTimes(2);
});

it('renders once per markdown change and derives isStreaming from the processed result', () => {
const pending: Array<() => void> = [];
const runSynchronously = processRemendInWorklet.getMockImplementation();
processRemendInWorklet.mockImplementation(
(markdown: string, onComplete: (r: string) => void) => {
pending.push(() => onComplete(`${markdown}!`));
}
);

const states: Array<{ processedMarkdown: string; isStreaming: boolean }> =
[];
const Probe = ({ markdown }: { markdown: string }) => {
states.push(useStreamdownMarkdown(markdown));
return null;
};
const flushWorklet = () => {
act(() => {
pending.splice(0).forEach((complete) => complete());
});
};

let renderer!: TestRenderer.ReactTestRenderer;
act(() => {
renderer = TestRenderer.create(<Probe markdown="**a" />);
});
// No follow-up render from the effect: the only render is the mount.
expect(states).toEqual([{ processedMarkdown: '', isStreaming: true }]);

flushWorklet();
expect(states.at(-1)).toEqual({
processedMarkdown: '**a!',
isStreaming: false,
});

states.length = 0;
act(() => {
renderer.update(<Probe markdown="**ab" />);
});
expect(states).toEqual([{ processedMarkdown: '**a!', isStreaming: true }]);

flushWorklet();
expect(states.at(-1)).toEqual({
processedMarkdown: '**ab!',
isStreaming: false,
});

states.length = 0;
act(() => {
renderer.update(<Probe markdown="" />);
});
expect(states).toEqual([{ processedMarkdown: '', isStreaming: false }]);

processRemendInWorklet.mockImplementation(runSynchronously);
});

it('keeps showing newer results when they arrive after the next change', () => {
const pending: Array<() => void> = [];
const runSynchronously = processRemendInWorklet.getMockImplementation();
processRemendInWorklet.mockImplementation(
(markdown: string, onComplete: (r: string) => void) => {
pending.push(() => onComplete(`${markdown}!`));
}
);

let latest!: { processedMarkdown: string; isStreaming: boolean };
const Probe = ({ markdown }: { markdown: string }) => {
latest = useStreamdownMarkdown(markdown);
return null;
};

let renderer!: TestRenderer.ReactTestRenderer;
act(() => {
renderer = TestRenderer.create(<Probe markdown="a" />);
});
act(() => {
renderer.update(<Probe markdown="ab" />);
});

// The result for "a" lands after "ab" arrived: still shown, still streaming.
act(() => {
pending.shift()?.();
});
expect(latest).toEqual({ processedMarkdown: 'a!', isStreaming: true });

act(() => {
pending.shift()?.();
});
expect(latest).toEqual({ processedMarkdown: 'ab!', isStreaming: false });

// A result older than the one shown is ignored.
act(() => {
renderer.update(<Probe markdown="abc" />);
});
act(() => {
renderer.update(<Probe markdown="abcd" />);
});
const [forAbc, forAbcd] = pending.splice(0);
act(() => {
forAbcd?.();
});
act(() => {
forAbc?.();
});
expect(latest).toEqual({ processedMarkdown: 'abcd!', isStreaming: false });

// Results from before the markdown was cleared are dropped.
act(() => {
renderer.update(<Probe markdown="x" />);
});
act(() => {
renderer.update(<Probe markdown="" />);
});
act(() => {
renderer.update(<Probe markdown="y" />);
});
const [forX, forY] = pending.splice(0);
act(() => {
forX?.();
});
expect(latest).toEqual({ processedMarkdown: 'abcd!', isStreaming: true });
act(() => {
forY?.();
});
expect(latest).toEqual({ processedMarkdown: 'y!', isStreaming: false });

processRemendInWorklet.mockImplementation(runSynchronously);
});
});
49 changes: 37 additions & 12 deletions src/hooks/useStreamdownMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,52 @@ interface UseStreamdownMarkdownResult {
isStreaming: boolean;
}

interface RemendResult {
/** The markdown this result was computed from. */
source: string;
processed: string;
}

const EMPTY_RESULT: RemendResult = { source: '', processed: '' };

export function useStreamdownMarkdown(
markdown: string,
options?: UseStreamdownMarkdownOptions
): UseStreamdownMarkdownResult {
const [processedMarkdown, setProcessedMarkdown] = useState('');
const [isStreaming, setIsStreaming] = useState(false);
// State is only set from the worklet callback, never from the effect body.
// A setState in the effect queues a second render for every markdown change;
// when the markdown comes from a synchronous store (e.g. `useSyncExternalStore`
// in AI SDK `useChat`) and the JS thread is busy, those follow-up updates
// count as nested updates and React throws "Maximum update depth exceeded"
// after ~50 chunks.
const [result, setResult] = useState<RemendResult>(EMPTY_RESULT);
const versionRef = useRef(0);
const appliedVersionRef = useRef(0);

// Gate the effect on the config's content, not its identity: an inline
// remendConfig is a fresh object every render and would otherwise reschedule
// the worklet job on every commit.
const remendConfigKey = JSON.stringify(options?.remendConfig);

useEffect(() => {
const currentVersion = ++versionRef.current;

if (markdown === '') {
setProcessedMarkdown('');
setIsStreaming(false);
// Results of jobs started before the markdown was cleared are stale.
appliedVersionRef.current = currentVersion;
return;
}

setIsStreaming(true);
const currentVersion = ++versionRef.current;

processRemendInWorklet(
markdown,
(result) => {
if (currentVersion === versionRef.current) {
setProcessedMarkdown(result);
setIsStreaming(false);
(processed) => {
// Apply any result newer than the one shown, not only the latest:
// when a round-trip takes longer than the gap between chunks, a
// latest-only check would drop every result and freeze the text
// until the stream pauses.
if (currentVersion > appliedVersionRef.current) {
appliedVersionRef.current = currentVersion;
setResult({ source: markdown, processed });
}
},
options?.remendConfig
Expand All @@ -48,5 +65,13 @@ export function useStreamdownMarkdown(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [markdown, remendConfigKey]);

return { processedMarkdown, isStreaming };
if (markdown === '') {
return { processedMarkdown: '', isStreaming: false };
}

return {
processedMarkdown: result.processed,
// Still streaming until the latest markdown has been processed.
isStreaming: result.source !== markdown,
};
}