-
Notifications
You must be signed in to change notification settings - Fork 46k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ids are base 32 strings whose binary representation corresponds to the position of a node in a tree. Every time the tree forks into multiple children, we add additional bits to the left of the sequence that represent the position of the child within the current level of children. 00101 00010001011010101 ╰─┬─╯ ╰───────┬───────╯ Fork 5 of 20 Parent id The leading 0s are important. In the above example, you only need 3 bits to represent slot 5. However, you need 5 bits to represent all the forks at the current level, so we must account for the empty bits at the end. We do this by tracking the length of the sequence, since you can't represent this using a single integer. For this same reason, slots are 1-indexed instead of 0-indexed. Otherwise, the zeroth id at a level would be indistinguishable from its parent. If a node has only one child, and does not materialize an id (i.e. does not contain a useId hook), then we don't need to allocate any space in the sequence. It's treated as a transparent indirection. For example, these two trees produce the same ids: <> <> <Indirection> <A /> <A /> <B /> </Indirection> </> <B /> </> However, we cannot skip any materializes an id. Otherwise, a parent id that does not fork would be indistinguishable from its child id. For example, this tree does not fork, but the parent and child must have different ids. <Parent> <Child /> </Parent> To handle this scenario, every time we materialize an id, we allocate a new level with a single slot. You can think of this as a fork with only one prong, or an array of children with length 1. It's possible for the the size of the sequence to exceed 32 bits, the max size for bitwise operations. When this happens, we make more room by converting the right part of the id to a string and storing it in an overflow variable. We use a base 32 string representation, because 32 is the largest power of 2 that is supported by toString(). We want the base to be large so that the resulting ids are compact, and we want the base to be a power of 2 because every log2(base) bits corresponds to a single character, i.e. every log2(32) = 5 bits. That means we can lop bits off the end 5 at a time without affecting the final result.
- Loading branch information
Showing
20 changed files
with
1,319 additions
and
81 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,285 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
let JSDOM; | ||
let React; | ||
let ReactDOM; | ||
let clientAct; | ||
let ReactDOMFizzServer; | ||
let Stream; | ||
let useId; | ||
let document; | ||
let writable; | ||
let container; | ||
let buffer = ''; | ||
let hasErrored = false; | ||
let fatalError = undefined; | ||
|
||
describe('useId', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
JSDOM = require('jsdom').JSDOM; | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
clientAct = require('jest-react').act; | ||
ReactDOMFizzServer = require('react-dom/server'); | ||
Stream = require('stream'); | ||
useId = React.unstable_useId; | ||
|
||
// Test Environment | ||
const jsdom = new JSDOM( | ||
'<!DOCTYPE html><html><head></head><body><div id="container">', | ||
{ | ||
runScripts: 'dangerously', | ||
}, | ||
); | ||
document = jsdom.window.document; | ||
container = document.getElementById('container'); | ||
|
||
buffer = ''; | ||
hasErrored = false; | ||
|
||
writable = new Stream.PassThrough(); | ||
writable.setEncoding('utf8'); | ||
writable.on('data', chunk => { | ||
buffer += chunk; | ||
}); | ||
writable.on('error', error => { | ||
hasErrored = true; | ||
fatalError = error; | ||
}); | ||
}); | ||
|
||
async function serverAct(callback) { | ||
await callback(); | ||
// Await one turn around the event loop. | ||
// This assumes that we'll flush everything we have so far. | ||
await new Promise(resolve => { | ||
setImmediate(resolve); | ||
}); | ||
if (hasErrored) { | ||
throw fatalError; | ||
} | ||
// JSDOM doesn't support stream HTML parser so we need to give it a proper fragment. | ||
// We also want to execute any scripts that are embedded. | ||
// We assume that we have now received a proper fragment of HTML. | ||
const bufferedContent = buffer; | ||
buffer = ''; | ||
const fakeBody = document.createElement('body'); | ||
fakeBody.innerHTML = bufferedContent; | ||
while (fakeBody.firstChild) { | ||
const node = fakeBody.firstChild; | ||
if (node.nodeName === 'SCRIPT') { | ||
const script = document.createElement('script'); | ||
script.textContent = node.textContent; | ||
fakeBody.removeChild(node); | ||
container.appendChild(script); | ||
} else { | ||
container.appendChild(node); | ||
} | ||
} | ||
} | ||
|
||
function normalizeTreeIdForTesting(id) { | ||
const [serverClientPrefix, base32, hookIndex] = id.split(':'); | ||
if (serverClientPrefix === 'r') { | ||
// Client ids aren't stable. For testing purposes, strip out the counter. | ||
return ( | ||
'CLIENT_GENERATED_ID' + | ||
(hookIndex !== undefined ? ` (${hookIndex})` : '') | ||
); | ||
} | ||
// Formats the tree id as a binary sequence, so it's easier to visualize | ||
// the structure. | ||
return ( | ||
parseInt(base32, 32).toString(2) + | ||
(hookIndex !== undefined ? ` (${hookIndex})` : '') | ||
); | ||
} | ||
|
||
function DivWithId({children}) { | ||
const id = normalizeTreeIdForTesting(useId()); | ||
return <div id={id}>{children}</div>; | ||
} | ||
|
||
test('basic example', async () => { | ||
function App() { | ||
return ( | ||
<div> | ||
<div> | ||
<DivWithId /> | ||
<DivWithId /> | ||
</div> | ||
<DivWithId /> | ||
</div> | ||
); | ||
} | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div | ||
id="container" | ||
> | ||
<div> | ||
<div> | ||
<div | ||
id="101" | ||
/> | ||
<div | ||
id="1001" | ||
/> | ||
</div> | ||
<div | ||
id="10" | ||
/> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
test('indirections', async () => { | ||
function App() { | ||
// There are no forks in this tree, but the parent and the child should | ||
// have different ids. | ||
return ( | ||
<DivWithId> | ||
<div> | ||
<div> | ||
<div> | ||
<DivWithId /> | ||
</div> | ||
</div> | ||
</div> | ||
</DivWithId> | ||
); | ||
} | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div | ||
id="container" | ||
> | ||
<div | ||
id="0" | ||
> | ||
<div> | ||
<div> | ||
<div> | ||
<div | ||
id="1" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
test('empty (null) children', async () => { | ||
// We don't treat empty children different from non-empty ones, which means | ||
// they get allocated a slot when generating ids. There's no inherent reason | ||
// to do this; Fiber happens to allocate a fiber for null children that | ||
// appear in a list, which is not ideal for performance. For the purposes | ||
// of id generation, though, what matters is that Fizz and Fiber | ||
// are consistent. | ||
function App() { | ||
return ( | ||
<> | ||
{null} | ||
<DivWithId /> | ||
{null} | ||
<DivWithId /> | ||
</> | ||
); | ||
} | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div | ||
id="container" | ||
> | ||
<div | ||
id="10" | ||
/> | ||
<div | ||
id="100" | ||
/> | ||
</div> | ||
`); | ||
}); | ||
|
||
test('large ids', async () => { | ||
// The component in this test outputs a recursive tree of nodes with ids, | ||
// where the underlying binary representation is an alternating series of 1s | ||
// and 0s. In other words, they are all of the form 101010101. | ||
// | ||
// Because we use base 32 encoding, the resulting id should consist of | ||
// alternating 'a' (01010) and 'l' (10101) characters, except for the the | ||
// 'R:' prefix, and the first character after that, which may not correspond | ||
// to a complete set of 5 bits. | ||
// | ||
// Example: R:clalalalalalalala... | ||
// | ||
// We can use this pattern to test large ids that exceed the bitwise | ||
// safe range (32 bits). The algorithm should theoretically support ids | ||
// of any size. | ||
|
||
function Child({children}) { | ||
const id = useId(); | ||
return <div id={id}>{children}</div>; | ||
} | ||
|
||
function App() { | ||
let tree = <Child />; | ||
for (let i = 0; i < 50; i++) { | ||
tree = ( | ||
<> | ||
<Child /> | ||
{tree} | ||
</> | ||
); | ||
} | ||
return tree; | ||
} | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
const divs = container.querySelectorAll('div'); | ||
|
||
// Confirm that every id matches the expected pattern | ||
for (let i = 0; i < divs.length; i++) { | ||
// Example: R:clalalalalalalala... | ||
expect(divs[i].id).toMatch(/^R:.(((al)*a?)((la)*l?))*$/); | ||
} | ||
}); | ||
}); |
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
Oops, something went wrong.