From a68806c44f96ac73d93f199fa8c4db3fd039222e Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Thu, 2 Jan 2025 10:33:50 -0800 Subject: [PATCH] Migrate EventDispatcher interface to Kotlin (#48445) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48445 # Changelog: [Internal] - As in the title. Differential Revision: D67760373 --- .../uimanager/events/EventDispatcher.java | 37 ----------------- .../react/uimanager/events/EventDispatcher.kt | 40 +++++++++++++++++++ .../uimanager/JSPointerDispatcherTest.kt | 8 ++-- 3 files changed, 44 insertions(+), 41 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java deleted file mode 100644 index 056d671143cbe9..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uimanager.events; - -import com.facebook.react.uimanager.common.UIManagerType; - -public interface EventDispatcher { - - /** Sends the given Event to JS, coalescing eligible events if JS is backed up. */ - void dispatchEvent(Event event); - - void dispatchAllEvents(); - - /** Add a listener to this EventDispatcher. */ - void addListener(EventDispatcherListener listener); - - /** Remove a listener from this EventDispatcher. */ - void removeListener(EventDispatcherListener listener); - - void addBatchEventDispatchedListener(BatchEventDispatchedListener listener); - - void removeBatchEventDispatchedListener(BatchEventDispatchedListener listener); - - @Deprecated - void registerEventEmitter(@UIManagerType int uiManagerType, RCTEventEmitter eventEmitter); - - void registerEventEmitter(@UIManagerType int uiManagerType, RCTModernEventEmitter eventEmitter); - - void unregisterEventEmitter(@UIManagerType int uiManagerType); - - void onCatalystInstanceDestroyed(); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.kt new file mode 100644 index 00000000000000..cbb7b623374237 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager.events + +import com.facebook.react.uimanager.common.UIManagerType + +public interface EventDispatcher { + /** Sends the given Event to JS, coalescing eligible events if JS is backed up. */ + public fun dispatchEvent(event: Event<*>) + + public fun dispatchAllEvents() + + /** Add a listener to this EventDispatcher. */ + public fun addListener(listener: EventDispatcherListener) + + /** Remove a listener from this EventDispatcher. */ + public fun removeListener(listener: EventDispatcherListener) + + public fun addBatchEventDispatchedListener(listener: BatchEventDispatchedListener) + + public fun removeBatchEventDispatchedListener(listener: BatchEventDispatchedListener) + + @Deprecated("Use the modern version with RCTModernEventEmitter") + @Suppress("DEPRECATION") + public fun registerEventEmitter(@UIManagerType uiManagerType: Int, eventEmitter: RCTEventEmitter) + + public fun registerEventEmitter( + @UIManagerType uiManagerType: Int, + eventEmitter: RCTModernEventEmitter + ) + + public fun unregisterEventEmitter(@UIManagerType uiManagerType: Int) + + public fun onCatalystInstanceDestroyed() +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt index 8c6fe8760ec216..249c6825ccc125 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt @@ -22,9 +22,9 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatcher -import org.mockito.Mockito.argThat -import org.mockito.Mockito.mock -import org.mockito.Mockito.verify +import org.mockito.kotlin.argThat +import org.mockito.kotlin.mock +import org.mockito.kotlin.verify import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment @@ -75,7 +75,7 @@ class JSPointerDispatcherTest { val ev = createMotionEvent( MotionEvent.ACTION_DOWN, childRect.centerX().toFloat(), childRect.centerY().toFloat()) - val mockDispatcher: EventDispatcher = mock(EventDispatcher::class.java) + val mockDispatcher: EventDispatcher = mock() pointerDispatcher.handleMotionEvent(ev, mockDispatcher, false) verify(mockDispatcher).dispatchEvent(argThat(EventWithName(PointerEventHelper.POINTER_DOWN))) }