Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Aug 29, 2022
1 parent f55e59c commit 500767b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
11 changes: 1 addition & 10 deletions packages/react-reconciler/src/ReactFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,7 @@ export function createWorkInProgress(current: Fiber, pendingProps: any): Fiber {
workInProgress.memoizedProps = current.memoizedProps;
workInProgress.memoizedState = current.memoizedState;
workInProgress.updateQueue = current.updateQueue;

const memoCache = current.memoCache;
if (memoCache !== null) {
workInProgress.memoCache = {
data: memoCache.data.map(data => data.slice()),
index: 0,
};
} else {
workInProgress.memoCache = null;
}
workInProgress.memoCache = current.memoCache;

// Clone the dependencies object. This is mutated during the render phase, so
// it cannot be shared with the current fiber.
Expand Down
11 changes: 1 addition & 10 deletions packages/react-reconciler/src/ReactFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,7 @@ export function createWorkInProgress(current: Fiber, pendingProps: any): Fiber {
workInProgress.memoizedProps = current.memoizedProps;
workInProgress.memoizedState = current.memoizedState;
workInProgress.updateQueue = current.updateQueue;

const memoCache = current.memoCache;
if (memoCache !== null) {
workInProgress.memoCache = {
data: memoCache.data.map(data => data.slice()),
index: 0,
};
} else {
workInProgress.memoCache = null;
}
workInProgress.memoCache = current.memoCache;

// Clone the dependencies object. This is mutated during the render phase, so
// it cannot be shared with the current fiber.
Expand Down
16 changes: 10 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,14 @@ export function renderWithHooks<Props, SecondArg>(
current !== null && current.type !== workInProgress.type;
}

// Reset the memoCache index and create a backup copy in case of a setState during render
// or error, either of which can leave the cache in an inconsistent state
const memoCache = workInProgress.memoCache;
let previousMemoCache = null;
if (memoCache !== null) {
// Clone the cache to allow resetting to the most recent version in case of a setstate during render
previousMemoCache = memoCache.data.map(data => data.slice());
previousMemoCache = memoCache.data.map(array => array.slice());
if (workInProgress.alternate != null) {
// Backup to the alternate fiber in case the component throws
workInProgress.alternate.memoCache = {
data: previousMemoCache,
index: 0,
Expand Down Expand Up @@ -477,9 +479,8 @@ export function renderWithHooks<Props, SecondArg>(
workInProgress.updateQueue = null;

if (memoCache !== null) {
// Setting state during render could leave the cache in a partially filled,
// and therefore inconsistent, state. Reset to the previous value to ensure
// the cache is consistent.
// Setting state during render could leave the cache in an inconsistent state,
// reset to the previous state before re-rendering.
if (previousMemoCache !== null) {
memoCache.data = previousMemoCache.map(data => data.slice());
} else {
Expand Down Expand Up @@ -650,10 +651,13 @@ export function resetHooksAfterThrow(erroredWork: Fiber | null): void {
if (erroredWork != null) {
const memoCache = erroredWork.memoCache;
if (memoCache !== null) {
// Unless this is the first render of a component, the alternate will have a
// consistent view of the memo cache that we can restore to
const alternateMemoCache = erroredWork.alternate?.memoCache ?? null;
if (alternateMemoCache !== null) {
memoCache.data = alternateMemoCache.data.map(data => data.slice());
memoCache.data = alternateMemoCache.data.map(array => array.slice());
} else {
// Just in case, fall back to clearing the memo cache
memoCache.data.length = 0;
}
memoCache.index = 0;
Expand Down
16 changes: 10 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,14 @@ export function renderWithHooks<Props, SecondArg>(
current !== null && current.type !== workInProgress.type;
}

// Reset the memoCache index and create a backup copy in case of a setState during render
// or error, either of which can leave the cache in an inconsistent state
const memoCache = workInProgress.memoCache;
let previousMemoCache = null;
if (memoCache !== null) {
// Clone the cache to allow resetting to the most recent version in case of a setstate during render
previousMemoCache = memoCache.data.map(data => data.slice());
previousMemoCache = memoCache.data.map(array => array.slice());
if (workInProgress.alternate != null) {
// Backup to the alternate fiber in case the component throws
workInProgress.alternate.memoCache = {
data: previousMemoCache,
index: 0,
Expand Down Expand Up @@ -477,9 +479,8 @@ export function renderWithHooks<Props, SecondArg>(
workInProgress.updateQueue = null;

if (memoCache !== null) {
// Setting state during render could leave the cache in a partially filled,
// and therefore inconsistent, state. Reset to the previous value to ensure
// the cache is consistent.
// Setting state during render could leave the cache in an inconsistent state,
// reset to the previous state before re-rendering.
if (previousMemoCache !== null) {
memoCache.data = previousMemoCache.map(data => data.slice());
} else {
Expand Down Expand Up @@ -650,10 +651,13 @@ export function resetHooksAfterThrow(erroredWork: Fiber | null): void {
if (erroredWork != null) {
const memoCache = erroredWork.memoCache;
if (memoCache !== null) {
// Unless this is the first render of a component, the alternate will have a
// consistent view of the memo cache that we can restore to
const alternateMemoCache = erroredWork.alternate?.memoCache ?? null;
if (alternateMemoCache !== null) {
memoCache.data = alternateMemoCache.data.map(data => data.slice());
memoCache.data = alternateMemoCache.data.map(array => array.slice());
} else {
// Just in case, fall back to clearing the memo cache
memoCache.data.length = 0;
}
memoCache.index = 0;
Expand Down

0 comments on commit 500767b

Please sign in to comment.