GeezerVison is showing github.com — [Fizz] Add FormatContext and Refactor Work (#21103) · react/react@38a1aed
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMFizzServerBrowser.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,10 @@ import { | 16 | 16 | abort, | 17 | 17 | } from 'react-server/src/ReactFizzServer'; | 18 | 18 |
| 19 | | -import {createResponseState} from './ReactDOMServerFormatConfig'; | | 19 | +import { | | 20 | + createResponseState, | | 21 | + createRootFormatContext, | | 22 | +} from './ReactDOMServerFormatConfig'; | 20 | 23 |
| 21 | 24 | type Options = { | 22 | 25 | identifierPrefix?: string, | @@ -46,6 +49,7 @@ function renderToReadableStream( | 46 | 49 | children, | 47 | 50 | controller, | 48 | 51 | createResponseState(options ? options.identifierPrefix : undefined), | | 52 | + createRootFormatContext(), // We call this here in case we need options to initialize it. | 49 | 53 | options ? options.progressiveChunkSize : undefined, | 50 | 54 | options ? options.onError : undefined, | 51 | 55 | options ? options.onCompleteAll : undefined, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMFizzServerNode.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,10 @@ import { | 17 | 17 | abort, | 18 | 18 | } from 'react-server/src/ReactFizzServer'; | 19 | 19 |
| 20 | | -import {createResponseState} from './ReactDOMServerFormatConfig'; | | 20 | +import { | | 21 | + createResponseState, | | 22 | + createRootFormatContext, | | 23 | +} from './ReactDOMServerFormatConfig'; | 21 | 24 |
| 22 | 25 | function createDrainHandler(destination, request) { | 23 | 26 | return () => startFlowing(request); | @@ -46,6 +49,7 @@ function pipeToNodeWritable( | 46 | 49 | children, | 47 | 50 | destination, | 48 | 51 | createResponseState(options ? options.identifierPrefix : undefined), | | 52 | + createRootFormatContext(), // We call this here in case we need options to initialize it. | 49 | 53 | options ? options.progressiveChunkSize : undefined, | 50 | 54 | options ? options.onError : undefined, | 51 | 55 | options ? options.onCompleteAll : undefined, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMServerFormatConfig.js +51-1Lines changed: 51 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,7 @@ import { | 22 | 22 | import escapeTextForBrowser from './escapeTextForBrowser'; | 23 | 23 | import invariant from 'shared/invariant'; | 24 | 24 |
| 25 | | -// Per response, | | 25 | +// Per response, global state that is not contextual to the rendering subtree. | 26 | 26 | export type ResponseState = { | 27 | 27 | placeholderPrefix: PrecomputedChunk, | 28 | 28 | segmentPrefix: PrecomputedChunk, | @@ -50,6 +50,56 @@ export function createResponseState( | 50 | 50 | }; | 51 | 51 | } | 52 | 52 |
| | 53 | +// Constants for the namespace we use. We don't actually provide the namespace but conditionally | | 54 | +// use different segment parents based on namespace. Therefore we use constants instead of the string. | | 55 | +const ROOT_NAMESPACE = 0; // At the root we don't need to know which namespace it is. We just need to know that it's already the right one. | | 56 | +const HTML_NAMESPACE = 1; | | 57 | +const SVG_NAMESPACE = 2; | | 58 | +const MATHML_NAMESPACE = 3; | | 59 | + | | 60 | +type NamespaceFlag = 0 | 1 | 2 | 3; | | 61 | + | | 62 | +// Lets us keep track of contextual state and pick it back up after suspending. | | 63 | +export type FormatContext = { | | 64 | + namespace: NamespaceFlag, // root/svg/html/mathml | | 65 | + selectedValue: null | string, // the selected value(s) inside a <select>, or null outside <select> | | 66 | +}; | | 67 | + | | 68 | +function createFormatContext( | | 69 | + namespace: NamespaceFlag, | | 70 | + selectedValue: null | string, | | 71 | +): FormatContext { | | 72 | + return { | | 73 | + namespace, | | 74 | + selectedValue, | | 75 | + }; | | 76 | +} | | 77 | + | | 78 | +export function createRootFormatContext(): FormatContext { | | 79 | + return createFormatContext(ROOT_NAMESPACE, null); | | 80 | +} | | 81 | + | | 82 | +export function getChildFormatContext( | | 83 | + parentContext: FormatContext, | | 84 | + type: string, | | 85 | + props: Object, | | 86 | +): FormatContext { | | 87 | + switch (type) { | | 88 | + case 'select': | | 89 | + return createFormatContext( | | 90 | + parentContext.namespace, | | 91 | + props.value != null ? props.value : props.defaultValue, | | 92 | + ); | | 93 | + case 'svg': | | 94 | + return createFormatContext(SVG_NAMESPACE, null); | | 95 | + case 'math': | | 96 | + return createFormatContext(MATHML_NAMESPACE, null); | | 97 | + case 'foreignObject': | | 98 | + return createFormatContext(HTML_NAMESPACE, null); | | 99 | + } | | 100 | + return parentContext; | | 101 | +} | | 102 | + | 53 | 103 | // This object is used to lazily reuse the ID of the first generated node, or assign one. | 54 | 104 | // We can't assign an ID up front because the node we're attaching it to might already | 55 | 105 | // have one. So we need to lazily use that if it's available. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/server/ReactNativeServerFormatConfig.js +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,33 @@ export function createResponseState(): ResponseState { | 66 | 66 | }; | 67 | 67 | } | 68 | 68 |
| | 69 | +// isInAParentText | | 70 | +export type FormatContext = boolean; | | 71 | + | | 72 | +export function createRootFormatContext(): FormatContext { | | 73 | + return false; | | 74 | +} | | 75 | + | | 76 | +export function getChildFormatContext( | | 77 | + parentContext: FormatContext, | | 78 | + type: string, | | 79 | + props: Object, | | 80 | +): FormatContext { | | 81 | + const prevIsInAParentText = parentContext; | | 82 | + const isInAParentText = | | 83 | + type === 'AndroidTextInput' || // Android | | 84 | + type === 'RCTMultilineTextInputView' || // iOS | | 85 | + type === 'RCTSinglelineTextInputView' || // iOS | | 86 | + type === 'RCTText' || | | 87 | + type === 'RCTVirtualText'; | | 88 | + | | 89 | + if (prevIsInAParentText !== isInAParentText) { | | 90 | + return isInAParentText; | | 91 | + } else { | | 92 | + return parentContext; | | 93 | + } | | 94 | +} | | 95 | + | 69 | 96 | // This object is used to lazily reuse the ID of the first generated node, or assign one. | 70 | 97 | // This is very specific to DOM where we can't assign an ID to. | 71 | 98 | export type SuspenseBoundaryID = number; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/ReactNoopServer.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,6 +82,10 @@ const ReactNoopServer = ReactFizzServer({ | 82 | 82 | return {state: 'pending', children: []}; | 83 | 83 | }, | 84 | 84 |
| | 85 | + getChildFormatContext(): null { | | 86 | + return null; | | 87 | + }, | | 88 | + | 85 | 89 | pushTextInstance(target: Array<Uint8Array>, text: string): void { | 86 | 90 | const textInstance: TextInstance = { | 87 | 91 | text, | @@ -236,6 +240,7 @@ function render(children: React$Element<any>, options?: Options): Destination { | 236 | 240 | children, | 237 | 241 | destination, | 238 | 242 | null, | | 243 | + null, | 239 | 244 | options ? options.progressiveChunkSize : undefined, | 240 | 245 | options ? options.onError : undefined, | 241 | 246 | options ? options.onCompleteAll : undefined, | |
|