diff --git a/src/renderers/noop/ReactNoopEntry.js b/src/renderers/noop/ReactNoopEntry.js index 2b3baee3c7..a71e704ebb 100644 --- a/src/renderers/noop/ReactNoopEntry.js +++ b/src/renderers/noop/ReactNoopEntry.js @@ -430,18 +430,18 @@ var ReactNoop = { if (fiber.updateQueue) { logUpdateQueue(fiber.updateQueue, depth); } - const childInProgress = fiber.progressedChild; - if (childInProgress && childInProgress !== fiber.child) { - log( - ' '.repeat(depth + 1) + 'IN PROGRESS: ' + fiber.progressedPriority, - ); - logFiber(childInProgress, depth + 1); - if (fiber.child) { - log(' '.repeat(depth + 1) + 'CURRENT'); - } - } else if (fiber.child && fiber.updateQueue) { - log(' '.repeat(depth + 1) + 'CHILDREN'); - } + // const childInProgress = fiber.progressedChild; + // if (childInProgress && childInProgress !== fiber.child) { + // log( + // ' '.repeat(depth + 1) + 'IN PROGRESS: ' + fiber.pendingWorkPriority, + // ); + // logFiber(childInProgress, depth + 1); + // if (fiber.child) { + // log(' '.repeat(depth + 1) + 'CURRENT'); + // } + // } else if (fiber.child && fiber.updateQueue) { + // log(' '.repeat(depth + 1) + 'CHILDREN'); + // } if (fiber.child) { logFiber(fiber.child, depth + 1); } diff --git a/src/renderers/shared/fiber/ReactChildFiber.js b/src/renderers/shared/fiber/ReactChildFiber.js index 122757119a..8a82420a70 100644 --- a/src/renderers/shared/fiber/ReactChildFiber.js +++ b/src/renderers/shared/fiber/ReactChildFiber.js @@ -74,7 +74,7 @@ if (__DEV__) { } const { - cloneFiber, + createWorkInProgress, createFiberFromElement, createFiberFromFragment, createFiberFromText, @@ -207,12 +207,16 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) { childToDelete = childToDelete.alternate; } // Deletions are added in reversed order so we add it to the front. - const last = returnFiber.progressedLastDeletion; + // At this point, the return fiber's effect list is empty except for + // deletions, so we can just append the deletion to the list. The remaining + // effects aren't added until the complete phase. Once we implement + // resuming, this may not be true. + const last = returnFiber.lastEffect; if (last !== null) { last.nextEffect = childToDelete; - returnFiber.progressedLastDeletion = childToDelete; + returnFiber.lastEffect = childToDelete; } else { - returnFiber.progressedFirstDeletion = returnFiber.progressedLastDeletion = childToDelete; + returnFiber.firstEffect = returnFiber.lastEffect = childToDelete; } childToDelete.nextEffect = null; childToDelete.effectTag = Deletion; @@ -262,7 +266,7 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) { // We currently set sibling to null and index to 0 here because it is easy // to forget to do before returning it. E.g. for the single child case. if (shouldClone) { - const clone = cloneFiber(fiber, priority); + const clone = createWorkInProgress(fiber, priority); clone.index = 0; clone.sibling = null; return clone; @@ -1445,44 +1449,29 @@ exports.mountChildFibersInPlace = ChildReconciler(false, false); exports.cloneChildFibers = function( current: Fiber | null, workInProgress: Fiber, + renderPriority: PriorityLevel, ): void { - if (!workInProgress.child) { + invariant( + current === null || workInProgress.child === current.child, + 'Resuming work not yet implemented.', + ); + + if (workInProgress.child === null) { return; } - if (current !== null && workInProgress.child === current.child) { - // We use workInProgress.child since that lets Flow know that it can't be - // null since we validated that already. However, as the line above suggests - // they're actually the same thing. - let currentChild = workInProgress.child; - // TODO: This used to reset the pending priority. Not sure if that is needed. - // workInProgress.pendingWorkPriority = current.pendingWorkPriority; - // TODO: The below priority used to be set to NoWork which would've - // dropped work. This is currently unobservable but will become - // observable when the first sibling has lower priority work remaining - // than the next sibling. At that point we should add tests that catches - // this. - let newChild = cloneFiber(currentChild, currentChild.pendingWorkPriority); - workInProgress.child = newChild; + let currentChild = workInProgress.child; + let newChild = createWorkInProgress(currentChild, renderPriority); + workInProgress.child = newChild; + + newChild.return = workInProgress; + while (currentChild.sibling !== null) { + currentChild = currentChild.sibling; + newChild = newChild.sibling = createWorkInProgress( + currentChild, + renderPriority, + ); newChild.return = workInProgress; - while (currentChild.sibling !== null) { - currentChild = currentChild.sibling; - newChild = newChild.sibling = cloneFiber( - currentChild, - currentChild.pendingWorkPriority, - ); - newChild.return = workInProgress; - } - newChild.sibling = null; - } else { - // If there is no alternate, then we don't need to clone the children. - // If the children of the alternate fiber is a different set, then we don't - // need to clone. We need to reset the return fiber though since we'll - // traverse down into them. - let child = workInProgress.child; - while (child !== null) { - child.return = workInProgress; - child = child.sibling; - } } + newChild.sibling = null; }; diff --git a/src/renderers/shared/fiber/ReactFiber.js b/src/renderers/shared/fiber/ReactFiber.js index 34b7482975..bfc4e17fe7 100644 --- a/src/renderers/shared/fiber/ReactFiber.js +++ b/src/renderers/shared/fiber/ReactFiber.js @@ -44,8 +44,6 @@ var {NoContext} = require('ReactTypeOfInternalContext'); var {NoEffect} = require('ReactTypeOfSideEffect'); -var {cloneUpdateQueue} = require('ReactFiberUpdateQueue'); - var invariant = require('fbjs/lib/invariant'); if (__DEV__) { @@ -118,7 +116,6 @@ export type Fiber = {| // Input is the data coming into process this fiber. Arguments. Props. pendingProps: any, // This type will be more specific once we overload the tag. - // TODO: I think that there is a way to merge pendingProps and memoizedProps. memoizedProps: any, // The props used to create the output. // A queue of state updates and callbacks. @@ -150,24 +147,6 @@ export type Fiber = {| // This will be used to quickly determine if a subtree has no pending changes. pendingWorkPriority: PriorityLevel, - // This value represents the priority level that was last used to process this - // component. This indicates whether it is better to continue from the - // progressed work or if it is better to continue from the current state. - progressedPriority: PriorityLevel, - - // If work bails out on a Fiber that already had some work started at a lower - // priority, then we need to store the progressed work somewhere. This holds - // the started child set until we need to get back to working on it. It may - // or may not be the same as the "current" child. - progressedChild: Fiber | null, - - // When we reconcile children onto progressedChild it is possible that we have - // to delete some child fibers. We need to keep track of this side-effects so - // that if we continue later on, we have to include those effects. Deletions - // are added in the reverse order from sibling pointers. - progressedFirstDeletion: Fiber | null, - progressedLastDeletion: Fiber | null, - // This is a pooled version of a Fiber. Every fiber that gets updated will // eventually have a pair. There are cases when we can clean up pairs to save // memory if we need to. @@ -234,10 +213,6 @@ var createFiber = function( lastEffect: null, pendingWorkPriority: NoWork, - progressedPriority: NoWork, - progressedChild: null, - progressedFirstDeletion: null, - progressedLastDeletion: null, alternate: null, }; @@ -260,66 +235,61 @@ function shouldConstruct(Component) { } // This is used to create an alternate fiber to do work on. -// TODO: Rename to createWorkInProgressFiber or something like that. -exports.cloneFiber = function( - fiber: Fiber, - priorityLevel: PriorityLevel, +exports.createWorkInProgress = function( + current: Fiber, + renderPriority: PriorityLevel, ): Fiber { - // We clone to get a work in progress. That means that this fiber is the - // current. To make it safe to reuse that fiber later on as work in progress - // we need to reset its work in progress flag now. We don't have an - // opportunity to do this earlier since we don't traverse the tree when - // the work in progress tree becomes the current tree. - // fiber.progressedPriority = NoWork; - // fiber.progressedChild = null; + let workInProgress = current.alternate; + if (workInProgress === null) { + // We use a double buffering pooling technique because we know that we'll + // only ever need at most two versions of a tree. We pool the "other" unused + // node that we're free to reuse. This is lazily created to avoid allocating + // extra objects for things that are never updated. It also allow us to + // reclaim the extra memory if needed. + workInProgress = createFiber( + current.tag, + current.key, + current.internalContextTag, + ); + workInProgress.type = current.type; + workInProgress.stateNode = current.stateNode; - // We use a double buffering pooling technique because we know that we'll only - // ever need at most two versions of a tree. We pool the "other" unused node - // that we're free to reuse. This is lazily created to avoid allocating extra - // objects for things that are never updated. It also allow us to reclaim the - // extra memory if needed. - let alt = fiber.alternate; - if (alt !== null) { - // If we clone, then we do so from the "current" state. The current state - // can't have any side-effects that are still valid so we reset just to be - // sure. - alt.effectTag = NoEffect; - alt.nextEffect = null; - alt.firstEffect = null; - alt.lastEffect = null; + if (__DEV__) { + // DEV-only fields + workInProgress._debugID = current._debugID; + workInProgress._debugSource = current._debugSource; + workInProgress._debugOwner = current._debugOwner; + } + + workInProgress.alternate = current; + current.alternate = workInProgress; } else { - // This should not have an alternate already - alt = createFiber(fiber.tag, fiber.key, fiber.internalContextTag); - alt.type = fiber.type; + // We already have an alternate. + // Reset the effect tag. + workInProgress.effectTag = NoWork; - alt.progressedChild = fiber.progressedChild; - alt.progressedPriority = fiber.progressedPriority; - - alt.alternate = fiber; - fiber.alternate = alt; + // The effect list is no longer valid. + workInProgress.nextEffect = null; + workInProgress.firstEffect = null; + workInProgress.lastEffect = null; } - alt.stateNode = fiber.stateNode; - alt.child = fiber.child; - alt.sibling = fiber.sibling; // This should always be overridden. TODO: null - alt.index = fiber.index; // This should always be overridden. - alt.ref = fiber.ref; - // pendingProps is here for symmetry but is unnecessary in practice for now. - // TODO: Pass in the new pendingProps as an argument maybe? - alt.pendingProps = fiber.pendingProps; - cloneUpdateQueue(fiber, alt); - alt.pendingWorkPriority = priorityLevel; + workInProgress.pendingWorkPriority = renderPriority; - alt.memoizedProps = fiber.memoizedProps; - alt.memoizedState = fiber.memoizedState; + workInProgress.child = current.child; + workInProgress.memoizedProps = current.memoizedProps; + workInProgress.memoizedState = current.memoizedState; + workInProgress.updateQueue = current.updateQueue; - if (__DEV__) { - alt._debugID = fiber._debugID; - alt._debugSource = fiber._debugSource; - alt._debugOwner = fiber._debugOwner; - } + // pendingProps is set by the parent during reconciliation. + // TODO: Pass this as an argument. - return alt; + // These will be overridden during the parent's reconciliation + workInProgress.sibling = current.sibling; + workInProgress.index = current.index; + workInProgress.ref = current.ref; + + return workInProgress; }; exports.createHostRootFiber = function(): Fiber { diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index 23d7898b8a..473168de24 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -49,7 +49,7 @@ var { YieldComponent, Fragment, } = ReactTypeOfWork; -var {NoWork, OffscreenPriority} = require('ReactPriorityLevel'); +var {NoWork} = require('ReactPriorityLevel'); var { PerformedWork, Placement, @@ -76,11 +76,7 @@ module.exports = function( scheduleUpdate: (fiber: Fiber, priorityLevel: PriorityLevel) => void, getPriorityContext: (fiber: Fiber, forceAsync: boolean) => PriorityLevel, ) { - const { - shouldSetTextContent, - useSyncScheduling, - shouldDeprioritizeSubtree, - } = config; + const {shouldSetTextContent} = config; const {pushHostContext, pushHostContainer} = hostContext; @@ -94,7 +90,7 @@ module.exports = function( adoptClassInstance, constructClassInstance, mountClassInstance, - resumeMountClassInstance, + // resumeMountClassInstance, updateClassInstance, } = ReactFiberClassComponent( scheduleUpdate, @@ -103,28 +99,6 @@ module.exports = function( memoizeState, ); - function markChildAsProgressed(current, workInProgress, priorityLevel) { - // We now have clones. Let's store them as the currently progressed work. - workInProgress.progressedChild = workInProgress.child; - workInProgress.progressedPriority = priorityLevel; - if (current !== null) { - // We also store it on the current. When the alternate swaps in we can - // continue from this point. - current.progressedChild = workInProgress.progressedChild; - current.progressedPriority = workInProgress.progressedPriority; - } - } - - function clearDeletions(workInProgress) { - workInProgress.progressedFirstDeletion = workInProgress.progressedLastDeletion = null; - } - - function transferDeletions(workInProgress) { - // Any deletions get added first into the effect list. - workInProgress.firstEffect = workInProgress.progressedFirstDeletion; - workInProgress.lastEffect = workInProgress.progressedLastDeletion; - } - function reconcileChildren(current, workInProgress, nextChildren) { const priorityLevel = workInProgress.pendingWorkPriority; reconcileChildrenAtPriority( @@ -141,9 +115,6 @@ module.exports = function( nextChildren, priorityLevel, ) { - // At this point any memoization is no longer valid since we'll have changed - // the children. - workInProgress.memoizedProps = null; if (current === null) { // If this is a fresh new component that hasn't been rendered yet, we // won't update its child set by applying minimal side-effects. Instead, @@ -162,16 +133,12 @@ module.exports = function( // If we had any progressed work already, that is invalid at this point so // let's throw it out. - clearDeletions(workInProgress); - workInProgress.child = reconcileChildFibers( workInProgress, workInProgress.child, nextChildren, priorityLevel, ); - - transferDeletions(workInProgress); } else { // If, on the other hand, it is already using a clone, that means we've // already begun some work on this tree and we can continue where we left @@ -182,10 +149,7 @@ module.exports = function( nextChildren, priorityLevel, ); - - transferDeletions(workInProgress); } - markChildAsProgressed(current, workInProgress, priorityLevel); } function updateFragment(current, workInProgress) { @@ -280,8 +244,9 @@ module.exports = function( mountClassInstance(workInProgress, priorityLevel); shouldUpdate = true; } else { + invariant(false, 'Resuming work not yet implemented.'); // In a resume, we'll already have an instance we can reuse. - shouldUpdate = resumeMountClassInstance(workInProgress, priorityLevel); + // shouldUpdate = resumeMountClassInstance(workInProgress, priorityLevel); } } else { shouldUpdate = updateClassInstance( @@ -357,6 +322,7 @@ module.exports = function( if (updateQueue !== null) { const prevState = workInProgress.memoizedState; const state = beginUpdateQueue( + current, workInProgress, updateQueue, null, @@ -395,7 +361,6 @@ module.exports = function( element, priorityLevel, ); - markChildAsProgressed(current, workInProgress, priorityLevel); } else { // Otherwise reset hydration state in case we aborted and resumed another // root. @@ -433,27 +398,6 @@ module.exports = function( ); } } else if (nextProps === null || memoizedProps === nextProps) { - if ( - !useSyncScheduling && - shouldDeprioritizeSubtree(type, memoizedProps) && - workInProgress.pendingWorkPriority !== OffscreenPriority - ) { - // This subtree still has work, but it should be deprioritized so we need - // to bail out and not do any work yet. - // TODO: It would be better if this tree got its correct priority set - // during scheduleUpdate instead because otherwise we'll start a higher - // priority reconciliation first before we can get down here. However, - // that is a bit tricky since workInProgress and current can have - // different "hidden" settings. - let child = workInProgress.progressedChild; - while (child !== null) { - // To ensure that this subtree gets its priority reset, the children - // need to be reset. - child.pendingWorkPriority = OffscreenPriority; - child = child.sibling; - } - return null; - } return bailoutOnAlreadyFinishedWork(current, workInProgress); } @@ -474,55 +418,9 @@ module.exports = function( markRef(current, workInProgress); - if ( - !useSyncScheduling && - shouldDeprioritizeSubtree(workInProgress.type, nextProps) && - workInProgress.pendingWorkPriority !== OffscreenPriority - ) { - // If this host component is hidden, we can bail out on the children. - // We'll rerender the children later at the lower priority. - - // It is unfortunate that we have to do the reconciliation of these - // children already since that will add them to the tree even though - // they are not actually done yet. If this is a large set it is also - // confusing that this takes time to do right now instead of later. - - if (workInProgress.progressedPriority === OffscreenPriority) { - // If we already made some progress on the offscreen priority before, - // then we should continue from where we left off. - workInProgress.child = workInProgress.progressedChild; - } - - // Reconcile the children and stash them for later work. - reconcileChildrenAtPriority( - current, - workInProgress, - nextChildren, - OffscreenPriority, - ); - memoizeProps(workInProgress, nextProps); - workInProgress.child = current !== null ? current.child : null; - - if (current === null) { - // If this doesn't have a current we won't track it for placement - // effects. However, when we come back around to this we have already - // inserted the parent which means that we'll infact need to make this a - // placement. - // TODO: There has to be a better solution to this problem. - let child = workInProgress.progressedChild; - while (child !== null) { - child.effectTag = Placement; - child = child.sibling; - } - } - - // Abort and don't process children yet. - return null; - } else { - reconcileChildren(current, workInProgress, nextChildren); - memoizeProps(workInProgress, nextProps); - return workInProgress.child; - } + reconcileChildren(current, workInProgress, nextChildren); + memoizeProps(workInProgress, nextProps); + return workInProgress.child; } function updateHostText(current, workInProgress) { @@ -647,10 +545,6 @@ module.exports = function( // The following is a fork of reconcileChildrenAtPriority but using // stateNode to store the child. - - // At this point any memoization is no longer valid since we'll have changed - // the children. - workInProgress.memoizedProps = null; if (current === null) { workInProgress.stateNode = mountChildFibersInPlace( workInProgress, @@ -659,16 +553,12 @@ module.exports = function( priorityLevel, ); } else if (current.child === workInProgress.child) { - clearDeletions(workInProgress); - workInProgress.stateNode = reconcileChildFibers( workInProgress, workInProgress.stateNode, nextChildren, priorityLevel, ); - - transferDeletions(workInProgress); } else { workInProgress.stateNode = reconcileChildFibersInPlace( workInProgress, @@ -676,8 +566,6 @@ module.exports = function( nextChildren, priorityLevel, ); - - transferDeletions(workInProgress); } memoizeProps(workInProgress, nextCoroutine); @@ -721,7 +609,6 @@ module.exports = function( priorityLevel, ); memoizeProps(workInProgress, nextChildren); - markChildAsProgressed(current, workInProgress, priorityLevel); } else { reconcileChildren(current, workInProgress, nextChildren); memoizeProps(workInProgress, nextChildren); @@ -756,7 +643,6 @@ module.exports = function( cancelWorkTimer(workInProgress); } - const priorityLevel = workInProgress.pendingWorkPriority; // TODO: We should ideally be able to bail out early if the children have no // more work to do. However, since we don't have a separation of this // Fiber's priority and its children yet - we don't know without doing lots @@ -771,14 +657,9 @@ module.exports = function( // return null; // } - if (current && workInProgress.child === current.child) { - // If we had any progressed work already, that is invalid at this point so - // let's throw it out. - clearDeletions(workInProgress); - } - - cloneChildFibers(current, workInProgress); - markChildAsProgressed(current, workInProgress, priorityLevel); + // TODO: Pass the priority as an argument + const renderPriority = workInProgress.pendingWorkPriority; + cloneChildFibers(current, workInProgress, renderPriority); return workInProgress.child; } @@ -833,17 +714,6 @@ module.exports = function( ReactDebugCurrentFiber.current = workInProgress; } - // If we don't bail out, we're going be recomputing our children so we need - // to drop our effect list. - workInProgress.firstEffect = null; - workInProgress.lastEffect = null; - - if (workInProgress.progressedPriority === priorityLevel) { - // If we have progressed work on this priority level already, we can - // proceed this that as the child. - workInProgress.child = workInProgress.progressedChild; - } - switch (workInProgress.tag) { case IndeterminateComponent: return mountIndeterminateComponent( diff --git a/src/renderers/shared/fiber/ReactFiberClassComponent.js b/src/renderers/shared/fiber/ReactFiberClassComponent.js index 1d695f7cb2..5da7250431 100644 --- a/src/renderers/shared/fiber/ReactFiberClassComponent.js +++ b/src/renderers/shared/fiber/ReactFiberClassComponent.js @@ -362,6 +362,8 @@ module.exports = function( workInProgress: Fiber, priorityLevel: PriorityLevel, ): void { + const current = workInProgress.alternate; + if (__DEV__) { checkClassInstance(workInProgress); } @@ -398,6 +400,7 @@ module.exports = function( const updateQueue = workInProgress.updateQueue; if (updateQueue !== null) { instance.state = beginUpdateQueue( + current, workInProgress, updateQueue, instance, @@ -414,108 +417,108 @@ module.exports = function( // Called on a preexisting class instance. Returns false if a resumed render // could be reused. - function resumeMountClassInstance( - workInProgress: Fiber, - priorityLevel: PriorityLevel, - ): boolean { - const instance = workInProgress.stateNode; - resetInputPointers(workInProgress, instance); + // function resumeMountClassInstance( + // workInProgress: Fiber, + // priorityLevel: PriorityLevel, + // ): boolean { + // const instance = workInProgress.stateNode; + // resetInputPointers(workInProgress, instance); - let newState = workInProgress.memoizedState; - let newProps = workInProgress.pendingProps; - if (!newProps) { - // If there isn't any new props, then we'll reuse the memoized props. - // This could be from already completed work. - newProps = workInProgress.memoizedProps; - invariant( - newProps != null, - 'There should always be pending or memoized props. This error is ' + - 'likely caused by a bug in React. Please file an issue.', - ); - } - const newUnmaskedContext = getUnmaskedContext(workInProgress); - const newContext = getMaskedContext(workInProgress, newUnmaskedContext); + // let newState = workInProgress.memoizedState; + // let newProps = workInProgress.pendingProps; + // if (!newProps) { + // // If there isn't any new props, then we'll reuse the memoized props. + // // This could be from already completed work. + // newProps = workInProgress.memoizedProps; + // invariant( + // newProps != null, + // 'There should always be pending or memoized props. This error is ' + + // 'likely caused by a bug in React. Please file an issue.', + // ); + // } + // const newUnmaskedContext = getUnmaskedContext(workInProgress); + // const newContext = getMaskedContext(workInProgress, newUnmaskedContext); - const oldContext = instance.context; - const oldProps = workInProgress.memoizedProps; + // const oldContext = instance.context; + // const oldProps = workInProgress.memoizedProps; - if ( - typeof instance.componentWillReceiveProps === 'function' && - (oldProps !== newProps || oldContext !== newContext) - ) { - callComponentWillReceiveProps( - workInProgress, - instance, - newProps, - newContext, - ); - } + // if ( + // typeof instance.componentWillReceiveProps === 'function' && + // (oldProps !== newProps || oldContext !== newContext) + // ) { + // callComponentWillReceiveProps( + // workInProgress, + // instance, + // newProps, + // newContext, + // ); + // } - // Process the update queue before calling shouldComponentUpdate - const updateQueue = workInProgress.updateQueue; - if (updateQueue !== null) { - newState = beginUpdateQueue( - workInProgress, - updateQueue, - instance, - newState, - newProps, - priorityLevel, - ); - } + // // Process the update queue before calling shouldComponentUpdate + // const updateQueue = workInProgress.updateQueue; + // if (updateQueue !== null) { + // newState = beginUpdateQueue( + // workInProgress, + // updateQueue, + // instance, + // newState, + // newProps, + // priorityLevel, + // ); + // } - // TODO: Should we deal with a setState that happened after the last - // componentWillMount and before this componentWillMount? Probably - // unsupported anyway. + // // TODO: Should we deal with a setState that happened after the last + // // componentWillMount and before this componentWillMount? Probably + // // unsupported anyway. - if ( - !checkShouldComponentUpdate( - workInProgress, - workInProgress.memoizedProps, - newProps, - workInProgress.memoizedState, - newState, - newContext, - ) - ) { - // Update the existing instance's state, props, and context pointers even - // though we're bailing out. - instance.props = newProps; - instance.state = newState; - instance.context = newContext; - return false; - } + // if ( + // !checkShouldComponentUpdate( + // workInProgress, + // workInProgress.memoizedProps, + // newProps, + // workInProgress.memoizedState, + // newState, + // newContext, + // ) + // ) { + // // Update the existing instance's state, props, and context pointers even + // // though we're bailing out. + // instance.props = newProps; + // instance.state = newState; + // instance.context = newContext; + // return false; + // } - // Update the input pointers now so that they are correct when we call - // componentWillMount - instance.props = newProps; - instance.state = newState; - instance.context = newContext; + // // Update the input pointers now so that they are correct when we call + // // componentWillMount + // instance.props = newProps; + // instance.state = newState; + // instance.context = newContext; - if (typeof instance.componentWillMount === 'function') { - callComponentWillMount(workInProgress, instance); - // componentWillMount may have called setState. Process the update queue. - const newUpdateQueue = workInProgress.updateQueue; - if (newUpdateQueue !== null) { - newState = beginUpdateQueue( - workInProgress, - newUpdateQueue, - instance, - newState, - newProps, - priorityLevel, - ); - } - } + // if (typeof instance.componentWillMount === 'function') { + // callComponentWillMount(workInProgress, instance); + // // componentWillMount may have called setState. Process the update queue. + // const newUpdateQueue = workInProgress.updateQueue; + // if (newUpdateQueue !== null) { + // newState = beginUpdateQueue( + // workInProgress, + // newUpdateQueue, + // instance, + // newState, + // newProps, + // priorityLevel, + // ); + // } + // } - if (typeof instance.componentDidMount === 'function') { - workInProgress.effectTag |= Update; - } + // if (typeof instance.componentDidMount === 'function') { + // workInProgress.effectTag |= Update; + // } - instance.state = newState; + // instance.state = newState; - return true; - } + // return true; + // } // Invokes the update life-cycles and returns false if it shouldn't rerender. function updateClassInstance( @@ -559,14 +562,14 @@ module.exports = function( } // Compute the next state using the memoized state and the update queue. - const updateQueue = workInProgress.updateQueue; const oldState = workInProgress.memoizedState; // TODO: Previous state can be null. let newState; - if (updateQueue !== null) { + if (workInProgress.updateQueue !== null) { newState = beginUpdateQueue( + current, workInProgress, - updateQueue, + workInProgress.updateQueue, instance, oldState, newProps, @@ -580,7 +583,8 @@ module.exports = function( oldProps === newProps && oldState === newState && !hasContextChanged() && - !(updateQueue !== null && updateQueue.hasForceUpdate) + !(workInProgress.updateQueue !== null && + workInProgress.updateQueue.hasForceUpdate) ) { // If an update was already in progress, we should schedule an Update // effect even though we're bailing out, so that cWU/cDU are called. @@ -648,7 +652,7 @@ module.exports = function( adoptClassInstance, constructClassInstance, mountClassInstance, - resumeMountClassInstance, + // resumeMountClassInstance, updateClassInstance, }; }; diff --git a/src/renderers/shared/fiber/ReactFiberCompleteWork.js b/src/renderers/shared/fiber/ReactFiberCompleteWork.js index 84406e877c..3aa2614b32 100644 --- a/src/renderers/shared/fiber/ReactFiberCompleteWork.js +++ b/src/renderers/shared/fiber/ReactFiberCompleteWork.js @@ -70,18 +70,6 @@ module.exports = function( popHydrationState, } = hydrationContext; - function markChildAsProgressed(current, workInProgress, priorityLevel) { - // We now have clones. Let's store them as the currently progressed work. - workInProgress.progressedChild = workInProgress.child; - workInProgress.progressedPriority = priorityLevel; - if (current !== null) { - // We also store it on the current. When the alternate swaps in we can - // continue from this point. - current.progressedChild = workInProgress.progressedChild; - current.progressedPriority = workInProgress.progressedPriority; - } - } - function markUpdate(workInProgress: Fiber) { // Tag the fiber with an update effect. This turns a Placement into // an UpdateAndPlacement. @@ -159,7 +147,6 @@ module.exports = function( nextChildren, priority, ); - markChildAsProgressed(current, workInProgress, priority); return workInProgress.child; } diff --git a/src/renderers/shared/fiber/ReactFiberScheduler.js b/src/renderers/shared/fiber/ReactFiberScheduler.js index 17ef201c95..e8640f619b 100644 --- a/src/renderers/shared/fiber/ReactFiberScheduler.js +++ b/src/renderers/shared/fiber/ReactFiberScheduler.js @@ -48,7 +48,7 @@ var ReactFiberHydrationContext = require('ReactFiberHydrationContext'); var {ReactCurrentOwner} = require('ReactGlobalSharedState'); var getComponentName = require('getComponentName'); -var {cloneFiber} = require('ReactFiber'); +var {createWorkInProgress} = require('ReactFiber'); var {onCommitRoot} = require('ReactFiberDevToolsHook'); var { @@ -81,7 +81,7 @@ var { ClassComponent, } = require('ReactTypeOfWork'); -var {getPendingPriority} = require('ReactFiberUpdateQueue'); +var {getUpdatePriority} = require('ReactFiberUpdateQueue'); var {resetContext} = require('ReactFiberContext'); @@ -292,7 +292,10 @@ module.exports = function( // unfortunately this is it. resetContextStack(); - return cloneFiber(highestPriorityRoot.current, highestPriorityLevel); + return createWorkInProgress( + highestPriorityRoot.current, + highestPriorityLevel, + ); } nextPriorityLevel = NoWork; @@ -549,27 +552,12 @@ module.exports = function( } function resetWorkPriority(workInProgress: Fiber) { - let newPriority = NoWork; - - // Check for pending update priority. This is usually null so it shouldn't - // be a perf issue. - const queue = workInProgress.updateQueue; - const tag = workInProgress.tag; - if ( - queue !== null && - // TODO: Revisit once updateQueue is typed properly to distinguish between - // update payloads for host components and update queues for composites - (tag === ClassComponent || tag === HostRoot) - ) { - newPriority = getPendingPriority(queue); - } + // Check for pending update priority. + let newPriority = getUpdatePriority(workInProgress); // TODO: Coroutines need to visit stateNode - // progressedChild is going to be the child set with the highest priority. - // Either it is the same as child, or it just bailed out because it choose - // not to do the work. - let child = workInProgress.progressedChild; + let child = workInProgress.child; while (child !== null) { // Ensure that remaining work priority bubbles up. if ( diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index 08b19e3137..e58ce420f3 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -23,6 +23,8 @@ const { TaskPriority, } = require('ReactPriorityLevel'); +const {ClassComponent, HostRoot} = require('ReactTypeOfWork'); + const invariant = require('fbjs/lib/invariant'); if (__DEV__) { var warning = require('fbjs/lib/warning'); @@ -85,65 +87,19 @@ function comparePriority(a: PriorityLevel, b: PriorityLevel): number { return a - b; } -// Ensures that a fiber has an update queue, creating a new one if needed. -// Returns the new or existing queue. -function ensureUpdateQueue(fiber: Fiber): UpdateQueue { - if (fiber.updateQueue !== null) { - // We already have an update queue. - return fiber.updateQueue; - } - - let queue; +function createUpdateQueue(): UpdateQueue { + const queue: UpdateQueue = { + first: null, + last: null, + hasForceUpdate: false, + callbackList: null, + }; if (__DEV__) { - queue = { - first: null, - last: null, - hasForceUpdate: false, - callbackList: null, - isProcessing: false, - }; - } else { - queue = { - first: null, - last: null, - hasForceUpdate: false, - callbackList: null, - }; + queue.isProcessing = false; } - - fiber.updateQueue = queue; return queue; } -// Clones an update queue from a source fiber onto its alternate. -function cloneUpdateQueue( - current: Fiber, - workInProgress: Fiber, -): UpdateQueue | null { - const currentQueue = current.updateQueue; - if (currentQueue === null) { - // The source fiber does not have an update queue. - workInProgress.updateQueue = null; - return null; - } - // If the alternate already has a queue, reuse the previous object. - const altQueue = workInProgress.updateQueue !== null - ? workInProgress.updateQueue - : {}; - altQueue.first = currentQueue.first; - altQueue.last = currentQueue.last; - - // These fields are invalid by the time we clone from current. Reset them. - altQueue.hasForceUpdate = false; - altQueue.callbackList = null; - altQueue.isProcessing = false; - - workInProgress.updateQueue = altQueue; - - return altQueue; -} -exports.cloneUpdateQueue = cloneUpdateQueue; - function cloneUpdate(update: Update): Update { return { priorityLevel: update.priorityLevel, @@ -204,6 +160,32 @@ function findInsertionPosition(queue, update): Update | null { return insertAfter; } +function ensureUpdateQueues(fiber: Fiber): [UpdateQueue, UpdateQueue | null] { + const alternateFiber = fiber.alternate; + + let queue1 = fiber.updateQueue; + if (queue1 === null) { + queue1 = fiber.updateQueue = createUpdateQueue(); + } + + let queue2; + if (alternateFiber !== null) { + queue2 = alternateFiber.updateQueue; + if (queue2 === null) { + queue2 = alternateFiber.updateQueue = createUpdateQueue(); + } + } else { + queue2 = null; + } + + // TODO: Refactor to avoid returning a tuple. + return [ + queue1, + // Return null if there is no alternate queue, or if its queue is the same. + queue2 !== queue1 ? queue2 : null, + ]; +} + // The work-in-progress queue is a subset of the current queue (if it exists). // We need to insert the incoming update into both lists. However, it's possible // that the correct position in one list will be different from the position in @@ -234,10 +216,8 @@ function findInsertionPosition(queue, update): Update | null { // // If the update is cloned, it returns the cloned update. function insertUpdate(fiber: Fiber, update: Update): Update | null { - const queue1 = ensureUpdateQueue(fiber); - const queue2 = fiber.alternate !== null - ? ensureUpdateQueue(fiber.alternate) - : null; + // We'll have at least one and at most two distinct update queues. + const [queue1, queue2] = ensureUpdateQueues(fiber); // Warn if an update is scheduled from inside an updater function. if (__DEV__) { @@ -274,13 +254,12 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null { // insertion positions because it mutates the list. insertUpdateIntoQueue(queue1, update, insertAfter1, insertBefore1); - if (insertBefore1 !== insertBefore2) { - // The insertion positions are different, so we need to clone the update and - // insert the clone into the alternate queue. - const update2 = cloneUpdate(update); - insertUpdateIntoQueue(queue2, update2, insertAfter2, insertBefore2); - return update2; - } else { + // See if the insertion positions are equal. Be careful to only compare + // non-null values. + if ( + (insertBefore1 === insertBefore2 && insertBefore1 !== null) || + (insertAfter1 === insertAfter2 && insertAfter1 !== null) + ) { // The insertion positions are the same, so when we inserted into the first // queue, it also inserted into the alternate. All we need to do is update // the alternate queue's `first` and `last` pointers, in case they @@ -291,9 +270,14 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null { if (insertBefore2 === null) { queue2.last = null; } + return null; + } else { + // The insertion positions are different, so we need to clone the update and + // insert the clone into the alternate queue. + const update2 = cloneUpdate(update); + insertUpdateIntoQueue(queue2, update2, insertAfter2, insertBefore2); + return update2; } - - return null; } function addUpdate( @@ -352,10 +336,17 @@ function addForceUpdate( } exports.addForceUpdate = addForceUpdate; -function getPendingPriority(queue: UpdateQueue): PriorityLevel { - return queue.first !== null ? queue.first.priorityLevel : NoWork; +function getUpdatePriority(fiber: Fiber): PriorityLevel { + const updateQueue = fiber.updateQueue; + if (updateQueue === null) { + return NoWork; + } + if (fiber.tag !== ClassComponent && fiber.tag !== HostRoot) { + return NoWork; + } + return updateQueue.first !== null ? updateQueue.first.priorityLevel : NoWork; } -exports.getPendingPriority = getPendingPriority; +exports.getUpdatePriority = getUpdatePriority; function addTopLevelUpdate( fiber: Fiber, @@ -377,13 +368,12 @@ function addTopLevelUpdate( const update2 = insertUpdate(fiber, update); if (isTopLevelUnmount) { + // TODO: Redesign the top-level mount/update/unmount API to avoid this + // special case. + const [queue1, queue2] = ensureUpdateQueues(fiber); + // Drop all updates that are lower-priority, so that the tree is not // remounted. We need to do this for both queues. - const queue1 = fiber.updateQueue; - const queue2 = fiber.alternate !== null - ? fiber.alternate.updateQueue - : null; - if (queue1 !== null && update.next !== null) { update.next = null; queue1.last = update; @@ -407,6 +397,7 @@ function getStateFromUpdate(update, instance, prevState, props) { } function beginUpdateQueue( + current: Fiber | null, workInProgress: Fiber, queue: UpdateQueue, instance: any, @@ -414,19 +405,33 @@ function beginUpdateQueue( props: any, priorityLevel: PriorityLevel, ): any { + if (current !== null && current.updateQueue === queue) { + // We need to create a work-in-progress queue, by cloning the current queue. + const currentQueue = queue; + queue = workInProgress.updateQueue = { + first: currentQueue.first, + last: currentQueue.last, + // These fields are no longer valid because they were already committed. + // Reset them. + callbackList: null, + hasForceUpdate: false, + }; + } + if (__DEV__) { // Set this flag so we can warn if setState is called inside the update // function of another setState. queue.isProcessing = true; } - queue.hasForceUpdate = false; + // Calculate these using the the existing values as a base. + let callbackList = queue.callbackList; + let hasForceUpdate = queue.hasForceUpdate; // Applies updates with matching priority to the previous state to create // a new state object. let state = prevState; let dontMutatePrevState = true; - let callbackList = queue.callbackList; let update = queue.first; while ( update !== null && @@ -456,7 +461,7 @@ function beginUpdateQueue( } } if (update.isForced) { - queue.hasForceUpdate = true; + hasForceUpdate = true; } // Second condition ignores top-level unmount callbacks if they are not the // last update in the queue, since a subsequent update will cause a remount. @@ -464,7 +469,7 @@ function beginUpdateQueue( update.callback !== null && !(update.isTopLevelUnmount && update.next !== null) ) { - callbackList = callbackList || []; + callbackList = callbackList !== null ? callbackList : []; callbackList.push(update.callback); workInProgress.effectTag |= CallbackEffect; } @@ -472,13 +477,15 @@ function beginUpdateQueue( } queue.callbackList = callbackList; + queue.hasForceUpdate = hasForceUpdate; - if (queue.first === null && callbackList === null && !queue.hasForceUpdate) { + if (queue.first === null && callbackList === null && !hasForceUpdate) { // The queue is empty and there are no callbacks. We can reset it. workInProgress.updateQueue = null; } if (__DEV__) { + // No longer processing. queue.isProcessing = false; } @@ -495,6 +502,10 @@ function commitCallbacks( if (callbackList === null) { return; } + + // Set the list to null to make sure they don't get called more than once. + queue.callbackList = null; + for (let i = 0; i < callbackList.length; i++) { const callback = callbackList[i]; invariant(