Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: lookup method only once #682

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,53 @@ import android.view.MotionEvent
import android.view.ViewGroup
import com.facebook.react.uimanager.JSPointerDispatcher
import com.facebook.react.uimanager.events.EventDispatcher
import java.lang.reflect.Method

/**
* Compat layer for `JSPointerDispatcher` interface for RN < 0.72
*/
class JSPointerDispatcherCompat(
private val viewGroup: ViewGroup,
viewGroup: ViewGroup,
) : JSPointerDispatcher(viewGroup) {
fun handleMotionEventCompat(
event: MotionEvent?,
eventDispatcher: EventDispatcher?,
isCapture: Boolean,
) {
private val handleMotionEventMethod: Method? by lazy {
try {
// Try to get the method with 3 parameters (for RN >= 0.72)
val method =
JSPointerDispatcher::class.java.getMethod(
"handleMotionEvent",
MotionEvent::class.java,
EventDispatcher::class.java,
Boolean::class.javaPrimitiveType,
)
method.invoke(this, event, eventDispatcher, isCapture)
// Try to get the 3-parameter method (for RN >= 0.72)
JSPointerDispatcher::class.java.getMethod(
HANDLE_MOTION_EVENT,
MotionEvent::class.java,
EventDispatcher::class.java,
Boolean::class.javaPrimitiveType,
)
} catch (_: NoSuchMethodException) {
// Fallback to 2-parameter version (for RN < 0.72)
val method =
try {
// Fallback to 2-parameter method (for RN < 0.72)
JSPointerDispatcher::class.java.getMethod(
"handleMotionEvent",
HANDLE_MOTION_EVENT,
MotionEvent::class.java,
EventDispatcher::class.java,
)
method.invoke(this, event, eventDispatcher)
} catch (_: NoSuchMethodException) {
null
}
}
}

fun handleMotionEventCompat(
event: MotionEvent?,
eventDispatcher: EventDispatcher?,
isCapture: Boolean,
) {
handleMotionEventMethod?.let { method ->
if (method.parameterCount == RN_72_PARAMS_COUNT) {
method.invoke(this, event, eventDispatcher, isCapture)
} else {
method.invoke(this, event, eventDispatcher)
}
}
}

companion object {
private const val HANDLE_MOTION_EVENT = "handleMotionEvent"
private const val RN_72_PARAMS_COUNT = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ class OverKeyboardRootViewGroup(

override fun onInterceptHoverEvent(event: MotionEvent): Boolean {
eventDispatcher?.let {
jsPointerDispatcher?.handleMotionEventCompat(event, eventDispatcher, true)
jsPointerDispatcher?.handleMotionEventCompat(event, it, true)
}
return super.onHoverEvent(event)
}

override fun onHoverEvent(event: MotionEvent): Boolean {
eventDispatcher?.let {
jsPointerDispatcher?.handleMotionEventCompat(event, eventDispatcher, false)
jsPointerDispatcher?.handleMotionEventCompat(event, it, false)
}
return super.onHoverEvent(event)
}
Expand Down