Skip to content

Commit

Permalink
change ReactBatchConfig.transition
Browse files Browse the repository at this point in the history
Previously, ReactBatchConfig.transition was an number (1 = there is a transition, 0 = there isn't one). This PR changes this to a transition object (object = there is a transition, null = there isn't one) in preparation for transition tracing changes.
  • Loading branch information
lunaruan authored Feb 17, 2022
1 parent 51c8411 commit e9aa959
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 51 deletions.
4 changes: 2 additions & 2 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function dispatchDiscreteEvent(
) {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
try {
setCurrentUpdatePriority(DiscreteEventPriority);
dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent);
Expand All @@ -135,7 +135,7 @@ function dispatchContinuousEvent(
) {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
try {
setCurrentUpdatePriority(ContinuousEventPriority);
dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent);
Expand Down
23 changes: 15 additions & 8 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ function mountDeferredValue<T>(value: T): T {
const [prevValue, setValue] = mountState(value);
mountEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1942,7 +1942,7 @@ function updateDeferredValue<T>(value: T): T {
const [prevValue, setValue] = updateState(value);
updateEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1956,7 +1956,7 @@ function rerenderDeferredValue<T>(value: T): T {
const [prevValue, setValue] = rerenderState(value);
updateEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1975,28 +1975,35 @@ function startTransition(setPending, callback) {
setPending(true);

const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
const currentTransition = ReactCurrentBatchConfig.transition;

if (__DEV__) {
ReactCurrentBatchConfig.transition._updatedFibers = new Set();
}

try {
setPending(false);
callback();
} finally {
setCurrentUpdatePriority(previousPriority);

ReactCurrentBatchConfig.transition = prevTransition;
if (__DEV__) {
if (
prevTransition !== 1 &&
prevTransition === null &&
warnOnSubscriptionInsideStartTransition &&
ReactCurrentBatchConfig._updatedFibers
currentTransition._updatedFibers
) {
const updatedFibersCount = ReactCurrentBatchConfig._updatedFibers.size;
const updatedFibersCount = currentTransition._updatedFibers.size;
if (updatedFibersCount > 10) {
console.warn(
'Detected a large number of updates inside startTransition. ' +
'If this is due to a subscription please re-write it to use React provided hooks. ' +
'Otherwise concurrent mode guarantees are off the table.',
);
}
ReactCurrentBatchConfig._updatedFibers.clear();
currentTransition._updatedFibers.clear();
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ function mountDeferredValue<T>(value: T): T {
const [prevValue, setValue] = mountState(value);
mountEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1942,7 +1942,7 @@ function updateDeferredValue<T>(value: T): T {
const [prevValue, setValue] = updateState(value);
updateEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1956,7 +1956,7 @@ function rerenderDeferredValue<T>(value: T): T {
const [prevValue, setValue] = rerenderState(value);
updateEffect(() => {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
try {
setValue(value);
} finally {
Expand All @@ -1975,28 +1975,35 @@ function startTransition(setPending, callback) {
setPending(true);

const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 1;
ReactCurrentBatchConfig.transition = {};
const currentTransition = ReactCurrentBatchConfig.transition;

if (__DEV__) {
ReactCurrentBatchConfig.transition._updatedFibers = new Set();
}

try {
setPending(false);
callback();
} finally {
setCurrentUpdatePriority(previousPriority);

ReactCurrentBatchConfig.transition = prevTransition;
if (__DEV__) {
if (
prevTransition !== 1 &&
prevTransition === null &&
warnOnSubscriptionInsideStartTransition &&
ReactCurrentBatchConfig._updatedFibers
currentTransition._updatedFibers
) {
const updatedFibersCount = ReactCurrentBatchConfig._updatedFibers.size;
const updatedFibersCount = currentTransition._updatedFibers.size;
if (updatedFibersCount > 10) {
console.warn(
'Detected a large number of updates inside startTransition. ' +
'If this is due to a subscription please re-write it to use React provided hooks. ' +
'Otherwise concurrent mode guarantees are off the table.',
);
}
ReactCurrentBatchConfig._updatedFibers.clear();
currentTransition._updatedFibers.clear();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';

export type Transition = {
_updatedFibers?: Set<Fiber>,
};
5 changes: 3 additions & 2 deletions packages/react-reconciler/src/ReactFiberTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*/

import ReactSharedInternals from 'shared/ReactSharedInternals';
import type {Transition} from './ReactFiberTracingMarkerComponent.new';

const {ReactCurrentBatchConfig} = ReactSharedInternals;

export const NoTransition = 0;
export const NoTransition = null;

export function requestCurrentTransition(): number {
export function requestCurrentTransition(): Transition | null {
return ReactCurrentBatchConfig.transition;
}
23 changes: 14 additions & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,14 @@ export function requestUpdateLane(fiber: Fiber): Lane {
if (
__DEV__ &&
warnOnSubscriptionInsideStartTransition &&
ReactCurrentBatchConfig._updatedFibers
ReactCurrentBatchConfig.transition !== null
) {
ReactCurrentBatchConfig._updatedFibers.add(fiber);
const transition = ReactCurrentBatchConfig.transition;
if (!transition._updatedFibers) {
transition._updatedFibers = new Set();
}

transition._updatedFibers.add(fiber);
}
// The algorithm for assigning an update to a lane should be stable for all
// updates at the same priority within the same event. To do this, the
Expand Down Expand Up @@ -1246,7 +1251,7 @@ export function deferredUpdates<A>(fn: () => A): A {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DefaultEventPriority);
return fn();
} finally {
Expand Down Expand Up @@ -1285,7 +1290,7 @@ export function discreteUpdates<A, B, C, D, R>(
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
return fn(a, b, c, d);
} finally {
Expand Down Expand Up @@ -1320,7 +1325,7 @@ export function flushSync(fn) {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
if (fn) {
return fn();
Expand Down Expand Up @@ -1355,7 +1360,7 @@ export function flushControlled(fn: () => mixed): void {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
fn();
} finally {
Expand Down Expand Up @@ -1893,7 +1898,7 @@ function commitRoot(root: FiberRoot, recoverableErrors: null | Array<mixed>) {
const previousUpdateLanePriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
commitRootImpl(root, recoverableErrors, previousUpdateLanePriority);
} finally {
Expand Down Expand Up @@ -2028,7 +2033,7 @@ function commitRootImpl(

if (subtreeHasEffects || rootHasEffect) {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
const previousPriority = getCurrentUpdatePriority();
setCurrentUpdatePriority(DiscreteEventPriority);

Expand Down Expand Up @@ -2283,7 +2288,7 @@ export function flushPassiveEffects(): boolean {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(priority);
return flushPassiveEffectsImpl();
} finally {
Expand Down
23 changes: 14 additions & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,14 @@ export function requestUpdateLane(fiber: Fiber): Lane {
if (
__DEV__ &&
warnOnSubscriptionInsideStartTransition &&
ReactCurrentBatchConfig._updatedFibers
ReactCurrentBatchConfig.transition !== null
) {
ReactCurrentBatchConfig._updatedFibers.add(fiber);
const transition = ReactCurrentBatchConfig.transition;
if (!transition._updatedFibers) {
transition._updatedFibers = new Set();
}

transition._updatedFibers.add(fiber);
}
// The algorithm for assigning an update to a lane should be stable for all
// updates at the same priority within the same event. To do this, the
Expand Down Expand Up @@ -1246,7 +1251,7 @@ export function deferredUpdates<A>(fn: () => A): A {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DefaultEventPriority);
return fn();
} finally {
Expand Down Expand Up @@ -1285,7 +1290,7 @@ export function discreteUpdates<A, B, C, D, R>(
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
return fn(a, b, c, d);
} finally {
Expand Down Expand Up @@ -1320,7 +1325,7 @@ export function flushSync(fn) {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
if (fn) {
return fn();
Expand Down Expand Up @@ -1355,7 +1360,7 @@ export function flushControlled(fn: () => mixed): void {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
fn();
} finally {
Expand Down Expand Up @@ -1893,7 +1898,7 @@ function commitRoot(root: FiberRoot, recoverableErrors: null | Array<mixed>) {
const previousUpdateLanePriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(DiscreteEventPriority);
commitRootImpl(root, recoverableErrors, previousUpdateLanePriority);
} finally {
Expand Down Expand Up @@ -2028,7 +2033,7 @@ function commitRootImpl(

if (subtreeHasEffects || rootHasEffect) {
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
const previousPriority = getCurrentUpdatePriority();
setCurrentUpdatePriority(DiscreteEventPriority);

Expand Down Expand Up @@ -2283,7 +2288,7 @@ export function flushPassiveEffects(): boolean {
const prevTransition = ReactCurrentBatchConfig.transition;
const previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig.transition = 0;
ReactCurrentBatchConfig.transition = null;
setCurrentUpdatePriority(priority);
return flushPassiveEffectsImpl();
} finally {
Expand Down
11 changes: 3 additions & 8 deletions packages/react/src/ReactCurrentBatchConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@
* @flow
*/

import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {Transition} from 'react-reconciler/src/ReactFiberTracingMarkerComponent.new';

type BatchConfig = {
transition: number,
_updatedFibers?: Set<Fiber>,
transition: Transition | null,
};
/**
* Keeps track of the current batch's configuration such as how long an update
* should suspend for if it needs to.
*/
const ReactCurrentBatchConfig: BatchConfig = {
transition: 0,
transition: null,
};

if (__DEV__) {
ReactCurrentBatchConfig._updatedFibers = new Set();
}

export default ReactCurrentBatchConfig;
Loading

0 comments on commit e9aa959

Please sign in to comment.