Skip to content

Commit

Permalink
Move unstable_scheduleHydration to ReactDOMHydrationRoot (#22455)
Browse files Browse the repository at this point in the history
* move unstable_scheduleHydration to ReactDOMHydrationRoot

* move definition of schedule hydration

* fix test?

* prototype

* fix test

* remove gating because unstable_scheduleHydration is no longer gated through index.stable.js because its exposed through ReactDOMHydrationRoot instead of the ReactDOM package

* remove another gating
  • Loading branch information
salazarm authored Nov 15, 2021
1 parent ee8f146 commit 4ff5f57
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 23 deletions.
1 change: 0 additions & 1 deletion packages/react-dom/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ export {
unstable_isNewReconciler,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
unstable_scheduleHydration,
version,
} from './src/client/ReactDOM';
1 change: 0 additions & 1 deletion packages/react-dom/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ export {
unstable_flushControlled,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
unstable_scheduleHydration,
version,
} from './src/client/ReactDOM';
1 change: 0 additions & 1 deletion packages/react-dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ export {
unstable_isNewReconciler,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
unstable_scheduleHydration,
version,
} from './src/client/ReactDOM';
1 change: 0 additions & 1 deletion packages/react-dom/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ export {
unstable_flushControlled,
unstable_isNewReconciler,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
unstable_scheduleHydration,
version,
} from './src/client/ReactDOM';
Original file line number Diff line number Diff line change
Expand Up @@ -1771,10 +1771,9 @@ describe('ReactDOMServerPartialHydration', () => {
const b = container.getElementsByTagName('span')[1];
expect(b.textContent).toBe('B');

const root = ReactDOM.createRoot(container, {hydrate: true});

const root = ReactDOM.hydrateRoot(container, <App />);
// Increase hydration priority to higher than "offscreen".
ReactDOM.unstable_scheduleHydration(b);
root.unstable_scheduleHydration(b);

suspend = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ describe('ReactDOMServerSelectiveHydration', () => {
document.body.removeChild(container);
});

// @gate experimental || www
it('hydrates the last explicitly hydrated target at higher priority', async () => {
function Child({text}) {
Scheduler.unstable_yieldValue(text);
Expand Down Expand Up @@ -920,15 +919,14 @@ describe('ReactDOMServerSelectiveHydration', () => {
const spanB = container.getElementsByTagName('span')[1];
const spanC = container.getElementsByTagName('span')[2];

const root = ReactDOM.createRoot(container, {hydrate: true});
root.render(<App />);
const root = ReactDOM.hydrateRoot(container, <App />);

// Nothing has been hydrated so far.
expect(Scheduler).toHaveYielded([]);

// Increase priority of B and then C.
ReactDOM.unstable_scheduleHydration(spanB);
ReactDOM.unstable_scheduleHydration(spanC);
root.unstable_scheduleHydration(spanB);
root.unstable_scheduleHydration(spanC);

// We should prioritize hydrating C first because the last added
// gets highest priority followed by the next added.
Expand Down
8 changes: 0 additions & 8 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
setAttemptDiscreteHydration,
setAttemptContinuousHydration,
setAttemptHydrationAtCurrentPriority,
queueExplicitHydrationTarget,
setGetCurrentUpdatePriority,
setAttemptHydrationAtPriority,
} from '../events/ReactDOMEventReplaying';
Expand Down Expand Up @@ -116,12 +115,6 @@ function createPortal(
return createPortalImpl(children, container, null, key);
}

function scheduleHydration(target: Node) {
if (target) {
queueExplicitHydrationTarget(target);
}
}

function renderSubtreeIntoContainer(
parentComponent: React$Component<any, any>,
element: React$Element<any>,
Expand Down Expand Up @@ -196,7 +189,6 @@ export {
createRoot,
hydrateRoot,
flushControlled as unstable_flushControlled,
scheduleHydration as unstable_scheduleHydration,
// Disabled behind disableUnstableRenderSubtreeIntoContainer
renderSubtreeIntoContainer as unstable_renderSubtreeIntoContainer,
// enableCreateEventHandleAPI
Expand Down
20 changes: 17 additions & 3 deletions packages/react-dom/src/client/ReactDOMRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {Container} from './ReactDOMHostConfig';
import type {MutableSource, ReactNodeList} from 'shared/ReactTypes';
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';

import {queueExplicitHydrationTarget} from '../events/ReactDOMEventReplaying';

export type RootType = {
render(children: ReactNodeList): void,
unmount(): void,
Expand Down Expand Up @@ -72,7 +74,9 @@ function ReactDOMRoot(internalRoot: FiberRoot) {
this._internalRoot = internalRoot;
}

ReactDOMRoot.prototype.render = function(children: ReactNodeList): void {
ReactDOMHydrationRoot.prototype.render = ReactDOMRoot.prototype.render = function(
children: ReactNodeList,
): void {
const root = this._internalRoot;
if (root === null) {
throw new Error('Cannot update an unmounted root.');
Expand Down Expand Up @@ -104,7 +108,7 @@ ReactDOMRoot.prototype.render = function(children: ReactNodeList): void {
updateContainer(children, root, null, null);
};

ReactDOMRoot.prototype.unmount = function(): void {
ReactDOMHydrationRoot.prototype.unmount = ReactDOMRoot.prototype.unmount = function(): void {
if (__DEV__) {
if (typeof arguments[0] === 'function') {
console.error(
Expand Down Expand Up @@ -189,6 +193,16 @@ export function createRoot(
return new ReactDOMRoot(root);
}

function ReactDOMHydrationRoot(internalRoot: FiberRoot) {
this._internalRoot = internalRoot;
}
function scheduleHydration(target: Node) {
if (target) {
queueExplicitHydrationTarget(target);
}
}
ReactDOMHydrationRoot.prototype.unstable_scheduleHydration = scheduleHydration;

export function hydrateRoot(
container: Container,
initialChildren: ReactNodeList,
Expand Down Expand Up @@ -236,7 +250,7 @@ export function hydrateRoot(
// Render the initial children
updateContainer(initialChildren, root, null, null);

return new ReactDOMRoot(root);
return new ReactDOMHydrationRoot(root);
}

export function isValidContainer(node: any): boolean {
Expand Down

0 comments on commit 4ff5f57

Please sign in to comment.