dillon1000/react
Commit
Browse files Resume immediately pinged fiber without unwinding (#25074)
* Yield to main thread if continuation is returned Instead of using an imperative method `requestYield` to ask Scheduler to yield to the main thread, we can assume that any time a Scheduler task returns a continuation callback, it's because it wants to yield to the main thread. We can assume the task already checked some condition that caused it to return a continuation, so we don't need to do any additional checks — we can immediately yield and schedule a new task for the continuation. The replaces the `requestYield` API that I added in ca990e9. * Move unwind after error into main work loop I need to be able to yield to the main thread in between when an error is thrown and when the stack is unwound. (This is the motivation behind the refactor, but it isn't implemented in this commit.) Currently the unwind is inlined directly into `handleError`. Instead, I've moved the unwind logic into the main work loop. At the very beginning of the function, we check to see if the work-in-progress is in a "suspended" state — that is, whether it needs to be unwound. If it is, we will enter the unwind phase instead of the begin phase. We only need to perform this check when we first enter the work loop: at the beginning of a Scheduler chunk, or after something throws. We don't need to perform it after every unit of work. * Yield to main thread whenever a fiber suspends When a fiber suspends, we should yield to the main thread in case the data is already cached, to unblock a potential ping event. By itself, this commit isn't useful because we don't do anything special in the case where to do receive an immediate ping event. I've split this out only to demonstrate that it doesn't break any existing behavior. See the next commit for full context and motivation. * Resume immediately pinged fiber without unwinding If a fiber suspends, and is pinged immediately in a microtask (or a regular task that fires before React resumes rendering), try rendering the same fiber again without unwinding the stack. This can be super helpful when working with promises and async-await, because even if the outermost promise hasn't been cached before, the underlying data may have been preloaded. In many cases, we can continue rendering immediately without having to show a fallback. This optimization should work during any concurrent (time-sliced) render. It doesn't work during discrete updates because those are semantically required to finish synchronously — those get the current behavior.
Changed paths18 files
First-parent comparisonpackages/react-reconciler/src/ReactFiberThrow.new.js ModifiedM packages/react-reconciler/src/ReactFiberThrow.old.js ModifiedA packages/react-reconciler/src/ReactFiberWakeable.new.js AddedA packages/react-reconciler/src/ReactFiberWakeable.old.js AddedM packages/react-reconciler/src/ReactFiberWorkLoop.new.js ModifiedM packages/react-reconciler/src/ReactFiberWorkLoop.old.js ModifiedM packages/react-reconciler/src/__tests__/ReactOffscreenSuspense-test.js ModifiedM packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js ModifiedA packages/react-reconciler/src/__tests__/ReactWakeable-test.js AddedM packages/scheduler/npm/umd/scheduler.development.js ModifiedM packages/scheduler/npm/umd/scheduler.production.min.js ModifiedM packages/scheduler/npm/umd/scheduler.profiling.min.js ModifiedM packages/scheduler/src/__tests__/Scheduler-test.js ModifiedM packages/scheduler/src/__tests__/SchedulerMock-test.js ModifiedM packages/scheduler/src/__tests__/SchedulerPostTask-test.js ModifiedM packages/scheduler/src/forks/Scheduler.js ModifiedM packages/scheduler/src/forks/SchedulerMock.js ModifiedM packages/scheduler/src/forks/SchedulerPostTask.js ModifiedPatch
Files changed
Rendering syntax-highlighted changes…