Skip to content

Commit

Permalink
fix: only intercept events with waiting handlers
Browse files Browse the repository at this point in the history
Previously, NativeReanimatedModule::handleRawEvent would intercept all
events received by the event listener. This resulted in an issue where
onLayout would not fire in JS on the New Architecture.

Instead, only intercept events with waiting handlers. This prevents
asJSIValue from being called on the Reanimated event loop and allows
onLayout to bubble up in JS.

See
https://github.com/facebook/react-native/blob/v0.76.2/packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.cpp#L82-L112,
which prevents onLayout from being dispatched more than once.

asJSIValue evaluates the lambda above in
https://github.com/facebook/react-native/blob/v0.76.2/packages/react-native/ReactCommon/react/renderer/core/ValueFactoryEventPayload.cpp#L16.

Fixes software-mansion#6684
  • Loading branch information
mhoran committed Dec 18, 2024
1 parent c6d6815 commit d8a240e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default function RuntimeTestsExample() {
require('./tests/core/useDerivedValue/chain.test');

require('./tests/core/useSharedValue/animationsCompilerApi.test');

require('./tests/core/onLayout.test');
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useEffect } from 'react';
import type { LayoutChangeEvent } from 'react-native';
import { StyleSheet, View } from 'react-native';
import Animated, { runOnJS, runOnUI, useAnimatedStyle, useEvent, useSharedValue } from 'react-native-reanimated';
import { describe, expect, notify, render, test, wait, waitForNotify } from '../../ReJest/RuntimeTestsApi';

interface TestResult {
height: number;
animatedHandlerCalled: boolean;
}

const TestComponent = ({ result, notifyId }: { result: TestResult; notifyId: number }) => {
const height = useSharedValue(styles.smallBox.height);

const onLayout = (event: LayoutChangeEvent) => {
result.height = event.nativeEvent.layout.height;
if (result.height === 200) {
notify(`onLayout${notifyId}`);
}
};

const animatedStyle = useAnimatedStyle(() => {
return { height: height.value };
});

useEffect(() => {
runOnUI(() => {
height.value += 100;
})();
}, [height]);

const setAnimatedHandlerCalled = () => {
result.animatedHandlerCalled = true;
notify(`animatedOnLayout${notifyId}`);
};

const animatedOnLayout = useEvent(() => {
'worklet';
runOnJS(setAnimatedHandlerCalled)();
}, ['onLayout']);

return (
<View onLayout={onLayout}>
<Animated.View style={[styles.smallBox, animatedStyle]} onLayout={animatedOnLayout} />
</View>
);
};

describe('onLayout', () => {
test('is not intercepted when there are no registered event handlers', async () => {
const result = {} as TestResult;
await render(<TestComponent result={result} notifyId={1} />);
await Promise.race([waitForNotify('onLayout1'), wait(1000)]);
expect(result.height).toBe(200);
});

test('is dispatched to the registered event handler', async () => {
const result = {} as TestResult;
await render(<TestComponent result={result} notifyId={2} />);
await Promise.race([waitForNotify('animatedOnLayout2'), wait(1000)]);
expect(result.animatedHandlerCalled).toBe(true);
});
});

const styles = StyleSheet.create({
smallBox: {
width: 100,
height: 100,
backgroundColor: 'pink',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ bool ReanimatedModuleProxy::handleRawEvent(
if (eventType.rfind("top", 0) == 0) {
eventType = "on" + eventType.substr(3);
}

if (!isAnyHandlerWaitingForEvent(eventType, tag)) {
return false;
}

jsi::Runtime &rt =
workletsModuleProxy_->getUIWorkletRuntime()->getJSIRuntime();
const auto &eventPayload = rawEvent.eventPayload;
Expand Down

0 comments on commit d8a240e

Please sign in to comment.