Skip to content

allow nested act()s from different renderers #15816

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion fixtures/dom/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import ARTSVGMode from 'art/modes/svg';
import ARTCurrentMode from 'art/modes/current';
import TestUtils from 'react-dom/test-utils';
import TestRenderer from 'react-test-renderer';

ARTCurrentMode.setCurrent(ARTSVGMode);

global.__DEV__ = process.env.NODE_ENV !== 'production';
Expand Down Expand Up @@ -158,3 +157,11 @@ it('does not warn when nesting react-act inside react-test-renderer', () => {
TestRenderer.create(<ARTTest />);
});
});

it("doesn't warn if you use nested acts from different renderers", () => {
TestRenderer.act(() => {
TestUtils.act(() => {
TestRenderer.create(<App />);
});
});
});
6 changes: 2 additions & 4 deletions packages/react-dom/src/test-utils/ReactTestUtilsAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ let actingUpdatesScopeDepth = 0;

function act(callback: () => Thenable) {
let previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
let previousActingUpdatesSigil;
actingUpdatesScopeDepth++;
if (__DEV__) {
previousActingUpdatesSigil = ReactCurrentActingRendererSigil.current;
ReactCurrentActingRendererSigil.current = ReactActingRendererSigil;
ReactCurrentActingRendererSigil.current.push(ReactActingRendererSigil);
}

function onDone() {
actingUpdatesScopeDepth--;
if (__DEV__) {
ReactCurrentActingRendererSigil.current = previousActingUpdatesSigil;
ReactCurrentActingRendererSigil.current.pop();
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
// if it's _less than_ previousActingUpdatesScopeDepth, then we can assume the 'other' one has warned
warningWithoutStack(
Expand Down
6 changes: 2 additions & 4 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,17 +615,15 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

function act(callback: () => Thenable) {
let previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
let previousActingUpdatesSigil;
actingUpdatesScopeDepth++;
if (__DEV__) {
previousActingUpdatesSigil = ReactCurrentActingRendererSigil.current;
ReactCurrentActingRendererSigil.current = ReactActingRendererSigil;
ReactCurrentActingRendererSigil.current.push(ReactActingRendererSigil);
}

function onDone() {
actingUpdatesScopeDepth--;
if (__DEV__) {
ReactCurrentActingRendererSigil.current = previousActingUpdatesSigil;
ReactCurrentActingRendererSigil.current.pop();
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
// if it's _less than_ previousActingUpdatesScopeDepth, then we can assume the 'other' one has warned
warningWithoutStack(
Expand Down
14 changes: 10 additions & 4 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2422,8 +2422,10 @@ export function warnIfNotScopedWithMatchingAct(fiber: Fiber): void {
if (__DEV__) {
if (
shouldWarnUnactedUpdates === true &&
ReactCurrentActingRendererSigil.current !== null &&
ReactCurrentActingRendererSigil.current !== ReactActingRendererSigil
ReactCurrentActingRendererSigil.current.length !== 0 &&
ReactCurrentActingRendererSigil.current.indexOf(
ReactActingRendererSigil,
) === -1
) {
warningWithoutStack(
false,
Expand All @@ -2449,7 +2451,9 @@ export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
if (__DEV__) {
if (
shouldWarnUnactedUpdates === true &&
ReactCurrentActingRendererSigil.current !== ReactActingRendererSigil
ReactCurrentActingRendererSigil.current.indexOf(
ReactActingRendererSigil,
) === -1
) {
warningWithoutStack(
false,
Expand All @@ -2476,7 +2480,9 @@ function warnIfNotCurrentlyActingUpdatesInDEV(fiber: Fiber): void {
if (
shouldWarnUnactedUpdates === true &&
executionContext === NoContext &&
ReactCurrentActingRendererSigil.current !== ReactActingRendererSigil
ReactCurrentActingRendererSigil.current.indexOf(
ReactActingRendererSigil,
) === -1
) {
warningWithoutStack(
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('ReactNoop.act()', () => {
/>,
);
});
expect(Scheduler).toFlushWithoutYielding();
expect(calledLog).toEqual([0]);
});

Expand All @@ -54,7 +53,6 @@ describe('ReactNoop.act()', () => {
ReactNoop.render(<App />);
});
expect(Scheduler).toHaveYielded(['stage 1', 'stage 2']);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([{text: '1', hidden: false}]);
});
});
6 changes: 2 additions & 4 deletions packages/react-test-renderer/src/ReactTestRendererAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ let actingUpdatesScopeDepth = 0;

function act(callback: () => Thenable) {
let previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
let previousActingUpdatesSigil;
actingUpdatesScopeDepth++;
if (__DEV__) {
previousActingUpdatesSigil = ReactCurrentActingRendererSigil.current;
ReactCurrentActingRendererSigil.current = ReactActingRendererSigil;
ReactCurrentActingRendererSigil.current.push(ReactActingRendererSigil);
}

function onDone() {
actingUpdatesScopeDepth--;
if (__DEV__) {
ReactCurrentActingRendererSigil.current = previousActingUpdatesSigil;
ReactCurrentActingRendererSigil.current.pop();
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
// if it's _less than_ previousActingUpdatesScopeDepth, then we can assume the 'other' one has warned
warningWithoutStack(
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/ReactCurrentActingRendererSigil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
*/

/**
* Used by act() to track whether you're outside an act() scope.
* We use a renderer's flushPassiveEffects as the sigil value
* so we can track identity of the renderer.
* We maintain a 'stack' of renderer specific sigils
* corresponding to act() calls, so we can track whenever an update
* happens inside/outside of one.
*/

const ReactCurrentActingRendererSigil = {
current: (null: null | (() => boolean)),
current: ([]: Array<{}>),
Copy link
Collaborator

@acdlite acdlite Jun 11, 2019

Choose a reason for hiding this comment

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

This is conceptually a Set, not an Array, so (assuming this is the approach we go with) let's make it an Set.

Copy link
Contributor Author

@threepointone threepointone Jun 12, 2019

Choose a reason for hiding this comment

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

I started this with a Set, until I realized it's not one. It's a stack. Consider when leaving an act() scope, if it was a Set, how could I know if the sigil has to be removed from the set?

};
export default ReactCurrentActingRendererSigil;