@@ -81,6 +81,7 @@ import { |
81 | 81 | warnAboutDefaultPropsOnFunctionComponents, |
82 | 82 | enableScopeAPI, |
83 | 83 | enableCache, |
| 84 | + enableLazyContextPropagation, |
84 | 85 | } from 'shared/ReactFeatureFlags'; |
85 | 86 | import invariant from 'shared/invariant'; |
86 | 87 | import shallowEqual from 'shared/shallowEqual'; |
@@ -154,6 +155,9 @@ import {findFirstSuspended} from './ReactFiberSuspenseComponent.new'; |
154 | 155 | import { |
155 | 156 | pushProvider, |
156 | 157 | propagateContextChange, |
| 158 | + lazilyPropagateParentContextChanges, |
| 159 | + propagateParentContextChangesToDeferredTree, |
| 160 | + checkIfContextChanged, |
157 | 161 | readContext, |
158 | 162 | prepareToReadContext, |
159 | 163 | calculateChangedBits, |
@@ -646,6 +650,18 @@ function updateOffscreenComponent( |
646 | 650 | // We're about to bail out, but we need to push this to the stack anyway |
647 | 651 | // to avoid a push/pop misalignment. |
648 | 652 | pushRenderLanes(workInProgress, nextBaseLanes); |
| 653 | + |
| 654 | + if (enableLazyContextPropagation && current !== null) { |
| 655 | + // Since this tree will resume rendering in a separate render, we need |
| 656 | + // to propagate parent contexts now so we don't lose track of which |
| 657 | + // ones changed. |
| 658 | + propagateParentContextChangesToDeferredTree( |
| 659 | + current, |
| 660 | + workInProgress, |
| 661 | + renderLanes, |
| 662 | + ); |
| 663 | + } |
| 664 | + |
649 | 665 | return null; |
650 | 666 | } else { |
651 | 667 | // This is the second render. The surrounding visible content has already |
@@ -2445,6 +2461,19 @@ function updateDehydratedSuspenseComponent( |
2445 | 2461 | renderLanes, |
2446 | 2462 | ); |
2447 | 2463 | } |
| 2464 | + |
| 2465 | + if ( |
| 2466 | + enableLazyContextPropagation && |
| 2467 | + // TODO: Factoring is a little weird, since we check this right below, too. |
| 2468 | + // But don't want to re-arrange the if-else chain until/unless this |
| 2469 | + // feature lands. |
| 2470 | + !didReceiveUpdate |
| 2471 | + ) { |
| 2472 | + // We need to check if any children have context before we decide to bail |
| 2473 | + // out, so propagate the changes now. |
| 2474 | + lazilyPropagateParentContextChanges(current, workInProgress, renderLanes); |
| 2475 | + } |
| 2476 | + |
2448 | 2477 | // We use lanes to indicate that a child might depend on context, so if |
2449 | 2478 | // any context has changed, we need to treat is as if the input might have changed. |
2450 | 2479 | const hasContextChanged = includesSomeLane(renderLanes, current.childLanes); |
@@ -2970,25 +2999,37 @@ function updateContextProvider( |
2970 | 2999 |
|
2971 | 3000 | pushProvider(workInProgress, context, newValue); |
2972 | 3001 |
|
2973 | | - if (oldProps !== null) { |
2974 | | - const oldValue = oldProps.value; |
2975 | | - const changedBits = calculateChangedBits(context, newValue, oldValue); |
2976 | | - if (changedBits === 0) { |
2977 | | - // No change. Bailout early if children are the same. |
2978 | | - if ( |
2979 | | - oldProps.children === newProps.children && |
2980 | | - !hasLegacyContextChanged() |
2981 | | - ) { |
2982 | | - return bailoutOnAlreadyFinishedWork( |
2983 | | - current, |
| 3002 | + if (enableLazyContextPropagation) { |
| 3003 | + // In the lazy propagation implementation, we don't scan for matching |
| 3004 | + // consumers until something bails out, because until something bails out |
| 3005 | + // we're going to visit those nodes, anyway. The trade-off is that it shifts |
| 3006 | + // responsibility to the consumer to track whether something has changed. |
| 3007 | + } else { |
| 3008 | + if (oldProps !== null) { |
| 3009 | + const oldValue = oldProps.value; |
| 3010 | + const changedBits = calculateChangedBits(context, newValue, oldValue); |
| 3011 | + if (changedBits === 0) { |
| 3012 | + // No change. Bailout early if children are the same. |
| 3013 | + if ( |
| 3014 | + oldProps.children === newProps.children && |
| 3015 | + !hasLegacyContextChanged() |
| 3016 | + ) { |
| 3017 | + return bailoutOnAlreadyFinishedWork( |
| 3018 | + current, |
| 3019 | + workInProgress, |
| 3020 | + renderLanes, |
| 3021 | + ); |
| 3022 | + } |
| 3023 | + } else { |
| 3024 | + // The context value changed. Search for matching consumers and schedule |
| 3025 | + // them to update. |
| 3026 | + propagateContextChange( |
2984 | 3027 | workInProgress, |
| 3028 | + context, |
| 3029 | + changedBits, |
2985 | 3030 | renderLanes, |
2986 | 3031 | ); |
2987 | 3032 | } |
2988 | | - } else { |
2989 | | - // The context value changed. Search for matching consumers and schedule |
2990 | | - // them to update. |
2991 | | - propagateContextChange(workInProgress, context, changedBits, renderLanes); |
2992 | 3033 | } |
2993 | 3034 | } |
2994 | 3035 |
|
@@ -3100,13 +3141,23 @@ function bailoutOnAlreadyFinishedWork( |
3100 | 3141 | // The children don't have any work either. We can skip them. |
3101 | 3142 | // TODO: Once we add back resuming, we should check if the children are |
3102 | 3143 | // a work-in-progress set. If so, we need to transfer their effects. |
3103 | | - return null; |
3104 | | - } else { |
3105 | | - // This fiber doesn't have work, but its subtree does. Clone the child |
3106 | | - // fibers and continue. |
3107 | | - cloneChildFibers(current, workInProgress); |
3108 | | - return workInProgress.child; |
| 3144 | + |
| 3145 | + if (enableLazyContextPropagation && current !== null) { |
| 3146 | + // Before bailing out, check if there are any context changes in |
| 3147 | + // the children. |
| 3148 | + lazilyPropagateParentContextChanges(current, workInProgress, renderLanes); |
| 3149 | + if (!includesSomeLane(renderLanes, workInProgress.childLanes)) { |
| 3150 | + return null; |
| 3151 | + } |
| 3152 | + } else { |
| 3153 | + return null; |
| 3154 | + } |
3109 | 3155 | } |
| 3156 | + |
| 3157 | + // This fiber doesn't have work, but its subtree does. Clone the child |
| 3158 | + // fibers and continue. |
| 3159 | + cloneChildFibers(current, workInProgress); |
| 3160 | + return workInProgress.child; |
3110 | 3161 | } |
3111 | 3162 |
|
3112 | 3163 | function remountFiber( |
@@ -3175,7 +3226,7 @@ function beginWork( |
3175 | 3226 | workInProgress: Fiber, |
3176 | 3227 | renderLanes: Lanes, |
3177 | 3228 | ): Fiber | null { |
3178 | | - const updateLanes = workInProgress.lanes; |
| 3229 | + let updateLanes = workInProgress.lanes; |
3179 | 3230 |
|
3180 | 3231 | if (__DEV__) { |
3181 | 3232 | if (workInProgress._debugNeedsRemount && current !== null) { |
@@ -3196,6 +3247,17 @@ function beginWork( |
3196 | 3247 | } |
3197 | 3248 |
|
3198 | 3249 | if (current !== null) { |
| 3250 | + // TODO: The factoring of this block is weird. |
| 3251 | + if ( |
| 3252 | + enableLazyContextPropagation && |
| 3253 | + !includesSomeLane(renderLanes, updateLanes) |
| 3254 | + ) { |
| 3255 | + const dependencies = current.dependencies; |
| 3256 | + if (dependencies !== null && checkIfContextChanged(dependencies)) { |
| 3257 | + updateLanes = mergeLanes(updateLanes, renderLanes); |
| 3258 | + } |
| 3259 | + } |
| 3260 | + |
3199 | 3261 | const oldProps = current.memoizedProps; |
3200 | 3262 | const newProps = workInProgress.pendingProps; |
3201 | 3263 |
|
@@ -3316,6 +3378,9 @@ function beginWork( |
3316 | 3378 | // primary children and work on the fallback. |
3317 | 3379 | return child.sibling; |
3318 | 3380 | } else { |
| 3381 | + // Note: We can return `null` here because we already checked |
| 3382 | + // whether there were nested context consumers, via the call to |
| 3383 | + // `bailoutOnAlreadyFinishedWork` above. |
3319 | 3384 | return null; |
3320 | 3385 | } |
3321 | 3386 | } |
@@ -3330,11 +3395,30 @@ function beginWork( |
3330 | 3395 | case SuspenseListComponent: { |
3331 | 3396 | const didSuspendBefore = (current.flags & DidCapture) !== NoFlags; |
3332 | 3397 |
|
3333 | | - const hasChildWork = includesSomeLane( |
| 3398 | + let hasChildWork = includesSomeLane( |
3334 | 3399 | renderLanes, |
3335 | 3400 | workInProgress.childLanes, |
3336 | 3401 | ); |
3337 | 3402 |
|
| 3403 | + if (enableLazyContextPropagation && !hasChildWork) { |
| 3404 | + // Context changes may not have been propagated yet. We need to do |
| 3405 | + // that now, before we can decide whether to bail out. |
| 3406 | + // TODO: We use `childLanes` as a heuristic for whether there is |
| 3407 | + // remaining work in a few places, including |
| 3408 | + // `bailoutOnAlreadyFinishedWork` and |
| 3409 | + // `updateDehydratedSuspenseComponent`. We should maybe extract this |
| 3410 | + // into a dedicated function. |
| 3411 | + lazilyPropagateParentContextChanges( |
| 3412 | + current, |
| 3413 | + workInProgress, |
| 3414 | + renderLanes, |
| 3415 | + ); |
| 3416 | + hasChildWork = includesSomeLane( |
| 3417 | + renderLanes, |
| 3418 | + workInProgress.childLanes, |
| 3419 | + ); |
| 3420 | + } |
| 3421 | + |
3338 | 3422 | if (didSuspendBefore) { |
3339 | 3423 | if (hasChildWork) { |
3340 | 3424 | // If something was in fallback state last time, and we have all the |
|