Skip to content

Continue Add end-of-frame scheduling for default events #25467

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
wants to merge 3 commits into from
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
10 changes: 10 additions & 0 deletions packages/jest-react/src/internalAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
let didFlushWork;
do {
didFlushWork = Scheduler.unstable_flushAllWithoutAsserting();

// Flush scheduled rAF.
if (global.flushRequestAnimationFrameQueue) {
global.flushRequestAnimationFrameQueue();
}
} while (didFlushWork);
return {
then(resolve, reject) {
Expand All @@ -130,6 +135,11 @@ function flushActWork(resolve, reject) {
reject(error);
}

// Flush scheduled rAF.
if (global.flushRequestAnimationFrameQueue) {
global.flushRequestAnimationFrameQueue();
}

// If Scheduler yields while there's still work, it's so that we can
// unblock the main thread (e.g. for paint or for microtasks). Yield to
// the main thread and continue in a new task.
Expand Down
65 changes: 65 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

// TODO: Remove this deep import when we delete the legacy root API
import {ConcurrentMode, NoMode} from 'react-reconciler/src/ReactTypeOfMode';
import * as Scheduler from 'scheduler';

import {
prepareToRenderResources,
Expand Down Expand Up @@ -430,6 +431,11 @@ export function getInstanceFromScope(
return null;
}

const localCancelAnimationFrame =
typeof window !== 'undefined' &&
typeof window.cancelAnimationFrame === 'function'
? window.cancelAnimationFrame
: cancelTimeout;
// -------------------
// Microtasks
// -------------------
Expand All @@ -445,6 +451,65 @@ export const scheduleMicrotask: any =
.catch(handleErrorInNextTick)
: scheduleTimeout; // TODO: Determine the best fallback here.

export const supportsFrameAlignedTask = true;

type FrameAlignedTask = {|
rafNode: AnimationFrameID,
schedulerNode: number | null,
task: function,
|};

let currentTask: FrameAlignedTask | null = null;
function performFrameAlignedWork() {
if (currentTask != null) {
const currentTaskForFlow = currentTask;
const task = currentTask.task;
localCancelAnimationFrame(currentTaskForFlow.rafNode);
if (currentTaskForFlow.schedulerNode !== null) {
Scheduler.unstable_cancelCallback(currentTaskForFlow.schedulerNode);
}
currentTask = null;
if (task != null) {
task();
}
}
}

export function scheduleFrameAlignedTask(task: any): any {
if (currentTask === null) {
const rafNode = localRequestAnimationFrame(performFrameAlignedWork);

const schedulerNode = Scheduler.unstable_scheduleCallback(
Scheduler.unstable_NormalPriority,
performFrameAlignedWork,
);

currentTask = {
rafNode,
schedulerNode,
task,
};
} else {
currentTask.task = task;
currentTask.schedulerNode = Scheduler.unstable_scheduleCallback(
Scheduler.unstable_NormalPriority,
performFrameAlignedWork,
);
}

return currentTask;
}

export function cancelFrameAlignedTask(task: any) {
if (task.schedulerNode) {
Scheduler.unstable_cancelCallback(task.schedulerNode);
task.schedulerNode = null;
}
// We don't cancel the rAF in case it gets re-used later.
// But clear the task so if it fires and shouldn't run, it won't.
task.task = null;
}

function handleErrorInNextTick(error) {
setTimeout(() => {
throw error;
Expand Down
Loading