introduce Fantom.flushAllNativeEvents (#48943)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48943

changelog: [internal]

A new helper function on Fantom flushAllNativeEvents, which will flush all pending native events.

Reviewed By: javache

Differential Revision: D68566753

fbshipit-source-id: 6cb19416e39807b9b381ff068cea5c2458101174
This commit is contained in:
Samuel Susla
2025-01-27 04:42:44 -08:00
committed by Facebook GitHub Bot
parent aa5760837c
commit 1257122a02
2 changed files with 34 additions and 1 deletions
@@ -577,4 +577,27 @@ describe('Fantom', () => {
root.destroy();
});
});
describe('flushAllNativeEvents', () => {
it('calls events in the event queue', () => {
const root = Fantom.createRoot();
const onLayout = jest.fn();
Fantom.runTask(() => {
root.render(
<View
style={{width: 100, height: 100}}
onLayout={event => {
onLayout(event.nativeEvent);
}}
/>,
);
});
expect(onLayout).toHaveBeenCalledTimes(0);
Fantom.flushAllNativeEvents();
expect(onLayout).toHaveBeenCalledTimes(1);
});
});
});
+11 -1
View File
@@ -120,13 +120,22 @@ function runTask(task: () => void | Promise<void>) {
}
/*
* Simmulates running a task on the UI thread and forces side effect to drain the event queue, dispatching events to JavaScript.
* Simmulates running a task on the UI thread and forces side effect to drain the event queue, scheduling events to be dispatched to JavaScript.
*/
function runOnUIThread(task: () => void) {
task();
NativeFantom.flushEventQueue();
}
/*
* Runs a side effect to drain the event queue and dispatches events to JavaScript.
* Useful to flash out all tasks.
*/
function flushAllNativeEvents() {
NativeFantom.flushEventQueue();
runWorkLoop();
}
/**
* Runs the event loop until all tasks are executed.
*/
@@ -264,6 +273,7 @@ export default {
runWorkLoop,
createRoot,
dispatchNativeEvent,
flushAllNativeEvents,
unstable_benchmark: Benchmark,
scrollTo,
};