From 68b1bc00f5689c7c53a8e54ccd1bdf2469d545a6 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 27 Jan 2025 03:54:41 -0800 Subject: [PATCH] introduce Fantom.flushAllNativeEvents (#48943) Summary: changelog: [internal] A new helper function on Fantom flushAllNativeEvents, which will flush all pending native events. Reviewed By: javache Differential Revision: D68566753 --- .../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 88a69ef4b02b..3eb82e160c52 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 640efa2c43d1..327227d743bf 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, };