Skip to content

Commit

Permalink
Prerendering support for useDeferredValue (#27512)
Browse files Browse the repository at this point in the history
### Based on #27509

Revealing a prerendered tree (hidden -> visible) is considered the same
as mounting a brand new tree. So, when an initialValue argument is
passed to useDeferredValue, and it's prerendered inside a hidden tree,
we should first prerender the initial value.

After the initial value has been prerendered, we switch to prerendering
the final one. This is the same sequence that we use when mounting new
visible tree. Depending on how much prerendering work has been finished
by the time the tree is revealed, we may or may not be able to skip all
the way to the final value.

This means we get the benefits of both prerendering and preview states:
if we have enough resources to prerender the whole thing, we do that. If
we don't, we have a preview state to show for immediate feedback.

DiffTrain build for [75c1bd7](75c1bd7)
  • Loading branch information
acdlite committed Oct 17, 2023
1 parent 385a8f1 commit 1050447
Show file tree
Hide file tree
Showing 23 changed files with 769 additions and 697 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2a68a65c84b63ac86930d88ae5c84380cbbdeb6
75c1bd7ee7e4a87a4dd0f560e157c57957ef823b
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-classic-8c719f5e";
var ReactVersion = "18.3.0-www-classic-08b720b1";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,4 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-www-classic-703d13e3";
exports.version = "18.3.0-www-classic-a546ef8a";
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,4 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-www-modern-eba2410d";
exports.version = "18.3.0-www-modern-f25765d2";
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-profiling.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-www-modern-f61d9097";
exports.version = "18.3.0-www-modern-a1b21365";

/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
Expand Down
82 changes: 40 additions & 42 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-classic-8789cf1c";
var ReactVersion = "18.3.0-www-classic-bb3a0070";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -9802,7 +9802,7 @@ function updateDeferredValue(value, initialValue) {
var hook = updateWorkInProgressHook();
var resolvedCurrentHook = currentHook;
var prevValue = resolvedCurrentHook.memoizedState;
return updateDeferredValueImpl(hook, prevValue, value);
return updateDeferredValueImpl(hook, prevValue, value, initialValue);
}

function rerenderDeferredValue(value, initialValue) {
Expand All @@ -9814,7 +9814,7 @@ function rerenderDeferredValue(value, initialValue) {
} else {
// This is a rerender during an update.
var prevValue = currentHook.memoizedState;
return updateDeferredValueImpl(hook, prevValue, value);
return updateDeferredValueImpl(hook, prevValue, value, initialValue);
}
}

Expand All @@ -9835,12 +9835,7 @@ function mountDeferredValueImpl(hook, value, initialValue) {
currentlyRenderingFiber$1.lanes,
deferredLane
);
markSkippedUpdateLanes(deferredLane); // Set this to true to indicate that the rendered value is inconsistent
// from the latest value. The name "baseState" doesn't really match how we
// use it because we're reusing a state hook field instead of creating a
// new one.

hook.baseState = true;
markSkippedUpdateLanes(deferredLane);
return initialValue;
} else {
hook.memoizedState = value;
Expand All @@ -9849,46 +9844,49 @@ function mountDeferredValueImpl(hook, value, initialValue) {
}

function updateDeferredValueImpl(hook, prevValue, value, initialValue) {
// TODO: We should also check if this component is going from
// hidden -> visible. If so, it should use the initialValue arg.
var shouldDeferValue = !includesOnlyNonUrgentLanes(renderLanes);

if (shouldDeferValue) {
// This is an urgent update. If the value has changed, keep using the
// previous value and spawn a deferred render to update it later.
if (!objectIs(value, prevValue)) {
if (objectIs(value, prevValue)) {
// The incoming value is referentially identical to the currently rendered
// value, so we can bail out quickly.
return value;
} else {
// Received a new value that's different from the current value.
// Check if we're inside a hidden tree
if (isCurrentTreeHidden()) {
// Revealing a prerendered tree is considered the same as mounting new
// one, so we reuse the "mount" path in this case.
var resultValue = mountDeferredValueImpl(hook, value, initialValue); // Unlike during an actual mount, we need to mark this as an update if
// the value changed.

if (!objectIs(resultValue, prevValue)) {
markWorkInProgressReceivedUpdate();
}

return resultValue;
}

var shouldDeferValue = !includesOnlyNonUrgentLanes(renderLanes);

if (shouldDeferValue) {
// This is an urgent update. Since the value has changed, keep using the
// previous value and spawn a deferred render to update it later.
// Schedule a deferred render
var deferredLane = requestDeferredLane();
currentlyRenderingFiber$1.lanes = mergeLanes(
currentlyRenderingFiber$1.lanes,
deferredLane
);
markSkippedUpdateLanes(deferredLane); // Set this to true to indicate that the rendered value is inconsistent
// from the latest value. The name "baseState" doesn't really match how we
// use it because we're reusing a state hook field instead of creating a
// new one.
markSkippedUpdateLanes(deferredLane); // Reuse the previous value. We do not need to mark this as an update,
// because we did not render a new value.

hook.baseState = true;
} // Reuse the previous value

return prevValue;
} else {
// This is not an urgent update, so we can use the latest value regardless
// of what it is. No need to defer it.
// However, if we're currently inside a spawned render, then we need to mark
// this as an update to prevent the fiber from bailing out.
//
// `baseState` is true when the current value is different from the rendered
// value. The name doesn't really match how we use it because we're reusing
// a state hook field instead of creating a new one.
if (hook.baseState) {
// Flip this back to false.
hook.baseState = false;
return prevValue;
} else {
// This is not an urgent update, so we can use the latest value regardless
// of what it is. No need to defer it.
// Mark this as an update to prevent the fiber from bailing out.
markWorkInProgressReceivedUpdate();
hook.memoizedState = value;
return value;
}

hook.memoizedState = value;
return value;
}
}

Expand Down Expand Up @@ -10825,7 +10823,7 @@ var InvalidNestedHooksDispatcherOnRerenderInDEV = null;
useDeferredValue: function (value, initialValue) {
currentHookNameInDev = "useDeferredValue";
updateHookTypesDev();
return updateDeferredValue(value);
return updateDeferredValue(value, initialValue);
},
useTransition: function () {
currentHookNameInDev = "useTransition";
Expand Down Expand Up @@ -11281,7 +11279,7 @@ var InvalidNestedHooksDispatcherOnRerenderInDEV = null;
currentHookNameInDev = "useDeferredValue";
warnInvalidHookAccess();
updateHookTypesDev();
return updateDeferredValue(value);
return updateDeferredValue(value, initialValue);
},
useTransition: function () {
currentHookNameInDev = "useTransition";
Expand Down
82 changes: 40 additions & 42 deletions compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-modern-bbf5dd10";
var ReactVersion = "18.3.0-www-modern-f972e958";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -9558,7 +9558,7 @@ function updateDeferredValue(value, initialValue) {
var hook = updateWorkInProgressHook();
var resolvedCurrentHook = currentHook;
var prevValue = resolvedCurrentHook.memoizedState;
return updateDeferredValueImpl(hook, prevValue, value);
return updateDeferredValueImpl(hook, prevValue, value, initialValue);
}

function rerenderDeferredValue(value, initialValue) {
Expand All @@ -9570,7 +9570,7 @@ function rerenderDeferredValue(value, initialValue) {
} else {
// This is a rerender during an update.
var prevValue = currentHook.memoizedState;
return updateDeferredValueImpl(hook, prevValue, value);
return updateDeferredValueImpl(hook, prevValue, value, initialValue);
}
}

Expand All @@ -9591,12 +9591,7 @@ function mountDeferredValueImpl(hook, value, initialValue) {
currentlyRenderingFiber$1.lanes,
deferredLane
);
markSkippedUpdateLanes(deferredLane); // Set this to true to indicate that the rendered value is inconsistent
// from the latest value. The name "baseState" doesn't really match how we
// use it because we're reusing a state hook field instead of creating a
// new one.

hook.baseState = true;
markSkippedUpdateLanes(deferredLane);
return initialValue;
} else {
hook.memoizedState = value;
Expand All @@ -9605,46 +9600,49 @@ function mountDeferredValueImpl(hook, value, initialValue) {
}

function updateDeferredValueImpl(hook, prevValue, value, initialValue) {
// TODO: We should also check if this component is going from
// hidden -> visible. If so, it should use the initialValue arg.
var shouldDeferValue = !includesOnlyNonUrgentLanes(renderLanes);

if (shouldDeferValue) {
// This is an urgent update. If the value has changed, keep using the
// previous value and spawn a deferred render to update it later.
if (!objectIs(value, prevValue)) {
if (objectIs(value, prevValue)) {
// The incoming value is referentially identical to the currently rendered
// value, so we can bail out quickly.
return value;
} else {
// Received a new value that's different from the current value.
// Check if we're inside a hidden tree
if (isCurrentTreeHidden()) {
// Revealing a prerendered tree is considered the same as mounting new
// one, so we reuse the "mount" path in this case.
var resultValue = mountDeferredValueImpl(hook, value, initialValue); // Unlike during an actual mount, we need to mark this as an update if
// the value changed.

if (!objectIs(resultValue, prevValue)) {
markWorkInProgressReceivedUpdate();
}

return resultValue;
}

var shouldDeferValue = !includesOnlyNonUrgentLanes(renderLanes);

if (shouldDeferValue) {
// This is an urgent update. Since the value has changed, keep using the
// previous value and spawn a deferred render to update it later.
// Schedule a deferred render
var deferredLane = requestDeferredLane();
currentlyRenderingFiber$1.lanes = mergeLanes(
currentlyRenderingFiber$1.lanes,
deferredLane
);
markSkippedUpdateLanes(deferredLane); // Set this to true to indicate that the rendered value is inconsistent
// from the latest value. The name "baseState" doesn't really match how we
// use it because we're reusing a state hook field instead of creating a
// new one.
markSkippedUpdateLanes(deferredLane); // Reuse the previous value. We do not need to mark this as an update,
// because we did not render a new value.

hook.baseState = true;
} // Reuse the previous value

return prevValue;
} else {
// This is not an urgent update, so we can use the latest value regardless
// of what it is. No need to defer it.
// However, if we're currently inside a spawned render, then we need to mark
// this as an update to prevent the fiber from bailing out.
//
// `baseState` is true when the current value is different from the rendered
// value. The name doesn't really match how we use it because we're reusing
// a state hook field instead of creating a new one.
if (hook.baseState) {
// Flip this back to false.
hook.baseState = false;
return prevValue;
} else {
// This is not an urgent update, so we can use the latest value regardless
// of what it is. No need to defer it.
// Mark this as an update to prevent the fiber from bailing out.
markWorkInProgressReceivedUpdate();
hook.memoizedState = value;
return value;
}

hook.memoizedState = value;
return value;
}
}

Expand Down Expand Up @@ -10581,7 +10579,7 @@ var InvalidNestedHooksDispatcherOnRerenderInDEV = null;
useDeferredValue: function (value, initialValue) {
currentHookNameInDev = "useDeferredValue";
updateHookTypesDev();
return updateDeferredValue(value);
return updateDeferredValue(value, initialValue);
},
useTransition: function () {
currentHookNameInDev = "useTransition";
Expand Down Expand Up @@ -11037,7 +11035,7 @@ var InvalidNestedHooksDispatcherOnRerenderInDEV = null;
currentHookNameInDev = "useDeferredValue";
warnInvalidHookAccess();
updateHookTypesDev();
return updateDeferredValue(value);
return updateDeferredValue(value, initialValue);
},
useTransition: function () {
currentHookNameInDev = "useTransition";
Expand Down
Loading

0 comments on commit 1050447

Please sign in to comment.