mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 00:20:28 +01:00
While most builds of Flight and Fizz schedule work in new tasks some do execute work synchronously. While this is necessary for legacy APIs like renderToString for modern APIs there really isn't a great reason to do this synchronously. We could schedule works as microtasks but we actually want to yield so the runtime can run events and other things that will unblock additional work before starting the next work loop. This change updates all non-legacy uses to be async using the best availalble macrotask scheduler. Browser now uses postMessage Bun uses setTimeout because while it also supports setImmediate the scheduling is not as eager as the same API in node the FB build also uses setTimeout This change required a number of changes to tests which were utilizing the sync nature of work in the Browser builds to avoid having to manage timers and tasks. I added a patch to install MessageChannel which is required by the browser builds and made this patched version integrate with the Scheduler mock. This way we can effectively use `act` to flush flight and fizz work similar to how we do this on the client.
14 lines
379 B
JavaScript
14 lines
379 B
JavaScript
'use strict';
|
|
|
|
export function patchSetImmediate(Scheduler) {
|
|
if (!Scheduler) {
|
|
throw new Error(
|
|
'setImmediate patch was used without providing a Scheduler implementation. If you are patching setImmediate you must provide a Scheduler.'
|
|
);
|
|
}
|
|
|
|
global.setImmediate = cb => {
|
|
Scheduler.unstable_scheduleCallback(Scheduler.unstable_NormalPriority, cb);
|
|
};
|
|
}
|