Skip to content

Commit

Permalink
introduce Fantom.flushAllNativeEvents (#48943)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 27, 2025
1 parent aa57608 commit 1257122
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
12 changes: 11 additions & 1 deletion packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -264,6 +273,7 @@ export default {
runWorkLoop,
createRoot,
dispatchNativeEvent,
flushAllNativeEvents,
unstable_benchmark: Benchmark,
scrollTo,
};

0 comments on commit 1257122

Please sign in to comment.