From c3490b8f0923a93250c11cc5d80b48cc5840ca3a Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Sat, 4 Jan 2025 00:35:08 -0800 Subject: [PATCH] Convert RCTModernEventEmitter (#48466) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48466 # Changelog: [Internal] - As in the title. Differential Revision: D67793108 --- .../react/animated/EventAnimationDriver.kt | 16 ++++---- .../react/fabric/events/FabricEventEmitter.kt | 12 +++--- .../events/RCTModernEventEmitter.java | 39 ------------------- .../uimanager/events/RCTModernEventEmitter.kt | 37 ++++++++++++++++++ .../uimanager/events/ReactEventEmitter.kt | 24 +++++------- 5 files changed, 60 insertions(+), 68 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.kt index cf7600f1885ef3..f59b730cf225bc 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.kt @@ -25,17 +25,17 @@ internal class EventAnimationDriver( @JvmField internal var valueNode: ValueAnimatedNode ) : RCTModernEventEmitter { @Deprecated("Deprecated in Java") - override fun receiveEvent(targetReactTag: Int, eventName: String, event: WritableMap?) = - receiveEvent(-1, targetReactTag, eventName, event) + override fun receiveEvent(targetTag: Int, eventName: String, params: WritableMap?) = + receiveEvent(-1, targetTag, eventName, params) override fun receiveEvent( surfaceId: Int, targetTag: Int, eventName: String, - event: WritableMap? + params: WritableMap? ) = // We assume this event can't be coalesced. `customCoalesceKey` has no meaning in Fabric. - receiveEvent(surfaceId, targetTag, eventName, false, 0, event, EventCategoryDef.UNSPECIFIED) + receiveEvent(surfaceId, targetTag, eventName, false, 0, params, EventCategoryDef.UNSPECIFIED) @Deprecated("Deprecated in Java") override fun receiveTouches( @@ -47,7 +47,7 @@ internal class EventAnimationDriver( } @Deprecated("Deprecated in Java") - override fun receiveTouches(touchEvent: TouchEvent) { + override fun receiveTouches(event: TouchEvent) { throw UnsupportedOperationException("receiveTouches is not support by native animated events") } @@ -57,13 +57,13 @@ internal class EventAnimationDriver( eventName: String, canCoalesceEvent: Boolean, customCoalesceKey: Int, - event: WritableMap?, + params: WritableMap?, @EventCategoryDef category: Int ) { - requireNotNull(event) { "Native animated events must have event data." } + requireNotNull(params) { "Native animated events must have event data." } // Get the new value for the node by looking into the event map using the provided event path. - var currMap: ReadableMap? = event + var currMap: ReadableMap? = params var currArray: ReadableArray? = null for (i in 0 until eventPath.size - 1) { if (currMap != null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.kt index 3420e0ed61b0c6..5395eca470a22c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.kt @@ -18,22 +18,22 @@ import com.facebook.systrace.Systrace public class FabricEventEmitter(private val uiManager: FabricUIManager) : RCTModernEventEmitter { @Deprecated("Deprecated in Java") - public override fun receiveEvent(reactTag: Int, eventName: String, params: WritableMap?): Unit { - receiveEvent(ViewUtil.NO_SURFACE_ID, reactTag, eventName, params) + public override fun receiveEvent(targetTag: Int, eventName: String, params: WritableMap?): Unit { + receiveEvent(ViewUtil.NO_SURFACE_ID, targetTag, eventName, params) } public override fun receiveEvent( surfaceId: Int, - reactTag: Int, + targetTag: Int, eventName: String, params: WritableMap? ) { - receiveEvent(surfaceId, reactTag, eventName, false, 0, params, EventCategoryDef.UNSPECIFIED) + receiveEvent(surfaceId, targetTag, eventName, false, 0, params, EventCategoryDef.UNSPECIFIED) } public override fun receiveEvent( surfaceId: Int, - reactTag: Int, + targetTag: Int, eventName: String, canCoalesceEvent: Boolean, customCoalesceKey: Int, @@ -43,7 +43,7 @@ public class FabricEventEmitter(private val uiManager: FabricUIManager) : RCTMod Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricEventEmitter.receiveEvent('$eventName')") try { - uiManager.receiveEvent(surfaceId, reactTag, eventName, canCoalesceEvent, params, category) + uiManager.receiveEvent(surfaceId, targetTag, eventName, canCoalesceEvent, params, category) } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE) } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.java deleted file mode 100644 index 056c7dcdf3e215..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.java +++ /dev/null @@ -1,39 +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 androidx.annotation.Nullable; -import com.facebook.react.bridge.WritableMap; - -/** - * This is a transitional replacement for RCTEventEmitter that works with Fabric and non-Fabric - * renderers. RCTEventEmitter works with Fabric as well, but there are negative perf implications - * and it should be avoided. - * - *

This interface will *also* be deleted in the distant future and be replaced with a new - * interface that doesn't need the old `receiveEvent` method at all. But for the foreseeable future, - * this is the recommended interface to use for EventEmitters. - */ -public interface RCTModernEventEmitter extends RCTEventEmitter { - void receiveEvent(int surfaceId, int targetTag, String eventName, @Nullable WritableMap event); - - void receiveEvent( - int surfaceId, - int targetTag, - String eventName, - boolean canCoalesceEvent, - int customCoalesceKey, - @Nullable WritableMap event, - @EventCategoryDef int category); - - /** - * @deprecated Dispatch the TouchEvent using {@link EventDispatcher} instead - */ - @Deprecated - void receiveTouches(TouchEvent event); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.kt new file mode 100644 index 00000000000000..dab9e0520172ee --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/RCTModernEventEmitter.kt @@ -0,0 +1,37 @@ +/* + * 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.bridge.WritableMap + +/** + * This is a transitional replacement for [RCTEventEmitter] that works with Fabric and non-Fabric + * renderers. [RCTEventEmitter] works with Fabric as well, but there are negative perf implications + * and it should be avoided. + * + * This interface will *also* be deleted in the distant future and be replaced with a new interface + * that doesn't need the old `receiveEvent` method at all. But for the foreseeable future, this is + * the recommended interface to use for EventEmitters. + */ +@Suppress("DEPRECATION") +public interface RCTModernEventEmitter : RCTEventEmitter { + public fun receiveEvent(surfaceId: Int, targetTag: Int, eventName: String, params: WritableMap?) + + public fun receiveEvent( + surfaceId: Int, + targetTag: Int, + eventName: String, + canCoalesceEvent: Boolean, + customCoalesceKey: Int, + params: WritableMap?, + @EventCategoryDef category: Int + ) + + @Deprecated("Dispatch the TouchEvent using [EventDispatcher] instead") + public fun receiveTouches(event: TouchEvent) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.kt index 53ad8d04d3d74b..07afeaf366e082 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.kt @@ -45,18 +45,18 @@ internal class ReactEventEmitter(private val reactContext: ReactApplicationConte } @Deprecated("Please use RCTModernEventEmitter") - override fun receiveEvent(targetReactTag: Int, eventName: String, event: WritableMap?) { - receiveEvent(-1, targetReactTag, eventName, event) + override fun receiveEvent(targetReactTag: Int, eventName: String, params: WritableMap?) { + receiveEvent(-1, targetReactTag, eventName, params) } override fun receiveEvent( surfaceId: Int, targetTag: Int, eventName: String, - event: WritableMap? + params: WritableMap? ) { // We assume this event can't be coalesced. `customCoalesceKey` has no meaning in Fabric. - receiveEvent(surfaceId, targetTag, eventName, false, 0, event, EventCategoryDef.UNSPECIFIED) + receiveEvent(surfaceId, targetTag, eventName, false, 0, params, EventCategoryDef.UNSPECIFIED) } @Deprecated("Please use RCTModernEventEmitter") @@ -108,25 +108,19 @@ internal class ReactEventEmitter(private val reactContext: ReactApplicationConte override fun receiveEvent( surfaceId: Int, - targetReactTag: Int, + targetTag: Int, eventName: String, canCoalesceEvent: Boolean, customCoalesceKey: Int, - event: WritableMap?, + params: WritableMap?, @EventCategoryDef category: Int ) { - @UIManagerType val uiManagerType = getUIManagerType(targetReactTag, surfaceId) + @UIManagerType val uiManagerType = getUIManagerType(targetTag, surfaceId) if (uiManagerType == UIManagerType.FABRIC) { fabricEventEmitter?.receiveEvent( - surfaceId, - targetReactTag, - eventName, - canCoalesceEvent, - customCoalesceKey, - event, - category) + surfaceId, targetTag, eventName, canCoalesceEvent, customCoalesceKey, params, category) } else if (uiManagerType == UIManagerType.DEFAULT) { - ensureDefaultEventEmitter()?.receiveEvent(targetReactTag, eventName, event) + ensureDefaultEventEmitter()?.receiveEvent(targetTag, eventName, params) } }