Skip to content

Commit

Permalink
Remove unstable_changedBits
Browse files Browse the repository at this point in the history
We added this unstable feature a few years ago, as a way to opt out of
context updates, but it didn't prove useful in practice.

We have other proposals for how to address the same problem, like
context selectors.

Since it was prefixed with `unstable_`, we should be able to remove it
without consequence. The hook API already warned if you used it.

Even if someone is using it somewhere, it's meant to be an optimization
only, so if they are using the API properly, it should not have any
semantic impact.
  • Loading branch information
acdlite committed Mar 8, 2021
1 parent 5fe091c commit efad8ac
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 720 deletions.
4 changes: 2 additions & 2 deletions packages/react-cache/src/ReactCacheOld.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;

function readContext(Context, observedBits) {
function readContext(Context) {
const dispatcher = ReactCurrentDispatcher.current;
if (dispatcher === null) {
throw new Error(
Expand All @@ -55,7 +55,7 @@ function readContext(Context, observedBits) {
'lifecycle methods.',
);
}
return dispatcher.readContext(Context, observedBits);
return dispatcher.readContext(Context);
}

function identityHashFn(input) {
Expand Down
10 changes: 2 additions & 8 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,12 @@ function getCacheForType<T>(resourceType: () => T): T {
invariant(false, 'Not implemented.');
}

function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
function readContext<T>(context: ReactContext<T>): T {
// For now we don't expose readContext usage in the hooks debugging info.
return context._currentValue;
}

function useContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
function useContext<T>(context: ReactContext<T>): T {
hookLog.push({
primitive: 'Context',
stackError: new Error(),
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-shared/src/devtools/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const Rejected = 2;
const ReactCurrentDispatcher = (React: any)
.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher;

function readContext(Context, observedBits) {
function readContext(Context) {
const dispatcher = ReactCurrentDispatcher.current;
if (dispatcher === null) {
throw new Error(
Expand All @@ -70,7 +70,7 @@ function readContext(Context, observedBits) {
'lifecycle methods.',
);
}
return dispatcher.readContext(Context, observedBits);
return dispatcher.readContext(Context);
}

const CacheContext = createContext(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,17 +736,6 @@ describe('ReactDOMServerHooks', () => {
},
);

itRenders('warns when bitmask is passed to useContext', async render => {
const Context = React.createContext('Hi');

function Foo() {
return <span>{useContext(Context, 1)}</span>;
}

const domNode = await render(<Foo />, 1);
expect(domNode.textContent).toBe('Hi');
});

describe('useDebugValue', () => {
itRenders('is a noop', async render => {
function Counter(props) {
Expand All @@ -760,11 +749,11 @@ describe('ReactDOMServerHooks', () => {
});

describe('readContext', () => {
function readContext(Context, observedBits) {
function readContext(Context) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Context, observedBits);
return dispatcher.readContext(Context);
}

itRenders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ describe('ReactDOMServerIntegration', () => {
});

itRenders('readContext() in different components', async render => {
function readContext(Ctx, observedBits) {
function readContext(Ctx) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Ctx, observedBits);
return dispatcher.readContext(Ctx);
}

class Cls extends React.Component {
Expand Down
10 changes: 2 additions & 8 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ function getCacheForType<T>(resourceType: () => T): T {
invariant(false, 'Not implemented.');
}

function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
function readContext<T>(context: ReactContext<T>): T {
const threadID = currentPartialRenderer.threadID;
validateContextBounds(context, threadID);
if (__DEV__) {
Expand All @@ -238,10 +235,7 @@ function readContext<T>(
return context[threadID];
}

function useContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
function useContext<T>(context: ReactContext<T>): T {
if (__DEV__) {
currentHookNameInDev = 'useContext';
}
Expand Down
29 changes: 6 additions & 23 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ import {
checkIfContextChanged,
readContext,
prepareToReadContext,
calculateChangedBits,
scheduleWorkOnParentPath,
} from './ReactFiberNewContext.new';
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.new';
Expand Down Expand Up @@ -221,7 +220,7 @@ import {
restoreSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.new';
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
import is from 'shared/objectIs';

import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';

Expand Down Expand Up @@ -796,12 +795,7 @@ function updateCacheComponent(
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// This cache refreshed. Propagate a context change.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}
}
Expand Down Expand Up @@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}

Expand Down Expand Up @@ -3007,8 +2996,7 @@ function updateContextProvider(
} else {
if (oldProps !== null) {
const oldValue = oldProps.value;
const changedBits = calculateChangedBits(context, newValue, oldValue);
if (changedBits === 0) {
if (is(oldValue, newValue)) {
// No change. Bailout early if children are the same.
if (
oldProps.children === newProps.children &&
Expand All @@ -3023,12 +3011,7 @@ function updateContextProvider(
} else {
// The context value changed. Search for matching consumers and schedule
// them to update.
propagateContextChange(
workInProgress,
context,
changedBits,
renderLanes,
);
propagateContextChange(workInProgress, context, renderLanes);
}
}
}
Expand Down Expand Up @@ -3086,7 +3069,7 @@ function updateContextConsumer(
}

prepareToReadContext(workInProgress, renderLanes);
const newValue = readContext(context, newProps.unstable_observedBits);
const newValue = readContext(context);
let newChildren;
if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
Expand Down
29 changes: 6 additions & 23 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ import {
checkIfContextChanged,
readContext,
prepareToReadContext,
calculateChangedBits,
scheduleWorkOnParentPath,
} from './ReactFiberNewContext.old';
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.old';
Expand Down Expand Up @@ -221,7 +220,7 @@ import {
restoreSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.old';
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
import is from 'shared/objectIs';

import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';

Expand Down Expand Up @@ -796,12 +795,7 @@ function updateCacheComponent(
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// This cache refreshed. Propagate a context change.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}
}
Expand Down Expand Up @@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}

Expand Down Expand Up @@ -3007,8 +2996,7 @@ function updateContextProvider(
} else {
if (oldProps !== null) {
const oldValue = oldProps.value;
const changedBits = calculateChangedBits(context, newValue, oldValue);
if (changedBits === 0) {
if (is(oldValue, newValue)) {
// No change. Bailout early if children are the same.
if (
oldProps.children === newProps.children &&
Expand All @@ -3023,12 +3011,7 @@ function updateContextProvider(
} else {
// The context value changed. Search for matching consumers and schedule
// them to update.
propagateContextChange(
workInProgress,
context,
changedBits,
renderLanes,
);
propagateContextChange(workInProgress, context, renderLanes);
}
}
}
Expand Down Expand Up @@ -3086,7 +3069,7 @@ function updateContextConsumer(
}

prepareToReadContext(workInProgress, renderLanes);
const newValue = readContext(context, newProps.unstable_observedBits);
const newValue = readContext(context);
let newChildren;
if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
// We don't use Consumer/Provider for Cache components. So we'll cheat.
Consumer: (null: any),
Provider: (null: any),
_calculateChangedBits: null,
// We'll initialize these at the root.
_currentValue: (null: any),
_currentValue2: (null: any),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
// We don't use Consumer/Provider for Cache components. So we'll cheat.
Consumer: (null: any),
Provider: (null: any),
_calculateChangedBits: null,
// We'll initialize these at the root.
_currentValue: (null: any),
_currentValue2: (null: any),
Expand Down
Loading

0 comments on commit efad8ac

Please sign in to comment.