Skip to content

Commit

Permalink
refactor(jsx/streaming): Clarified the type of renderToReadableStream. (
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma authored Sep 23, 2024
1 parent 66df4e1 commit c502aa4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/jsx/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { raw } from '../helper/html'
import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html'
import type { HtmlEscapedString } from '../utils/html'
import { JSXNode } from './base'
import { childrenToString } from './components'
import { DOM_RENDERER, DOM_STASH } from './constants'
import { Suspense as SuspenseDomRenderer } from './dom/components'
Expand Down Expand Up @@ -121,16 +122,19 @@ const textEncoder = new TextEncoder()
* The API might be changed.
*/
export const renderToReadableStream = (
str: HtmlEscapedString | Promise<HtmlEscapedString>,
content: HtmlEscapedString | JSXNode | Promise<HtmlEscapedString>,
onError: (e: unknown) => string | void = console.trace
): ReadableStream<Uint8Array> => {
const reader = new ReadableStream<Uint8Array>({
async start(controller) {
try {
const tmp = str instanceof Promise ? await str : await str.toString()
const context = typeof tmp === 'object' ? tmp : {}
if (content instanceof JSXNode) {
// aJSXNode.toString() returns a string or Promise<string> and string is already escaped
content = content.toString() as HtmlEscapedString | Promise<HtmlEscapedString>
}
const context = typeof content === 'object' ? content : {}
const resolved = await resolveCallback(
tmp,
content,
HtmlEscapedCallbackPhase.BeforeStream,
true,
context
Expand Down

0 comments on commit c502aa4

Please sign in to comment.