From 1257122a0287c8dd8cec65499da06f19eca471d5 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 27 Jan 2025 04:42:44 -0800 Subject: [PATCH] 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 --- .../src/__tests__/Fantom-itest.js | 23 +++++++++++++++++++ packages/react-native-fantom/src/index.js | 12 +++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/react-native-fantom/src/__tests__/Fantom-itest.js b/packages/react-native-fantom/src/__tests__/Fantom-itest.js index 88a69ef4b02..3eb82e160c5 100644 --- a/packages/react-native-fantom/src/__tests__/Fantom-itest.js +++ b/packages/react-native-fantom/src/__tests__/Fantom-itest.js @@ -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( + { + onLayout(event.nativeEvent); + }} + />, + ); + }); + + expect(onLayout).toHaveBeenCalledTimes(0); + + Fantom.flushAllNativeEvents(); + + expect(onLayout).toHaveBeenCalledTimes(1); + }); + }); }); diff --git a/packages/react-native-fantom/src/index.js b/packages/react-native-fantom/src/index.js index 640efa2c43d..327227d743b 100644 --- a/packages/react-native-fantom/src/index.js +++ b/packages/react-native-fantom/src/index.js @@ -120,13 +120,22 @@ function runTask(task: () => void | Promise) { } /* - * 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, };