Skip to content

Commit

Permalink
Add feature flag for setting update lane priority
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Jul 18, 2020
1 parent b23ea02 commit b3ff611
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 80 deletions.
22 changes: 16 additions & 6 deletions packages/react-dom/src/events/DeprecatedDOMEventResponderSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
executeUserEventHandler,
} from './ReactDOMUpdateBatching';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import {enableDeprecatedFlareAPI} from 'shared/ReactFeatureFlags';
import {
enableDeprecatedFlareAPI,
enableSetUpdateLanePriority,
} from 'shared/ReactFeatureFlags';
import invariant from 'shared/invariant';

import {getClosestInstanceFromNode} from '../client/ReactDOMComponentTree';
Expand Down Expand Up @@ -108,14 +111,21 @@ const eventResponderContext: ReactDOMResponderContext = {
break;
}
case UserBlockingEvent: {
const previousPriority = getCurrentUpdateLanePriority();
try {
setCurrentUpdateLanePriority(InputContinuousLanePriority);
if (enableSetUpdateLanePriority) {
// TODO: Double wrapping is necessary while we decouple Scheduler priority.
const previousPriority = getCurrentUpdateLanePriority();
try {
setCurrentUpdateLanePriority(InputContinuousLanePriority);
runWithPriority(UserBlockingPriority, () =>
executeUserEventHandler(eventListener, eventValue),
);
} finally {
setCurrentUpdateLanePriority(previousPriority);
}
} else {
runWithPriority(UserBlockingPriority, () =>
executeUserEventHandler(eventListener, eventValue),
);
} finally {
setCurrentUpdateLanePriority(previousPriority);
}
break;
}
Expand Down
27 changes: 21 additions & 6 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {getClosestInstanceFromNode} from '../client/ReactDOMComponentTree';
import {
enableDeprecatedFlareAPI,
enableLegacyFBSupport,
enableSetUpdateLanePriority,
} from 'shared/ReactFeatureFlags';
import {
UserBlockingEvent,
Expand Down Expand Up @@ -153,10 +154,26 @@ function dispatchUserBlockingUpdate(
container,
nativeEvent,
) {
// TODO: Double wrapping is necessary while we decouple Scheduler priority.
const previousPriority = getCurrentUpdateLanePriority();
try {
setCurrentUpdateLanePriority(InputContinuousLanePriority);

if (enableSetUpdateLanePriority) {
const previousPriority = getCurrentUpdateLanePriority();
try {
// TODO: Double wrapping is necessary while we decouple Scheduler priority.
setCurrentUpdateLanePriority(InputContinuousLanePriority);
runWithPriority(
UserBlockingPriority,
dispatchEvent.bind(
null,
topLevelType,
eventSystemFlags,
container,
nativeEvent,
),
);
} finally {
setCurrentUpdateLanePriority(previousPriority);
}
} else {
runWithPriority(
UserBlockingPriority,
dispatchEvent.bind(
Expand All @@ -167,8 +184,6 @@ function dispatchUserBlockingUpdate(
nativeEvent,
),
);
} finally {
setCurrentUpdateLanePriority(previousPriority);
}
}

Expand Down
22 changes: 15 additions & 7 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
enableDebugTracing,
enableSchedulingProfiler,
enableNewReconciler,
enableSetUpdateLanePriority,
} from 'shared/ReactFeatureFlags';

import {NoMode, BlockingMode, DebugTracingMode} from './ReactTypeOfMode';
Expand Down Expand Up @@ -1509,19 +1510,24 @@ function rerenderDeferredValue<T>(

function startTransition(setPending, config, callback) {
const priorityLevel = getCurrentPriorityLevel();
const previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(
higherLanePriority(previousLanePriority, InputContinuousLanePriority),
);
let previousLanePriority;
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(
higherLanePriority(previousLanePriority, InputContinuousLanePriority),
);
}
runWithPriority(
priorityLevel < UserBlockingPriority ? UserBlockingPriority : priorityLevel,
() => {
setPending(true);
},
);

// If there's no SuspenseConfig set, we'll use the DefaultLanePriority for this transition.
setCurrentUpdateLanePriority(DefaultLanePriority);
if (enableSetUpdateLanePriority) {
// If there's no SuspenseConfig set, we'll use the DefaultLanePriority for this transition.
setCurrentUpdateLanePriority(DefaultLanePriority);
}

runWithPriority(
priorityLevel > NormalPriority ? NormalPriority : priorityLevel,
Expand All @@ -1532,7 +1538,9 @@ function startTransition(setPending, config, callback) {
setPending(false);
callback();
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
ReactCurrentBatchConfig.suspense = previousConfig;
}
},
Expand Down
22 changes: 15 additions & 7 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
enableDebugTracing,
enableSchedulingProfiler,
enableNewReconciler,
enableSetUpdateLanePriority,
} from 'shared/ReactFeatureFlags';

import {NoMode, BlockingMode, DebugTracingMode} from './ReactTypeOfMode';
Expand Down Expand Up @@ -1509,19 +1510,24 @@ function rerenderDeferredValue<T>(

function startTransition(setPending, config, callback) {
const priorityLevel = getCurrentPriorityLevel();
const previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(
higherLanePriority(previousLanePriority, InputContinuousLanePriority),
);
let previousLanePriority;
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(
higherLanePriority(previousLanePriority, InputContinuousLanePriority),
);
}
runWithPriority(
priorityLevel < UserBlockingPriority ? UserBlockingPriority : priorityLevel,
() => {
setPending(true);
},
);

// If there's no SuspenseConfig set, we'll use the DefaultLanePriority for this transition.
setCurrentUpdateLanePriority(DefaultLanePriority);
if (enableSetUpdateLanePriority) {
// If there's no SuspenseConfig set, we'll use the DefaultLanePriority for this transition.
setCurrentUpdateLanePriority(DefaultLanePriority);
}

runWithPriority(
priorityLevel > NormalPriority ? NormalPriority : priorityLevel,
Expand All @@ -1532,7 +1538,9 @@ function startTransition(setPending, config, callback) {
setPending(false);
callback();
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
ReactCurrentBatchConfig.suspense = previousConfig;
}
},
Expand Down
76 changes: 53 additions & 23 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
enableDebugTracing,
enableSchedulingProfiler,
enableScopeAPI,
enableSetUpdateLanePriority,
} from 'shared/ReactFeatureFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import invariant from 'shared/invariant';
Expand Down Expand Up @@ -477,7 +478,7 @@ export function requestUpdateLane(
schedulerPriority,
);

if (decoupleUpdatePriorityFromScheduler) {
if (enableSetUpdateLanePriority && decoupleUpdatePriorityFromScheduler) {
// In the new strategy, we will track the current update lane priority
// inside React and use that priority to select a lane for this update.
// For now, we're just logging when they're different so we can assess.
Expand Down Expand Up @@ -1142,13 +1143,17 @@ export function flushDiscreteUpdates() {
}

export function deferredUpdates<A>(fn: () => A): A {
// TODO: Remove in favor of Scheduler.next
const previousLanePriority = getCurrentUpdateLanePriority();
let previousLanePriority;
try {
setCurrentUpdateLanePriority(DefaultLanePriority);
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(DefaultLanePriority);
}
return runWithPriority(NormalSchedulerPriority, fn);
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
}
}

Expand Down Expand Up @@ -1204,16 +1209,21 @@ export function discreteUpdates<A, B, C, D, R>(
): R {
const prevExecutionContext = executionContext;
executionContext |= DiscreteEventContext;
const previousLanePriority = getCurrentUpdateLanePriority();
let previousLanePriority;
try {
setCurrentUpdateLanePriority(InputDiscreteLanePriority);
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(InputDiscreteLanePriority);
}
// Should this
return runWithPriority(
UserBlockingSchedulerPriority,
fn.bind(null, a, b, c, d),
);
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
executionContext = prevExecutionContext;
if (executionContext === NoContext) {
// Flush the immediate callbacks that were scheduled during this batch
Expand Down Expand Up @@ -1250,16 +1260,21 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
return fn(a);
}
executionContext |= BatchedContext;
const previousLanePriority = getCurrentUpdateLanePriority();
let previousLanePriority;
try {
setCurrentUpdateLanePriority(SyncLanePriority);
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(SyncLanePriority);
}
if (fn) {
return runWithPriority(ImmediateSchedulerPriority, fn.bind(null, a));
} else {
return (undefined: $FlowFixMe);
}
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
executionContext = prevExecutionContext;
// Flush the immediate callbacks that were scheduled during this batch.
// Note that this will happen even if batchedUpdates is higher up
Expand All @@ -1271,12 +1286,17 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
export function flushControlled(fn: () => mixed): void {
const prevExecutionContext = executionContext;
executionContext |= BatchedContext;
const previousLanePriority = getCurrentUpdateLanePriority();
let previousLanePriority;
try {
setCurrentUpdateLanePriority(SyncLanePriority);
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(SyncLanePriority);
}
runWithPriority(ImmediateSchedulerPriority, fn);
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
executionContext = prevExecutionContext;
if (executionContext === NoContext) {
// Flush the immediate callbacks that were scheduled during this batch
Expand Down Expand Up @@ -2090,8 +2110,11 @@ function commitRootImpl(root, renderPriorityLevel) {
}

if (firstEffect !== null) {
const previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(SyncLanePriority);
let previousLanePriority;
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(SyncLanePriority);
}

const prevExecutionContext = executionContext;
executionContext |= CommitContext;
Expand Down Expand Up @@ -2168,8 +2191,10 @@ function commitRootImpl(root, renderPriorityLevel) {
}
executionContext = prevExecutionContext;

// Reset the priority to the previous non-sync value.
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
// Reset the priority to the previous non-sync value.
setCurrentUpdateLanePriority(previousLanePriority);
}
} else {
// No effects.
root.current = finishedWork;
Expand Down Expand Up @@ -2607,14 +2632,19 @@ export function flushPassiveEffects() {
? NormalSchedulerPriority
: pendingPassiveEffectsRenderPriority;
pendingPassiveEffectsRenderPriority = NoSchedulerPriority;
const previousLanePriority = getCurrentUpdateLanePriority();
let previousLanePriority;
try {
setCurrentUpdateLanePriority(
schedulerPriorityToLanePriority(priorityLevel),
);
if (enableSetUpdateLanePriority) {
previousLanePriority = getCurrentUpdateLanePriority();
setCurrentUpdateLanePriority(
schedulerPriorityToLanePriority(priorityLevel),
);
}
return runWithPriority(priorityLevel, flushPassiveEffectsImpl);
} finally {
setCurrentUpdateLanePriority(previousLanePriority);
if (enableSetUpdateLanePriority && previousLanePriority != null) {
setCurrentUpdateLanePriority(previousLanePriority);
}
}
}
}
Expand Down
Loading

0 comments on commit b3ff611

Please sign in to comment.