diff --git a/src/__tests__/useStreamdownMarkdown.test.tsx b/src/__tests__/useStreamdownMarkdown.test.tsx
index 72c7283..ef0be4c 100644
--- a/src/__tests__/useStreamdownMarkdown.test.tsx
+++ b/src/__tests__/useStreamdownMarkdown.test.tsx
@@ -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();
+ });
+ // 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();
+ });
+ expect(states).toEqual([{ processedMarkdown: '**a!', isStreaming: true }]);
+
+ flushWorklet();
+ expect(states.at(-1)).toEqual({
+ processedMarkdown: '**ab!',
+ isStreaming: false,
+ });
+
+ states.length = 0;
+ act(() => {
+ renderer.update();
+ });
+ 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();
+ });
+ act(() => {
+ renderer.update();
+ });
+
+ // 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();
+ });
+ act(() => {
+ renderer.update();
+ });
+ 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();
+ });
+ act(() => {
+ renderer.update();
+ });
+ act(() => {
+ renderer.update();
+ });
+ 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);
+ });
});
diff --git a/src/hooks/useStreamdownMarkdown.ts b/src/hooks/useStreamdownMarkdown.ts
index a8e7d44..a656620 100644
--- a/src/hooks/useStreamdownMarkdown.ts
+++ b/src/hooks/useStreamdownMarkdown.ts
@@ -11,13 +11,27 @@ 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(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
@@ -25,21 +39,24 @@ export function useStreamdownMarkdown(
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
@@ -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,
+ };
}