Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};