Skip to content

Commit 1033584

Browse files
riteshshukla04facebook-github-bot
authored andcommitted
Migrate UiThreadUtil to Kotlin (#50536)
Summary: This PR aims to migrate UiThreadUtil from Java to kotlin as part of #50513 ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID][CHANGED]Migrate UiThreadUtil to Kotlin Pull Request resolved: #50536 Test Plan: Tested on RN tester with both new and old arch Reviewed By: cortinico Differential Revision: D72602041 Pulled By: arushikesarwani94 fbshipit-source-id: 9f45a139805819a21039eb640a0bd1583a3acde9
1 parent 7b01943 commit 1033584

File tree

4 files changed

+81
-85
lines changed

4 files changed

+81
-85
lines changed

Diff for: packages/react-native/ReactAndroid/api/ReactAndroid.api

+9-9
Original file line numberDiff line numberDiff line change
@@ -1476,15 +1476,15 @@ public abstract interface class com/facebook/react/bridge/UIManagerProvider {
14761476
public abstract fun createUIManager (Lcom/facebook/react/bridge/ReactApplicationContext;)Lcom/facebook/react/bridge/UIManager;
14771477
}
14781478

1479-
public class com/facebook/react/bridge/UiThreadUtil {
1480-
public fun <init> ()V
1481-
public static fun assertNotOnUiThread ()V
1482-
public static fun assertOnUiThread ()V
1483-
public static fun getUiThreadHandler ()Landroid/os/Handler;
1484-
public static fun isOnUiThread ()Z
1485-
public static fun removeOnUiThread (Ljava/lang/Runnable;)V
1486-
public static fun runOnUiThread (Ljava/lang/Runnable;)Z
1487-
public static fun runOnUiThread (Ljava/lang/Runnable;J)Z
1479+
public final class com/facebook/react/bridge/UiThreadUtil {
1480+
public static final field INSTANCE Lcom/facebook/react/bridge/UiThreadUtil;
1481+
public static final fun assertNotOnUiThread ()V
1482+
public static final fun assertOnUiThread ()V
1483+
public static final fun getUiThreadHandler ()Landroid/os/Handler;
1484+
public static final fun isOnUiThread ()Z
1485+
public static final fun removeOnUiThread (Ljava/lang/Runnable;)V
1486+
public static final fun runOnUiThread (Ljava/lang/Runnable;)Z
1487+
public static final fun runOnUiThread (Ljava/lang/Runnable;J)Z
14881488
}
14891489

14901490
public final class com/facebook/react/bridge/UnexpectedNativeTypeException : java/lang/RuntimeException {

Diff for: packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UiThreadUtil.java

-75
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import android.os.Handler
11+
import android.os.Looper
12+
import com.facebook.react.common.build.ReactBuildConfig
13+
14+
/** Utility for interacting with the UI thread. */
15+
public object UiThreadUtil {
16+
17+
private val mainHandler: Handler by
18+
lazy(LazyThreadSafetyMode.NONE) { Handler(Looper.getMainLooper()) }
19+
20+
/**
21+
* Returns the handler associated with the main (UI) thread.
22+
*
23+
* @return The handler for the main thread
24+
*/
25+
@JvmStatic public fun getUiThreadHandler(): Handler = mainHandler
26+
27+
/** @return `true` if the current thread is the UI thread. */
28+
@JvmStatic
29+
public fun isOnUiThread(): Boolean = Looper.getMainLooper().thread == Thread.currentThread()
30+
31+
/**
32+
* Throws an [AssertionException] if the current thread is not the UI thread. This is a no-op in
33+
* production and is only meant to run in debug mode! If you need to check for incorrect-thread
34+
* issues in production, duplicate this code and call it elsewhere.
35+
*/
36+
@JvmStatic
37+
public fun assertOnUiThread() {
38+
if (ReactBuildConfig.DEBUG) {
39+
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!")
40+
}
41+
}
42+
43+
/**
44+
* Throws an [AssertionException] if the current thread is the UI thread. This is a noop in
45+
* production, and is only meant to run in debug mode! If you need to check for incorrect-thread
46+
* issues in production, duplicate this code and call it elsewhere.
47+
*/
48+
@JvmStatic
49+
public fun assertNotOnUiThread() {
50+
if (ReactBuildConfig.DEBUG) {
51+
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!")
52+
}
53+
}
54+
55+
/** Runs the given [Runnable] on the UI thread. */
56+
@JvmStatic
57+
public fun runOnUiThread(runnable: Runnable): Boolean = mainHandler.postDelayed(runnable, 0)
58+
59+
/** Runs the given [Runnable] on the UI thread after the specified delay. */
60+
@JvmStatic
61+
public fun runOnUiThread(runnable: Runnable, delayInMs: Long): Boolean =
62+
mainHandler.postDelayed(runnable, delayInMs)
63+
64+
/** Removes the given [Runnable] on the UI thread. */
65+
@JvmStatic
66+
public fun removeOnUiThread(runnable: Runnable) {
67+
mainHandler.removeCallbacks(runnable)
68+
}
69+
}

Diff for: packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/debuggingoverlay/DebuggingOverlay.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ internal class DebuggingOverlay(context: Context) : View(context) {
3535
for (traceUpdate in traceUpdates) {
3636
val traceUpdateId = traceUpdate.id
3737
if (traceUpdateIdToCleanupRunnableMap.containsKey(traceUpdateId)) {
38-
UiThreadUtil.removeOnUiThread(traceUpdateIdToCleanupRunnableMap[traceUpdateId])
38+
traceUpdateIdToCleanupRunnableMap[traceUpdateId]?.let { runnable ->
39+
UiThreadUtil.removeOnUiThread(runnable)
40+
}
3941
traceUpdateIdToCleanupRunnableMap.remove(traceUpdateId)
4042
}
4143

0 commit comments

Comments
 (0)