Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sharing context objects between concurrent renderers #12779

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add back concurrent renderer warning
Only warn for two concurrent primary or two concurrent secondary renderers.
  • Loading branch information
acdlite committed May 11, 2018
commit af82611ddd595855b608f73da4c070fe70e855fa
26 changes: 26 additions & 0 deletions packages/react-reconciler/src/ReactFiberNewContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ export type NewContext = {
getContextChangedBits(context: ReactContext<any>): number,
};

import warning from 'fbjs/lib/warning';

export default function(stack: Stack, isPrimaryRenderer: boolean) {
const {createCursor, push, pop} = stack;

const providerCursor: StackCursor<Fiber | null> = createCursor(null);
const valueCursor: StackCursor<mixed> = createCursor(null);
const changedBitsCursor: StackCursor<number> = createCursor(0);

let rendererSigil;
if (__DEV__) {
// Use this to detect multiple renderers using the same context
rendererSigil = {};
}

function pushProvider(providerFiber: Fiber): void {
const context: ReactContext<any> = providerFiber.type._context;

Expand All @@ -35,13 +43,31 @@ export default function(stack: Stack, isPrimaryRenderer: boolean) {

context._currentValue = providerFiber.pendingProps.value;
context._changedBits = providerFiber.stateNode;
if (__DEV__) {
warning(
context._currentRenderer === null ||
context._currentRenderer === rendererSigil,
'Detected multiple renderers concurrently rendering the ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm bumping into this with test renderer when the context object is reused, but resetModules() is called between tests. Seems confusing.

'same context provider. This is currently unsupported.',
);
context._currentRenderer = rendererSigil;
}
} else {
push(changedBitsCursor, context._changedBits_secondary, providerFiber);
push(valueCursor, context._currentValue_secondary, providerFiber);
push(providerCursor, providerFiber, providerFiber);

context._currentValue_secondary = providerFiber.pendingProps.value;
context._changedBits_secondary = providerFiber.stateNode;
if (__DEV__) {
warning(
context._currentRenderer_secondary === null ||
context._currentRenderer_secondary === rendererSigil,
'Detected multiple renderers concurrently rendering the ' +
'same context provider. This is currently unsupported.',
);
context._currentRenderer_secondary = rendererSigil;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,46 @@ describe('ReactNewContext', () => {
}
});

it('warns if multiple renderers concurrently render the same context', () => {
spyOnDev(console, 'error');
const Context = React.createContext(0);

function Foo(props) {
ReactNoop.yield('Foo');
return null;
}

function App(props) {
return (
<Context.Provider value={props.value}>
<Foo />
<Foo />
</Context.Provider>
);
}

ReactNoop.render(<App value={1} />);
// Render past the Provider, but don't commit yet
ReactNoop.flushThrough(['Foo']);

// Get a new copy of ReactNoop
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
React = require('react');
ReactNoop = require('react-noop-renderer');

// Render the provider again using a different renderer
ReactNoop.render(<App value={1} />);
ReactNoop.flush();

if (__DEV__) {
expect(console.error.calls.argsFor(0)[0]).toContain(
'Detected multiple renderers concurrently rendering the same ' +
'context provider. This is currently unsupported',
);
}
});

it('warns if consumer child is not a function', () => {
spyOnDev(console, 'error');
const Context = React.createContext(0);
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/ReactContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function createContext<T>(

if (__DEV__) {
context._currentRenderer = null;
context._currentRenderer_secondary = null;
}

return context;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type ReactContext<T> = {

// DEV only
_currentRenderer?: Object | null,
_currentRenderer_secondary?: Object | null,
};

export type ReactPortal = {
Expand Down