forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReactEventEmitter -> Kotlin (facebook#48464)
Summary: Pull Request resolved: facebook#48464 # Changelog: [Internal] - As in the title. Differential Revision: D67791375
- Loading branch information
1 parent
dab98c8
commit de2f5c2
Showing
4 changed files
with
147 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 0 additions & 165 deletions
165
...ive/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.java
This file was deleted.
Oops, something went wrong.
139 changes: 139 additions & 0 deletions
139
...ative/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* 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.infer.annotation.Assertions | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactNoCrashSoftException | ||
import com.facebook.react.bridge.ReactSoftExceptionLogger.logSoftException | ||
import com.facebook.react.bridge.WritableArray | ||
import com.facebook.react.bridge.WritableMap | ||
import com.facebook.react.uimanager.common.UIManagerType | ||
import com.facebook.react.uimanager.common.ViewUtil.getUIManagerType | ||
|
||
internal class ReactEventEmitter(private val reactContext: ReactApplicationContext) : | ||
RCTModernEventEmitter { | ||
/** Corresponds to [com.facebook.react.fabric.events.FabricEventEmitter] */ | ||
private var fabricEventEmitter: RCTModernEventEmitter? = null | ||
|
||
/** Corresponds to (Paper) EventEmitter, which is a JS interface */ | ||
@Suppress("DEPRECATION") private var defaultEventEmitter: RCTEventEmitter? = null | ||
|
||
fun register(@UIManagerType uiManagerType: Int, eventEmitter: RCTModernEventEmitter?) { | ||
assert(uiManagerType == UIManagerType.FABRIC) | ||
fabricEventEmitter = eventEmitter | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
fun register(@UIManagerType uiManagerType: Int, eventEmitter: RCTEventEmitter?) { | ||
assert(uiManagerType == UIManagerType.DEFAULT) | ||
defaultEventEmitter = eventEmitter | ||
} | ||
|
||
fun unregister(@UIManagerType uiManagerType: Int) { | ||
if (uiManagerType == UIManagerType.DEFAULT) { | ||
defaultEventEmitter = null | ||
} else { | ||
fabricEventEmitter = null | ||
} | ||
} | ||
|
||
@Deprecated("Please use RCTModernEventEmitter") | ||
override fun receiveEvent(targetReactTag: Int, eventName: String, event: WritableMap?) { | ||
receiveEvent(-1, targetReactTag, eventName, event) | ||
} | ||
|
||
override fun receiveEvent( | ||
surfaceId: Int, | ||
targetTag: Int, | ||
eventName: String, | ||
event: WritableMap? | ||
) { | ||
// We assume this event can't be coalesced. `customCoalesceKey` has no meaning in Fabric. | ||
receiveEvent(surfaceId, targetTag, eventName, false, 0, event, EventCategoryDef.UNSPECIFIED) | ||
} | ||
|
||
@Deprecated("Please use RCTModernEventEmitter") | ||
override fun receiveTouches( | ||
eventName: String, | ||
touches: WritableArray, | ||
changedIndices: WritableArray | ||
) { | ||
/* | ||
* This method should be unused by default processing pipeline, but leaving it here to make sure | ||
* that any custom code using it in legacy renderer is compatible | ||
*/ | ||
Assertions.assertCondition(touches.size() > 0) | ||
|
||
@Suppress("DEPRECATION") val reactTag = touches.getMap(0)?.getInt(TouchesHelper.TARGET_KEY) ?: 0 | ||
@UIManagerType val uiManagerType = getUIManagerType(reactTag) | ||
if (uiManagerType == UIManagerType.DEFAULT) { | ||
@Suppress("DEPRECATION") | ||
ensureDefaultEventEmitter()?.receiveTouches(eventName, touches, changedIndices) | ||
} | ||
} | ||
|
||
@Deprecated("Please use RCTModernEventEmitter") | ||
override fun receiveTouches(event: TouchEvent) { | ||
@UIManagerType val uiManagerType = getUIManagerType(event.viewTag, event.surfaceId) | ||
if (uiManagerType == UIManagerType.FABRIC) { | ||
fabricEventEmitter?.let { TouchesHelper.sendTouchEvent(it, event) } | ||
} else if (uiManagerType == UIManagerType.DEFAULT) { | ||
ensureDefaultEventEmitter()?.let { TouchesHelper.sendTouchesLegacy(it, event) } | ||
} | ||
} | ||
|
||
/** | ||
* Get default/Paper event emitter. Callers should have verified that this is not an event for a | ||
* View managed by Fabric | ||
*/ | ||
@Suppress("DEPRECATION") | ||
private fun ensureDefaultEventEmitter(): RCTEventEmitter? { | ||
if (defaultEventEmitter == null) { | ||
if (reactContext.hasActiveReactInstance()) { | ||
@Suppress("DEPRECATION") | ||
defaultEventEmitter = reactContext.getJSModule(RCTEventEmitter::class.java) | ||
} else { | ||
logSoftException( | ||
TAG, | ||
ReactNoCrashSoftException( | ||
"Cannot get RCTEventEmitter from Context, no active Catalyst instance!")) | ||
} | ||
} | ||
return defaultEventEmitter | ||
} | ||
|
||
override fun receiveEvent( | ||
surfaceId: Int, | ||
targetReactTag: Int, | ||
eventName: String, | ||
canCoalesceEvent: Boolean, | ||
customCoalesceKey: Int, | ||
event: WritableMap?, | ||
@EventCategoryDef category: Int | ||
) { | ||
@UIManagerType val uiManagerType = getUIManagerType(targetReactTag, surfaceId) | ||
if (uiManagerType == UIManagerType.FABRIC) { | ||
fabricEventEmitter?.receiveEvent( | ||
surfaceId, | ||
targetReactTag, | ||
eventName, | ||
canCoalesceEvent, | ||
customCoalesceKey, | ||
event, | ||
category) | ||
} else if (uiManagerType == UIManagerType.DEFAULT) { | ||
@Suppress("DEPRECATION") | ||
ensureDefaultEventEmitter()?.receiveEvent(targetReactTag, eventName, event) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "ReactEventEmitter" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters