GeezerVison is showing github.com — Implement useOpaqueIdentifier (#21260) · react/react@f4d7a0f
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/ReactDOMServerFormatConfig.js +20-2Lines changed: 20 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,8 +57,9 @@ export type ResponseState = { | 57 | 57 | placeholderPrefix: PrecomputedChunk, | 58 | 58 | segmentPrefix: PrecomputedChunk, | 59 | 59 | boundaryPrefix: string, | 60 | | - opaqueIdentifierPrefix: PrecomputedChunk, | | 60 | + opaqueIdentifierPrefix: string, | 61 | 61 | nextSuspenseID: number, | | 62 | + nextOpaqueID: number, | 62 | 63 | sentCompleteSegmentFunction: boolean, | 63 | 64 | sentCompleteBoundaryFunction: boolean, | 64 | 65 | sentClientRenderFunction: boolean, | @@ -72,8 +73,9 @@ export function createResponseState( | 72 | 73 | placeholderPrefix: stringToPrecomputedChunk(identifierPrefix + 'P:'), | 73 | 74 | segmentPrefix: stringToPrecomputedChunk(identifierPrefix + 'S:'), | 74 | 75 | boundaryPrefix: identifierPrefix + 'B:', | 75 | | - opaqueIdentifierPrefix: stringToPrecomputedChunk(identifierPrefix + 'R:'), | | 76 | + opaqueIdentifierPrefix: identifierPrefix + 'R:', | 76 | 77 | nextSuspenseID: 0, | | 78 | + nextOpaqueID: 0, | 77 | 79 | sentCompleteSegmentFunction: false, | 78 | 80 | sentCompleteBoundaryFunction: false, | 79 | 81 | sentClientRenderFunction: false, | @@ -172,6 +174,22 @@ export function createSuspenseBoundaryID( | 172 | 174 | return {formattedID: null}; | 173 | 175 | } | 174 | 176 |
| | 177 | +export type OpaqueIDType = string; | | 178 | + | | 179 | +export function makeServerID( | | 180 | + responseState: null | ResponseState, | | 181 | +): OpaqueIDType { | | 182 | + invariant( | | 183 | + responseState !== null, | | 184 | + 'Invalid hook call. Hooks can only be called inside of the body of a function component.', | | 185 | + ); | | 186 | + // TODO: This is not deterministic since it's created during render. | | 187 | + return ( | | 188 | + responseState.opaqueIdentifierPrefix + | | 189 | + (responseState.nextOpaqueID++).toString(36) | | 190 | + ); | | 191 | +} | | 192 | + | 175 | 193 | function encodeHTMLTextNode(text: string): string { | 176 | 194 | return escapeTextForBrowser(text); | 177 | 195 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/server/ReactNativeServerFormatConfig.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,12 +61,14 @@ SUSPENSE_UPDATE_TO_CLIENT_RENDER[0] = SUSPENSE_UPDATE_TO_CLIENT_RENDER_TAG; | 61 | 61 | // Per response, | 62 | 62 | export type ResponseState = { | 63 | 63 | nextSuspenseID: number, | | 64 | + nextOpaqueID: number, | 64 | 65 | }; | 65 | 66 |
| 66 | 67 | // Allows us to keep track of what we've already written so we can refer back to it. | 67 | 68 | export function createResponseState(): ResponseState { | 68 | 69 | return { | 69 | 70 | nextSuspenseID: 0, | | 71 | + nextOpaqueID: 0, | 70 | 72 | }; | 71 | 73 | } | 72 | 74 |
| @@ -108,6 +110,19 @@ export function createSuspenseBoundaryID( | 108 | 110 | return responseState.nextSuspenseID++; | 109 | 111 | } | 110 | 112 |
| | 113 | +export type OpaqueIDType = number; | | 114 | + | | 115 | +export function makeServerID( | | 116 | + responseState: null | ResponseState, | | 117 | +): OpaqueIDType { | | 118 | + invariant( | | 119 | + responseState !== null, | | 120 | + 'Invalid hook call. Hooks can only be called inside of the body of a function component.', | | 121 | + ); | | 122 | + // TODO: This is not deterministic since it's created during render. | | 123 | + return responseState.nextOpaqueID++; | | 124 | +} | | 125 | + | 111 | 126 | const RAW_TEXT = stringToPrecomputedChunk('RCTRawText'); | 112 | 127 |
| 113 | 128 | export function pushEmpty( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/ReactNoopServer.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,8 @@ type Destination = { | 53 | 53 |
| 54 | 54 | const POP = Buffer.from('/', 'utf8'); | 55 | 55 |
| | 56 | +let opaqueID = 0; | | 57 | + | 56 | 58 | const ReactNoopServer = ReactFizzServer({ | 57 | 59 | scheduleWork(callback: () => void) { | 58 | 60 | callback(); | @@ -84,6 +86,10 @@ const ReactNoopServer = ReactFizzServer({ | 84 | 86 | return {state: 'pending', children: []}; | 85 | 87 | }, | 86 | 88 |
| | 89 | + makeServerID(): number { | | 90 | + return opaqueID++; | | 91 | + }, | | 92 | + | 87 | 93 | getChildFormatContext(): null { | 88 | 94 | return null; | 89 | 95 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFizzHooks.js +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,8 +16,12 @@ import type { | 16 | 16 | ReactContext, | 17 | 17 | } from 'shared/ReactTypes'; | 18 | 18 |
| | 19 | +import type {ResponseState, OpaqueIDType} from './ReactServerFormatConfig'; | | 20 | + | 19 | 21 | import {readContext as readContextImpl} from './ReactFizzNewContext'; | 20 | 22 |
| | 23 | +import {makeServerID} from './ReactServerFormatConfig'; | | 24 | + | 21 | 25 | import invariant from 'shared/invariant'; | 22 | 26 | import {enableCache} from 'shared/ReactFeatureFlags'; | 23 | 27 | import is from 'shared/objectIs'; | @@ -41,8 +45,6 @@ type Hook = {| | 41 | 45 | next: Hook | null, | 42 | 46 | |}; | 43 | 47 |
| 44 | | -type OpaqueIDType = string; | 45 | | - | 46 | 48 | let currentlyRenderingComponent: Object | null = null; | 47 | 49 | let firstWorkInProgressHook: Hook | null = null; | 48 | 50 | let workInProgressHook: Hook | null = null; | @@ -474,7 +476,7 @@ function useTransition(): [(callback: () => void) => void, boolean] { | 474 | 476 | } | 475 | 477 |
| 476 | 478 | function useOpaqueIdentifier(): OpaqueIDType { | 477 | | - throw new Error('Not yet implemented.'); | | 479 | + return makeServerID(currentResponseState); | 478 | 480 | } | 479 | 481 |
| 480 | 482 | function unsupportedRefresh() { | @@ -513,3 +515,10 @@ if (enableCache) { | 513 | 515 | Dispatcher.getCacheForType = getCacheForType; | 514 | 516 | Dispatcher.useCacheRefresh = useCacheRefresh; | 515 | 517 | } | | 518 | + | | 519 | +export let currentResponseState: null | ResponseState = (null: any); | | 520 | +export function setCurrentResponseState( | | 521 | + responseState: null | ResponseState, | | 522 | +): void { | | 523 | + currentResponseState = responseState; | | 524 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFizzServer.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,8 @@ import { | 74 | 74 | finishHooks, | 75 | 75 | resetHooksState, | 76 | 76 | Dispatcher, | | 77 | + currentResponseState, | | 78 | + setCurrentResponseState, | 77 | 79 | } from './ReactFizzHooks'; | 78 | 80 |
| 79 | 81 | import { | @@ -1341,7 +1343,8 @@ function performWork(request: Request): void { | 1341 | 1343 | const prevContext = getActiveContext(); | 1342 | 1344 | const prevDispatcher = ReactCurrentDispatcher.current; | 1343 | 1345 | ReactCurrentDispatcher.current = Dispatcher; | 1344 | | - | | 1346 | + const prevResponseState = currentResponseState; | | 1347 | + setCurrentResponseState(request.responseState); | 1345 | 1348 | try { | 1346 | 1349 | const pingedTasks = request.pingedTasks; | 1347 | 1350 | let i; | @@ -1357,6 +1360,7 @@ function performWork(request: Request): void { | 1357 | 1360 | reportError(request, error); | 1358 | 1361 | fatalError(request, error); | 1359 | 1362 | } finally { | | 1363 | + setCurrentResponseState(prevResponseState); | 1360 | 1364 | ReactCurrentDispatcher.current = prevDispatcher; | 1361 | 1365 | if (prevDispatcher === Dispatcher) { | 1362 | 1366 | // This means that we were in a reentrant work loop. This could happen | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/forks/ReactServerFormatConfig.custom.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,11 +28,13 @@ export opaque type Destination = mixed; // eslint-disable-line no-undef | 28 | 28 | export opaque type ResponseState = mixed; | 29 | 29 | export opaque type FormatContext = mixed; | 30 | 30 | export opaque type SuspenseBoundaryID = mixed; | | 31 | +export opaque type OpaqueIDType = mixed; | 31 | 32 |
| 32 | 33 | export const isPrimaryRenderer = false; | 33 | 34 |
| 34 | 35 | export const getChildFormatContext = $$$hostConfig.getChildFormatContext; | 35 | 36 | export const createSuspenseBoundaryID = $$$hostConfig.createSuspenseBoundaryID; | | 37 | +export const makeServerID = $$$hostConfig.makeServerID; | 36 | 38 | export const pushEmpty = $$$hostConfig.pushEmpty; | 37 | 39 | export const pushTextInstance = $$$hostConfig.pushTextInstance; | 38 | 40 | export const pushStartInstance = $$$hostConfig.pushStartInstance; | |
Collapse file Copy file name to clipboardExpand all lines: scripts/error-codes/codes.json +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 391 | 391 | "400": "menuitems cannot have `children` nor `dangerouslySetInnerHTML`.", | 392 | 392 | "401": "The stacks must reach the root at the same time. This is a bug in React.", | 393 | 393 | "402": "The depth must equal at least at zero before reaching the root. This is a bug in React.", | 394 | | - "403": "Tried to pop a Context at the root of the app. This is a bug in React." | | 394 | + "403": "Tried to pop a Context at the root of the app. This is a bug in React.", | | 395 | + "404": "Invalid hook call. Hooks can only be called inside of the body of a function component." | 395 | 396 | } |
|