-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ai/rsc): New
createStreamableUI
implementation (#1825)
Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
- Loading branch information
Showing
24 changed files
with
448 additions
and
1,154 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
fix(ai/rsc): Refactor streamable UI internal implementation |
91 changes: 0 additions & 91 deletions
91
packages/core/rsc/__snapshots__/streamable.ui.test.tsx.snap
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -8,4 +8,5 @@ export { | |
useActions, | ||
useSyncUIState, | ||
InternalAIProvider, | ||
InternalStreamableUIClient, | ||
} from './shared-client'; |
This file contains 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,56 @@ | ||
'use client'; | ||
|
||
import { useState, useEffect } from 'react'; | ||
import { StreamableValue } from '../types'; | ||
import { readStreamableValue } from './streamable'; | ||
|
||
export function InternalStreamableUIClient<T>({ | ||
s, | ||
}: { | ||
s: StreamableValue<T>; | ||
}) { | ||
// Set the value to the initial value of the streamable, if it has one. | ||
const [value, setValue] = useState<T | undefined>(s.curr); | ||
|
||
// Error state for the streamable. It might be errored initially and we want | ||
// to error out as soon as possible. | ||
const [error, setError] = useState<Error | undefined>(s.error); | ||
|
||
useEffect(() => { | ||
let canceled = false; | ||
setError(undefined); | ||
|
||
(async () => { | ||
try { | ||
// Read the streamable value and update the state with the new value. | ||
for await (const v of readStreamableValue(s)) { | ||
if (canceled) { | ||
break; | ||
} | ||
|
||
setValue(v); | ||
} | ||
} catch (e) { | ||
if (canceled) { | ||
return; | ||
} | ||
|
||
setError(e as Error); | ||
} | ||
})(); | ||
|
||
return () => { | ||
// If the component is unmounted, we want to cancel the stream. | ||
canceled = true; | ||
}; | ||
}, [s]); | ||
|
||
// This ensures that errors from the streamable UI are thrown during the | ||
// render phase, so that they can be caught by error boundary components. | ||
// This is necessary for React's declarative model. | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
return value; | ||
} |
This file contains 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 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
157 changes: 0 additions & 157 deletions
157
packages/core/rsc/stream-ui/__snapshots__/stream-ui.ui.test.tsx.snap
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.