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 disabling interaction tracing for suspense promises #16776

Merged
merged 2 commits into from
Sep 13, 2019
Merged
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
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,9 @@ function attachSuspenseRetryListeners(finishedWork: Fiber) {
let retry = resolveRetryThenable.bind(null, finishedWork, thenable);
if (!retryCache.has(thenable)) {
if (enableSchedulerTracing) {
retry = Schedule_tracing_wrap(retry);
if (thenable.__reactDoNotTraceInteractions !== true) {
retry = Schedule_tracing_wrap(retry);
}
}
retryCache.add(thenable);
thenable.then(retry, retry);
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberThrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ function attachPingListener(
renderExpirationTime,
);
if (enableSchedulerTracing) {
ping = Schedule_tracing_wrap(ping);
if (thenable.__reactDoNotTraceInteractions !== true) {
ping = Schedule_tracing_wrap(ping);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The ping part is a bit weird because it means that means that if we’re only suspended on this promise for the first commit I believe it will decrement and complete the interaction before we commit.

I think this only happens if this also happened to cause a “Delayed” suspended state (bad loading) since otherwise we don’t suspend the commit and it’ll continue until it commits.

However fixing this is tricky since we’d have to keep track of this promise and call it once we commit to detach.

Copy link
Contributor Author

@bvaughn bvaughn Sep 13, 2019

Choose a reason for hiding this comment

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

The ping part is a bit weird because it means that means that if we’re only suspended on this promise for the first commit I believe it will decrement and complete the interaction before we commit.

Hm... I don't think this is the case, but maybe you can point out what I'm overlooking? We only decrement in finishPendingInteractions, which runs after commit.

Not-wrapping isn't the same as decrementing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another way to put it is, if this is ok, then it should be ok to remove this wrapper in all cases.

Because this ping is only relevant when there is an in progress render. If the in progress render keeps its own interactions alive then we never need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm...yeah, maybe wrapping the ping listener is never necessary.

}
}
thenable.then(ping, ping);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ const RootCompleted = 4;

export type Thenable = {
then(resolve: () => mixed, reject?: () => mixed): Thenable | void,

// Special flag to opt out of tracing interactions across a Suspense boundary.
__reactDoNotTraceInteractions?: boolean,
};

// Describes where we are in the React execution stack
Expand Down
85 changes: 85 additions & 0 deletions packages/react/src/__tests__/ReactProfiler-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,91 @@ describe('Profiler', () => {
onInteractionScheduledWorkCompleted.mock.calls[1][0],
).toMatchInteraction(highPriUpdateInteraction);
});

it('does not trace Promises flagged with __reactDoNotTraceInteractions', async () => {
loadModulesForTracing({useNoopRenderer: true});

const interaction = {
id: 0,
name: 'initial render',
timestamp: Scheduler.unstable_now(),
};

AsyncText = ({ms, text}) => {
try {
TextResource.read([text, ms]);
Scheduler.unstable_yieldValue(`AsyncText [${text}]`);
return text;
} catch (promise) {
promise.__reactDoNotTraceInteractions = true;

if (typeof promise.then === 'function') {
Scheduler.unstable_yieldValue(`Suspend [${text}]`);
} else {
Scheduler.unstable_yieldValue(`Error [${text}]`);
}
throw promise;
}
};

const onRender = jest.fn();
SchedulerTracing.unstable_trace(
interaction.name,
Scheduler.unstable_now(),
() => {
ReactNoop.render(
<React.Profiler id="test-profiler" onRender={onRender}>
<React.Suspense fallback={<Text text="Loading..." />}>
<AsyncText text="Async" ms={20000} />
</React.Suspense>
<Text text="Sync" />
</React.Profiler>,
);
},
);

expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(getWorkForReactThreads(onWorkStarted)).toHaveLength(0);
expect(getWorkForReactThreads(onWorkStopped)).toHaveLength(0);

expect(Scheduler).toFlushAndYield([
'Suspend [Async]',
'Text [Loading...]',
'Text [Sync]',
]);
// Should have committed the placeholder.
expect(ReactNoop.getChildrenAsJSX()).toEqual('Loading...Sync');
expect(onRender).toHaveBeenCalledTimes(1);

let call = onRender.mock.calls[0];
expect(call[0]).toEqual('test-profiler');
expect(call[6]).toMatchInteractions(
ReactFeatureFlags.enableSchedulerTracing ? [interaction] : [],
);

// The interaction is now complete.
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);

// Once the promise resolves, we render the suspended view
await awaitableAdvanceTimers(20000);
expect(Scheduler).toHaveYielded(['Promise resolved [Async]']);
expect(Scheduler).toFlushAndYield(['AsyncText [Async]']);
expect(ReactNoop.getChildrenAsJSX()).toEqual('AsyncSync');
expect(onRender).toHaveBeenCalledTimes(2);

// No interactions should be associated with this update.
call = onRender.mock.calls[1];
expect(call[0]).toEqual('test-profiler');
expect(call[6]).toMatchInteractions([]);
});
});
});
});