Skip to content

Commit

Permalink
update host configs with requestPostPaintCallback function
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaruan committed Sep 7, 2022
1 parent b857251 commit 93155c5
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,7 @@ export function preparePortalMount(portalInstance: any): void {
export function detachDeletedInstance(node: Instance): void {
// noop
}

export function requestPostPaintCallback(callback: (time: number) => void) {
// noop
}
11 changes: 10 additions & 1 deletion packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ export const cancelTimeout: any =
typeof clearTimeout === 'function' ? clearTimeout : (undefined: any);
export const noTimeout = -1;
const localPromise = typeof Promise === 'function' ? Promise : undefined;

const localRequestAnimationFrame =
typeof requestAnimationFrame === 'function'
? requestAnimationFrame
: scheduleTimeout;
// -------------------
// Microtasks
// -------------------
Expand Down Expand Up @@ -1379,3 +1382,9 @@ export function setupIntersectionObserver(
},
};
}

export function requestPostPaintCallback(callback: (time: number) => void) {
localRequestAnimationFrame(() => {
localRequestAnimationFrame(time => callback(time));
});
}
4 changes: 4 additions & 0 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,7 @@ export function preparePortalMount(portalInstance: Instance): void {
export function detachDeletedInstance(node: Instance): void {
// noop
}

export function requestPostPaintCallback(callback: (time: number) => void) {
// noop
}
4 changes: 4 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,7 @@ export function preparePortalMount(portalInstance: Instance): void {
export function detachDeletedInstance(node: Instance): void {
// noop
}

export function requestPostPaintCallback(callback: (time: number) => void) {
// noop
}
5 changes: 5 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
logRecoverableError() {
// no-op
},

requestPostPaintCallback(callback) {
const endTime = Scheduler.unstable_now();
callback(endTime);
},
};

const hostConfig = useMutation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('ReactFiberHostContext', () => {
return DefaultEventPriority;
},
supportsMutation: true,
requestPostPaintCallback: function() {},
});

const container = Renderer.createContainer(
Expand Down Expand Up @@ -129,6 +130,7 @@ describe('ReactFiberHostContext', () => {
getCurrentEventPriority: function() {
return DefaultEventPriority;
},
requestPostPaintCallback: function() {},
supportsMutation: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2273,4 +2273,66 @@ describe('ReactInteractionTracing', () => {
});
expect(Scheduler).toHaveYielded(['Hidden Text']);
});

// @gate enableTransitionTracing
it('discrete events', async () => {
const transitionCallbacks = {
onTransitionStart: (name, startTime) => {
Scheduler.unstable_yieldValue(
`onTransitionStart(${name}, ${startTime})`,
);
},
onTransitionProgress: (name, startTime, endTime, pending) => {
const suspenseNames = pending.map(p => p.name || '<null>').join(', ');
Scheduler.unstable_yieldValue(
`onTransitionProgress(${name}, ${startTime}, ${endTime}, [${suspenseNames}])`,
);
},
onTransitionComplete: (name, startTime, endTime) => {
Scheduler.unstable_yieldValue(
`onTransitionComplete(${name}, ${startTime}, ${endTime})`,
);
},
};

function App() {
return (
<Suspense
fallback={<Text text="Loading..." />}
unstable_name="suspense page">
<AsyncText text="Page Two" />
</Suspense>
);
}

const root = ReactNoop.createRoot({
unstable_transitionCallbacks: transitionCallbacks,
});

await act(async () => {
ReactNoop.discreteUpdates(() =>
startTransition(() => root.render(<App />), {name: 'page transition'}),
);
ReactNoop.expire(1000);
await advanceTimers(1000);
});

expect(Scheduler).toHaveYielded([
'Suspend [Page Two]',
'Loading...',
'onTransitionStart(page transition, 0)',
'onTransitionProgress(page transition, 0, 1000, [suspense page])',
]);
await act(async () => {
ReactNoop.discreteUpdates(() => resolveText('Page Two'));
ReactNoop.expire(1000);
await advanceTimers(1000);
});

expect(Scheduler).toHaveYielded([
'Page Two',
'onTransitionProgress(page transition, 0, 2000, [])',
'onTransitionComplete(page transition, 0, 2000)',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const prepareScopeUpdate = $$$hostConfig.prepareScopeUpdate;
export const getInstanceFromScope = $$$hostConfig.getInstanceFromScope;
export const getCurrentEventPriority = $$$hostConfig.getCurrentEventPriority;
export const detachDeletedInstance = $$$hostConfig.detachDeletedInstance;
export const requestPostPaintCallback = $$$hostConfig.requestPostPaintCallback;

// -------------------
// Microtasks
Expand Down
4 changes: 4 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,7 @@ export function detachDeletedInstance(node: Instance): void {
export function logRecoverableError(error: mixed): void {
// noop
}

export function requestPostPaintCallback(callback: (time: number) => void) {
// noop
}

0 comments on commit 93155c5

Please sign in to comment.