Skip to content

Commit

Permalink
add isUnique option to Fantom.dispatchNativeEvent (facebook#48801)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#48801

changelog: [internal]

Adds isUnique option to Fantom.dispatchNativeEvent.

isUnique controls whether only the last event of the same type and target is dispatched to JavaScript or all events are queued and dispatched.

Reviewed By: rubennorte

Differential Revision: D68416157

fbshipit-source-id: 415e7db7d258d60a6bc510d929091153bfdccb3f
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 23, 2025
1 parent b9f418e commit f695411
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
56 changes: 55 additions & 1 deletion packages/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
runWorkLoop,
} from '..';
import * as React from 'react';
import {Text, TextInput, View} from 'react-native';
import {ScrollView, Text, TextInput, View} from 'react-native';
import ensureInstance from 'react-native/src/private/utilities/ensureInstance';
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';

Expand Down Expand Up @@ -443,4 +443,58 @@ describe('Fantom', () => {
const [entry] = onChange.mock.lastCall;
expect(entry.text).toEqual('Hello World');
});

it('it batches events with isUnique option', () => {
const root = createRoot();
let maybeNode;
const onScroll = jest.fn();

runTask(() => {
root.render(
<ScrollView
onScroll={event => {
onScroll(event.nativeEvent);
}}
ref={node => {
maybeNode = node;
}}
/>,
);
});

const element = ensureInstance(maybeNode, ReactNativeElement);

runOnUIThread(() => {
dispatchNativeEvent(element, 'scroll', {
contentOffset: {
x: 0,
y: 1,
},
});
dispatchNativeEvent(
element,
'scroll',
{
contentOffset: {
x: 0,
y: 2,
},
},
{
isUnique: true,
},
);
});

runWorkLoop();

expect(onScroll).toHaveBeenCalledTimes(1);
const [entry] = onScroll.mock.lastCall;
expect(entry.contentOffset).toEqual({
x: 0,
y: 2,
});

root.destroy();
});
});
5 changes: 2 additions & 3 deletions packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,15 @@ export function dispatchNativeEvent(
node: ReactNativeElement,
type: string,
payload?: {[key: string]: mixed},
options?: {
category?: NativeEventCategory,
},
options?: {category?: NativeEventCategory, isUnique?: boolean},
) {
const shadowNode = getShadowNode(node);
NativeFantom.dispatchNativeEvent(
shadowNode,
type,
payload,
options?.category,
options?.isUnique,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10474,7 +10474,8 @@ interface Spec extends TurboModule {
shadowNode: mixed,
type: string,
payload?: mixed,
category?: NativeEventCategory
category?: NativeEventCategory,
isUnique?: boolean
) => void;
getMountingManagerLogs: (surfaceId: number) => Array<string>;
flushMessageQueue: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface Spec extends TurboModule {
type: string,
payload?: mixed,
category?: NativeEventCategory,
isUnique?: boolean,
) => void;
getMountingManagerLogs: (surfaceId: number) => Array<string>;
flushMessageQueue: () => void;
Expand Down

0 comments on commit f695411

Please sign in to comment.